BSV v0.1.0-dev.1 BSV.Crypto.RSA View Source
Functions for use with RSA asymmetric cryptography.
Examples
iex> private_key = BSV.Crypto.RSA.generate_key
...> public_key = BSV.Crypto.RSA.PrivateKey.get_public_key(private_key)
...>
...> "hello world"
...> |> BSV.Crypto.RSA.encrypt(public_key)
...> |> BSV.Crypto.RSA.decrypt(private_key)
"hello world"
iex> private_key = BSV.Crypto.RSA.generate_key
...> public_key = BSV.Crypto.RSA.PrivateKey.get_public_key(private_key)
...>
...> message = "hello world"
...> signature = BSV.Crypto.RSA.sign(message, private_key)
...> BSV.Crypto.RSA.verify(message, signature, public_key)
true
Link to this section Summary
Functions
Decrypts the encrypted data with the given public or private key.
Encrypts the given data with the given public or private key.
Generates a new RSA private key.
Decodes the given PEM string into a public or private key.
Encodes the given public or private key into a PEM string.
Creates a signature for the given message, using the given private key.
Verifies the given message and signature, using the given private key.
Link to this section Functions
decrypt(data, key, options \\ [])
View Sourcedecrypt( binary(), BSV.Crypto.RSA.PublicKey.t() | BSV.Crypto.RSA.PrivateKey.t(), keyword() ) :: binary()
Decrypts the encrypted data with the given public or private key.
Examples
BSV.Crypto.RSA.decrypt(encrypted_binary, public_or_private_key)
<< decrypted binary >>
encrypt(data, key, options \\ [])
View Sourceencrypt( binary(), BSV.Crypto.RSA.PublicKey.t() | {:public, BSV.Crypto.RSA.PublicKey.t()} | {:private, BSV.Crypto.RSA.PrivateKey.t()}, keyword() ) :: binary()
Encrypts the given data with the given public or private key.
The method implicitly assumes the use of a public key, but encryption is possible with a private key by passing the key in a tuple format: {:private, private_key}
.
Options
The accepted options are:
:encode
- Optionally encode the returned binary with either the:base64
or:hex
encoding scheme.:encode
- Optionally encode the returned binary with either the:base64
or:hex
encoding scheme.
Examples
BSV.Crypto.RSA.encrypt("hello world", public_or_private_key)
<< encrypted binary >>
generate_key(options \\ [])
View Sourcegenerate_key(keyword()) :: BSV.Crypto.RSA.PrivateKey.t()
Generates a new RSA private key.
Options
The accepted options are:
:size
- Specific the size of the RSA key. Defaults to2048
.
Examples
iex> private_key = BSV.Crypto.RSA.generate_key
...> (%BSV.Crypto.RSA.PrivateKey{} = private_key) == private_key
true
pem_decode(pem)
View Sourcepem_decode(binary()) :: BSV.Crypto.RSA.PublicKey.t() | BSV.Crypto.RSA.PrivateKey.t()
Decodes the given PEM string into a public or private key.
Examples
iex> public_key = BSV.Crypto.RSA.PrivateKey.from_sequence(BSV.Test.rsa_key)
...> |> BSV.Crypto.RSA.PrivateKey.get_public_key
...> pem = BSV.Crypto.RSA.pem_encode(public_key)
...>
...> imported_key = BSV.Crypto.RSA.pem_decode(pem)
...> imported_key == public_key
true
pem_encode(key)
View Sourcepem_encode(BSV.Crypto.RSA.PublicKey.t() | BSV.Crypto.RSA.PrivateKey.t()) :: binary()
Encodes the given public or private key into a PEM string.
Examples
iex> BSV.Crypto.RSA.PrivateKey.from_sequence(BSV.Test.rsa_key)
...> |> BSV.Crypto.RSA.PrivateKey.get_public_key
...> |> BSV.Crypto.RSA.pem_encode
...> |> String.starts_with?("-----BEGIN RSA PUBLIC KEY-----")
true
iex> BSV.Crypto.RSA.PrivateKey.from_sequence(BSV.Test.rsa_key)
...> |> BSV.Crypto.RSA.pem_encode
...> |> String.starts_with?("-----BEGIN RSA PRIVATE KEY-----")
true
sign(message, private_key, options \\ [])
View Sourcesign(binary(), BSV.Crypto.RSA.PrivateKey.t(), keyword()) :: binary()
Creates a signature for the given message, using the given private key.
Options
The accepted options are:
:encode
- Optionally encode the returned binary with either the:base64
or:hex
encoding scheme.:salt_length
- Specify the RSA-PSS salt length (defaults to20
).
Examples
BSV.Crypto.RSA.sign("hello world", private_key, encode: :base64)
<< signature >>
Verifies the given message and signature, using the given private key.
Options
The accepted options are:
:salt_length
- Specify the RSA-PSS salt length (defaults to20
).
Examples
BSV.Crypto.RSA.verify(signature, public_key)