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
Simple wrapper function to convert a binary to a hex string.
Examples
iex> ExthCrypto.Math.bin_to_hex(<<0x01, 0x02, 0x0a, 0x0d>>)
"01020a0d"
Simple wrapper function to convert a hex string to a binary.
Examples
iex> ExthCrypto.Math.hex_to_bin("01020a0d")
<<0x01, 0x02, 0x0a, 0x0d>>
Link to this function
mod(x, n)
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
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
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)
<<>>