Fields.AES (fields v2.9.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.

Link to this section Summary

Functions

Decrypt a binary using GCM.

Encrypt Using AES Galois/Counter Mode (GCM) https://en.wikipedia.org/wiki/Galois/Counter_Mode 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!

Link to this section Functions

Link to this function

decrypt(ciphertext)

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

Decrypt a binary using GCM.

parameters

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

Example

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

Link to this function

encrypt(plaintext)

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

Encrypt Using AES Galois/Counter Mode (GCM) https://en.wikipedia.org/wiki/Galois/Counter_Mode 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

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

Examples

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