Fields.AES (fields v2.11.0)

Encrypt values with AES in Galois/Counter Mode (GCM) https://en.wikipedia.org/wiki/Galois/Counter_Mode using a random Initialisation Vector for each encryption, this makes "bruteforce" decryption much more difficult. See encrypt/1 and decrypt/1 for more details.

Summary

Functions

Decrypt a binary using GCM.

Encrypt Using AES GCM. Uses a random IV for each call, and prepends the IV and Tag to the ciphertext. This means that encrypt/1 will never return the same ciphertext for the same value. This makes "cracking" (bruteforce decryption) much harder!

Functions

Link to this function

decrypt(ciphertext)

@spec decrypt(any()) :: String.t()

Decrypt a binary using GCM.

Parameters

  • ciphertext: a binary to decrypt, assuming that the first 16 bytes of the binary are the IV to use for decryption.
  • key_id: the index of the AES encryption key used to encrypt the ciphertext

Example

iex> Fields.AES.encrypt("test") |> Fields.AES.decrypt() "test"

Link to this function

encrypt(plaintext)

@spec encrypt(any()) :: String.t()

Encrypt Using AES GCM. Uses a random IV for each call, and prepends the IV and Tag to the ciphertext. This means that encrypt/1 will never return the same ciphertext for the same value. This makes "cracking" (bruteforce decryption) much harder!

Parameters

  • plaintext: Accepts any data type as all values are converted to a String using to_string before encryption.
  • key_id: the index of the AES encryption key used to encrypt the ciphertext

Examples

iex> Fields.AES.encrypt("tea") != Fields.AES.encrypt("tea") true iex> ciphertext = Fields.AES.encrypt(123) iex> is_binary(ciphertext) true