BSV v0.1.0-dev.2 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)
...>
...> "hello world"
...> |> BSV.Crypto.RSA.sign(private_key)
...> |> BSV.Crypto.RSA.verify("hello world", 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 public 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.
Options
The accepted options are:
:encoding
- Optionally decode the given cipher text with either the:base64
or:hex
encoding scheme.
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() | BSV.Crypto.RSA.PrivateKey.t(), keyword() ) :: binary()
Encrypts the given data with the given public or private key.
Options
The accepted options are:
:encoding
- 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
...> private_key.__struct__ == BSV.Crypto.RSA.PrivateKey
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:
:encoding
- 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 >>
verify(signature, message, public_key, options \\ [])
View Sourceverify(binary(), binary(), BSV.Crypto.RSA.PublicKey.t(), keyword()) :: boolean()
Verifies the given message and signature, using the given public key.
Options
The accepted options are:
:encoding
- Optionally decode the given signature with either the:base64
or:hex
encoding scheme.:salt_length
- Specify the RSA-PSS salt length (defaults to20
).
Examples
BSV.Crypto.RSA.verify(signature, message, public_key)
true