entropy_string v1.0.2 EntropyString.CharSet

EntropyString CharSet functionality.

To generating random strings, EntropyString plucks characters out of a specified CharSet. To facilitate efficient generation of these strings, EntropyString uses CharSets with character counts that are powers of two: 2, 4, 8, 16, 32 and 64. Pre-defined character sets are provided and custom character sets are supported.

Examples

Pre-defined CharSet with 32 characters

iex> EntropyString.CharSet.charset32
"2346789bdfghjmnpqrtBDFGHJLMNPQRT"

Entropy bits per character for charset64

iex> charset = EntropyString.CharSet.charset64
iex> EntropyString.CharSet.bits_per_char(charset)
6

Custom bytes needed to produce a string of 48 entropy bits using charset32

iex> charset = EntropyString.CharSet.charset32
iex> EntropyString.CharSet.bytes_needed(48, charset)
7

Validate custom CharSet

iex> EntropyString.CharSet.validate(<<"HT">>)
true

iex> EntropyString.CharSet.validate(<<"012345">>)
{:error, "Invalid char count: must be one of 2,4,8,16,32,64"}

iex> EntropyString.CharSet.validate(<<"ABCB">>)
{:error, "Chars not unique"}

Link to this section Summary

Functions

Entropy bits per character for charset

Bytes needed to form a string of entropy bits from characters in charset

Lowercase hexidecimal

Binary characters

Strings that don’t look like English words and are easy to parse visually

DNA alphabet

RFC 4648 file system and URL safe character set

Octal characters

Validate charset

Link to this section Functions

Link to this function bits_per_char(charset)

Entropy bits per character for charset

  • charset - CharSet in use

Example

iex> charset = EntropyString.CharSet.charset32
"2346789bdfghjmnpqrtBDFGHJLMNPQRT"
iex> EntropyString.CharSet.bits_per_char(charset)
5
Link to this function bytes_needed(bits, charset)

Bytes needed to form a string of entropy bits from characters in charset

  • bits - entropy bits for string
  • charset - CharSet in use

Returns number of bytes needed to form strings with entropy bits using characters from charset; or

  • {:error, reason} if EntropyString.CharSet.validate(charset) is not true.

Example

iex> charset = EntropyString.CharSet.charset16
"0123456789abcdef"
iex> EntropyString.CharSet.bytes_needed(48, charset)
6

Lowercase hexidecimal

"0123456789abcdef"

Binary characters

"01"

Strings that don’t look like English words and are easy to parse visually

"2346789bdfghjmnpqrtBDFGHJLMNPQRT" 
  • remove all upper and lower case vowels (including y)
  • remove all numbers that look like letters
  • remove all letters that look like numbers
  • remove all letters that have poor distinction between upper and lower case values

DNA alphabet

"ATCG"

No good reason; just wanted to get away from the obvious

RFC 4648 file system and URL safe character set

"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"

Octal characters

"01234567"
Link to this function validate(charset)

Validate charset

  • charset - CharSet to use

Validations

  • charset must have 2, 4, 8, 16, 32, or 64 characters
  • characters must by unique

Examples

iex> EntropyString.CharSet.validate(<<"0123">>)
true

iex> EntropyString.CharSet.validate(<<"01234567890abcdef">>)
{:error, "Invalid char count: must be one of 2,4,8,16,32,64"}

iex> EntropyString.CharSet.validate(<<"01234566">>)
{:error, "Chars not unique"}