BSV-ex v0.2.3 BSV.Crypto.ECDSA View Source

Functions for use with ECDSA asymmetric cryptography.

Examples

iex> private_key = BSV.Crypto.ECDSA.generate_key
...> public_key = BSV.Crypto.ECDSA.PrivateKey.get_public_key(private_key)
...>
...> "hello world"
...> |> BSV.Crypto.ECDSA.sign(private_key)
...> |> BSV.Crypto.ECDSA.verify("hello world", public_key)
true

Link to this section Summary

Functions

Generates a new ECDSA private key.

Generates a new ECDSA public and private key pair, returned as a tuple containing two binaries.

Decodes the given PEM string into a ECDSA key.

Encodes the given public or private key into a PEM string.

Creates a signature for the given message, using the given private key.

Verify the given message and signature, using the given public key.

Link to this section Functions

Link to this function

generate_key(options \\ [])

View Source
generate_key(keyword()) :: BSV.Crypto.ECDSA.PrivateKey.t()

Generates a new ECDSA private key.

Options

The accepted options are:

  • :named_curve - Specify the elliptic curve name. Defaults to :secp256k1.

Examples

iex> private_key = BSV.Crypto.ECDSA.generate_key
...> private_key.__struct__ == BSV.Crypto.ECDSA.PrivateKey
true
Link to this function

generate_key_pair(options \\ [])

View Source
generate_key_pair(keyword()) :: {binary(), binary()}

Generates a new ECDSA public and private key pair, returned as a tuple containing two binaries.

Options

The accepted options are:

  • :named_curve - Specify the elliptic curve name. Defaults to :secp256k1.
  • :private_key - When a private key binary is given, it will be returned with its public key.

Examples

iex> BSV.Crypto.ECDSA.generate_key_pair
...> |> is_tuple
true

iex> ecdsa_key = BSV.Crypto.ECDSA.PrivateKey.from_sequence(BSV.Test.ecdsa_key)
...>
...> public_key = BSV.Crypto.ECDSA.generate_key_pair(private_key: ecdsa_key.private_key)
...> |> elem(0)
...> public_key == ecdsa_key.public_key
true

Decodes the given PEM string into a ECDSA key.

Examples

iex> ecdsa_key = BSV.Crypto.ECDSA.PrivateKey.from_sequence(BSV.Test.ecdsa_key)
...> pem = BSV.Crypto.ECDSA.pem_encode(ecdsa_key)
...>
...> imported_key = BSV.Crypto.ECDSA.pem_decode(pem)
...> imported_key == ecdsa_key
true

Encodes the given public or private key into a PEM string.

Examples

iex> BSV.Crypto.ECDSA.PrivateKey.from_sequence(BSV.Test.ecdsa_key)
...> |> BSV.Crypto.ECDSA.pem_encode
...> |> String.starts_with?("-----BEGIN EC 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:

  • :named_curve - Specific the elliptic curve name. Defaults to :secp256k1.
  • :encoding - Optionally encode the returned binary with either the :base64 or :hex encoding scheme.

Examples

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

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

View Source

Verify 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.
  • :named_curve - Specific the elliptic curve name. Defaults to :secp256k1.

Examples

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