BSV-ex v0.2.2 BSV.Crypto.AES View Source

Functions for use with AES symmetric cryptography.

Examples

iex> secret = BSV.Crypto.AES.generate_secret
...>
...> "hello world"
...> |> BSV.Crypto.AES.encrypt(:gcm, secret)
...> |> BSV.Crypto.AES.decrypt(:gcm, secret)
"hello world"

Link to this section Summary

Functions

Returns a list of supported cypher modes.

Decrypts the given ciphertext using the specified cipher mode with the given secret key.

Encrypts the given data using the specified cipher mode with the given secret key.

Generate a 256 bit random secret key.

Link to this section Functions

Link to this function

cipher_modes()

View Source
cipher_modes() :: list()

Returns a list of supported cypher modes.

Link to this function

decrypt(ciphertext, mode, secret, options \\ [])

View Source
decrypt(binary(), atom(), binary(), keyword()) :: binary()

Decrypts the given ciphertext using the specified cipher mode with the given secret key.

Options

The accepted cipher modes are:

  • `:gcm` - Galois/Counter Mode (GCM)
  • `:cbc` - Cipher Block Chaining (CBC)
  • `:ctr` - Counter (CTR)

The accepted options are:

  • `:aad` - If Additional Authentication Data was specified at encryption, it must be used here to successfully decrypt.
  • `:encoding` - Optionally decode the given cipher text with either the `:base64` or `:hex` encoding scheme.
  • `:iv` - If your own initialization vector was specified at encryption, it must be used here to successfully decrypt.

Examples

iex> "Mku/VQR8uf3UIkCpoALkvWhtCUs+ASpfa9q76zewa0Y6G9t/5u5n"
...> |> BSV.Crypto.AES.decrypt(:gcm, BSV.Test.symetric_key, iv: BSV.Test.iv12, encoding: :base64, aad: "MyAAD")
"hello world"

iex> "quZoaDPv4OXNC5Ze2wmbCA=="
...> |> BSV.Crypto.AES.decrypt(:cbc, BSV.Test.symetric_key, iv: BSV.Test.iv16, encoding: :base64)
"hello world"

iex> "cdf91fda732325cf96de03"
...> |> BSV.Crypto.AES.decrypt(:ctr, BSV.Test.symetric_key, iv: BSV.Test.iv16, encoding: :hex)
"hello world"
Link to this function

encrypt(data, mode, secret, options \\ [])

View Source
encrypt(binary(), atom(), binary(), keyword()) :: binary()

Encrypts the given data using the specified cipher mode with the given secret key.

Options

The accepted cipher modes are:

  • `:gcm` - Galois/Counter Mode (GCM)
  • `:cbc` - Cipher Block Chaining (CBC)
  • `:ctr` - Counter (CTR)

The accepted options are:

  • `:aad` - Provide your own Additional Authentication Data (only used in `:gcm` mode). If not provided, defaults to `"BSV.Crypto.AES"`.
  • `:encoding` - Optionally encode the returned cipher text with either the `:base64` or `:hex` encoding scheme.
  • `:iv` - Provide your own initialization vector. In `:cbc` and `:ctr` mode this is necessary as the same vector is needed to decrypt. In `:gcm` mode it is unnecessary as a random vector is generated and encoded in the returned cipher text.

Examples

iex> BSV.Crypto.AES.encrypt("hello world", :gcm, BSV.Test.symetric_key, iv: BSV.Test.iv12, aad: "MyAAD")
<<50, 75, 191, 85, 4, 124, 185, 253, 212, 34, 64, 169, 160, 2, 228, 189, 104, 109, 9, 75, 62, 1, 42, 95, 107, 218, 187, 235, 55, 176, 107, 70, 58, 27, 219, 127, 230, 238, 103>>

iex> BSV.Crypto.AES.encrypt("hello world", :gcm, BSV.Test.symetric_key, iv: BSV.Test.iv12, aad: "MyAAD", encoding: :base64)
"Mku/VQR8uf3UIkCpoALkvWhtCUs+ASpfa9q76zewa0Y6G9t/5u5n"

iex> BSV.Crypto.AES.encrypt("hello world", :cbc, BSV.Test.symetric_key, iv: BSV.Test.iv16, encoding: :base64)
"quZoaDPv4OXNC5Ze2wmbCA=="

iex> BSV.Crypto.AES.encrypt("hello world", :ctr, BSV.Test.symetric_key, iv: BSV.Test.iv16, encoding: :hex)
"cdf91fda732325cf96de03"
Link to this function

generate_secret()

View Source
generate_secret() :: binary()

Generate a 256 bit random secret key.

Examples

iex> BSV.Crypto.AES.generate_secret
...> |> byte_size
32