exth_crypto v0.1.6 ExthCrypto.Cipher

Module for symmetric encryption.

Link to this section Summary

Functions

Decrypts the given ciphertext from the given block cipher

Encrypts the given plaintext for the given block cipher

Generate a random initialization vector for the given type of cipher

Link to this section Types

Link to this type cipher()
cipher() :: {atom(), integer(), mode()}
Link to this type ciphertext()
ciphertext() :: binary()
Link to this type init_vector()
init_vector() :: binary()
Link to this type mode()
mode() :: :cbc | :ctr | :ecb
Link to this type plaintext()
plaintext() :: iodata()
Link to this opaque stream() (opaque)
stream()

Link to this section Functions

Link to this function decrypt(ciphertext, symmetric_key, cipher)
Link to this function decrypt(ciphertext, symmetric_key, init_vector, cipher)

Decrypts the given ciphertext from the given block cipher.

Examples

iex> "4f0150273733727f994754fee054df7e18ec169892db5ba973cf8580b898651b"
...> |> ExthCrypto.Math.hex_to_bin
...> |> ExthCrypto.Cipher.decrypt(ExthCrypto.Test.symmetric_key, ExthCrypto.Test.init_vector, {ExthCrypto.AES, ExthCrypto.AES.block_size, :cbc})
<<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>> <> "execute order 66"

iex> "2a7935444247175ff635309b9274e948"
...> |> ExthCrypto.Math.hex_to_bin
...> |> ExthCrypto.Cipher.decrypt(ExthCrypto.Test.symmetric_key, ExthCrypto.Test.init_vector, {ExthCrypto.AES, ExthCrypto.AES.block_size, :ctr})
"execute order 66"

iex> "a73c5576667b7b43a23a9fd930b5465d637a44d08bf702881a8d4e6a5d4944b5"
...> |> ExthCrypto.Math.hex_to_bin
...> |> ExthCrypto.Cipher.decrypt(ExthCrypto.Test.symmetric_key, {ExthCrypto.AES, ExthCrypto.AES.block_size, :ecb})
<<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>> <> "execute order 66"
Link to this function encrypt(plaintext, symmetric_key, cipher)
Link to this function encrypt(plaintext, symmetric_key, init_vector, cipher)

Encrypts the given plaintext for the given block cipher.

Examples

iex> ExthCrypto.Cipher.encrypt("execute order 66", ExthCrypto.Test.symmetric_key, ExthCrypto.Test.init_vector, {ExthCrypto.AES, ExthCrypto.AES.block_size, :cbc}) |> ExthCrypto.Math.bin_to_hex
"4f0150273733727f994754fee054df7e18ec169892db5ba973cf8580b898651b"

iex> ExthCrypto.Cipher.encrypt("execute order 66", ExthCrypto.Test.symmetric_key, ExthCrypto.Test.init_vector, {ExthCrypto.AES, ExthCrypto.AES.block_size, :ctr}) |> ExthCrypto.Math.bin_to_hex
"2a7935444247175ff635309b9274e948"

iex> ExthCrypto.Cipher.encrypt("execute order 66", ExthCrypto.Test.symmetric_key, {ExthCrypto.AES, ExthCrypto.AES.block_size, :ecb}) |> ExthCrypto.Math.bin_to_hex
"a73c5576667b7b43a23a9fd930b5465d637a44d08bf702881a8d4e6a5d4944b5"
Link to this function generate_init_vector(block_size)
generate_init_vector(integer()) :: init_vector()

Generate a random initialization vector for the given type of cipher.

Examples

iex> ExthCrypto.Cipher.generate_init_vector(32) |> byte_size
32

iex> ExthCrypto.Cipher.generate_init_vector(32) == ExthCrypto.Cipher.generate_init_vector(32)
false