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

Decrypts the encrypted data with the given public or private key.

Examples

BSV.Crypto.RSA.decrypt(encrypted_binary, public_or_private_key)
<< decrypted binary >>
Link to this function

encrypt(data, key, options \\ [])

View Source

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 >>
Link to this function

generate_key(options \\ [])

View Source
generate_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 to 2048.

Examples

iex> private_key = BSV.Crypto.RSA.generate_key
...> (%BSV.Crypto.RSA.PrivateKey{} = private_key) == private_key
true

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

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
Link to this function

sign(message, private_key, options \\ [])

View Source

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 to 20).

Examples

BSV.Crypto.RSA.sign("hello world", private_key, encode: :base64)
<< signature >>
Link to this function

verify(message, signature, public_key, options \\ [])

View Source

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 to 20).

Examples

BSV.Crypto.RSA.verify(signature, public_key)