BSV-ex v0.2.3 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

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 >>

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 >>
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
...> private_key.__struct__ == BSV.Crypto.RSA.PrivateKey
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:

  • :encoding - 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, encoding: :base64)
<< signature >>
Link to this function

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

View Source

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

Examples

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