exth_crypto v0.1.6 ExthCrypto.Math

Helpers for basic math functions.

Link to this section Summary

Functions

Simple wrapper function to convert a binary to a hex string

Simple wrapper function to convert a hex string to a binary

Simple function to compute modulo function to work on integers of any sign

Generate a random nonce value of specified length

Left pads a given binary to specified length in bytes

Computes the xor between two equal length binaries

Link to this section Functions

Link to this function bin_to_hex(bin)
bin_to_hex(binary()) :: String.t()

Simple wrapper function to convert a binary to a hex string.

Examples

iex> ExthCrypto.Math.bin_to_hex(<<0x01, 0x02, 0x0a, 0x0d>>)
"01020a0d"
Link to this function hex_to_bin(hex)
hex_to_bin(String.t()) :: binary()

Simple wrapper function to convert a hex string to a binary.

Examples

iex> ExthCrypto.Math.hex_to_bin("01020a0d")
<<0x01, 0x02, 0x0a, 0x0d>>

Simple function to compute modulo function to work on integers of any sign.

Examples

iex> ExthCrypto.Math.mod(5, 2)
1

iex> ExthCrypto.Math.mod(-5, 1337)
1332

iex> ExthCrypto.Math.mod(1337 + 5, 1337)
5

iex> ExthCrypto.Math.mod(0, 1337)
0
Link to this function nonce(nonce_size)
nonce(integer()) :: binary()

Generate a random nonce value of specified length.

Examples

iex> ExthCrypto.Math.nonce(32) |> byte_size
32

iex> ExthCrypto.Math.nonce(32) == ExthCrypto.Math.nonce(32)
false
Link to this function pad(bin, length)
pad(binary(), integer()) :: binary()

Left pads a given binary to specified length in bytes.

This function raises if binary longer than given length already.

Examples

iex> ExthCrypto.Math.pad(<<1, 2, 3>>, 6)
<<0x00, 0x00, 0x00, 0x01, 0x02, 0x03>>

iex> ExthCrypto.Math.pad(<<1, 2, 3>>, 4)
<<0x00, 0x01, 0x02, 0x03>>

iex> ExthCrypto.Math.pad(<<1, 2, 3>>, 3)
<<0x01, 0x02, 0x03>>

iex> ExthCrypto.Math.pad(<<1, 2, 3>>, 0)
** (ArgumentError) argument error

iex> ExthCrypto.Math.pad(<<>>, 0)
<<>>
Link to this function xor(a, b)
xor(binary(), binary()) :: binary()

Computes the xor between two equal length binaries.

Examples

iex> ExthCrypto.Math.xor(<<0b10101010>>, <<0b11110000>>)
<<0b01011010>>