Kryptonite v0.1.6 Kryptonite.Random View Source

This module provides helper functions related to generating data at random.

Link to this section Summary

Functions

Returns a string at random where the length is equal to length. Essentially the same as bytes!/1 but returns a tuple on error instead of raising

Returns a string at random where the length is equal to length

Hashes a given digest over itself for as many times as specified by count

Calculates the entropy rating of a given binary

Link to this section Functions

Link to this function bytes(length) View Source
bytes(integer()) :: {:ok, binary()} | {:error, any()}

Returns a string at random where the length is equal to length. Essentially the same as bytes!/1 but returns a tuple on error instead of raising.

Examples

iex> data = bytes!(16)
iex> {byte_size(data), bit_size(data)}
{16, 128}
iex> bytes(-1)
{:error, :badarg}

Returns a string at random where the length is equal to length.

Examples

iex> data = bytes!(16)
iex> {byte_size(data), bit_size(data)}
{16, 128}
iex> data = bytes!(32)
iex> {byte_size(data), bit_size(data)}
{32, 256}
Link to this function hash_round(digest, count) View Source
hash_round(binary(), pos_integer()) :: <<_::512>>

Hashes a given digest over itself for as many times as specified by count.

Examples

iex> m = "Some message."
iex> hash_round(m, 1) == hash_round(m, 2)
false
iex> hash_round(m, 2) == m |> hash_round(1) |> hash_round(1)
true
Link to this function shannon_entropy(input) View Source
shannon_entropy(binary()) :: float()

Calculates the entropy rating of a given binary.

The Shannon index is calculated by looking at the recurence of values in the given input binary.

Examples

iex> shannon_entropy <<0xCaffee, 0xBadF00d,>>
1.0
iex> shannon_entropy <<1, 2, 3, 4>>
2.0
iex> shannon_entropy "1223334444"
1.8464393446710154