Base32-Crockford v1.0.0 Base32Crockford View Source

Base32-Crockford: base-32 encoding for expressing integer numbers in a form that can be conveniently and accurately transmitted between humans and computer systems.

https://www.crockford.com/wrmg/base32.html

A symbol set of 10 digits and 22 letters is used: 0123456789ABCDEFGHJKMNPQRSTVWXYZ It does not include 4 of the 26 letters: I L O U.

A check symbol can be appended to a symbol string. 5 additional symbols *~$=U are used only for encoding or decoding the check symbol.

When decoding, upper and lower case letters are accepted, and i and l will be treated as 1 and o will be treated as 0. When encoding, only upper case letters are used.

Link to this section Summary

Functions

Decodes base32-crockford encoded string into integer number

Similar to decode/2 but raises ArgumentError if a checksum is invalid or an invalid character is present in the string

Encodes an integer number into base32-crockford encoded string

Link to this section Functions

Link to this function decode(binary, opts \\ []) View Source
decode(binary(), keyword()) :: {:ok, integer()} | :error

Decodes base32-crockford encoded string into integer number.

Upper and lower case letters are accepted, and i and l will be treated as 1 and o will be treated as 0.

Hyphens are ignored during decoding.

Options

  • :checksum (boolean) - the last symbol will be considered as check symbol and extracted from the encoded string before decoding. It then will be compared with a check symbol calculated from a decoded number.

Examples

iex> Base32Crockford.decode("X011Z5")
{:ok, 973113317}

iex> Base32Crockford.decode("XoIlZ5")
{:ok, 973113317}

iex> Base32Crockford.decode("X01-1Z5")
{:ok, 973113317}

iex> Base32Crockford.decode("X011Z5$", checksum: true)
{:ok, 973113317}

iex> Base32Crockford.decode("X011Z5=", checksum: true)
:error
Link to this function decode!(binary, opts \\ []) View Source
decode!(binary(), keyword()) :: integer()

Similar to decode/2 but raises ArgumentError if a checksum is invalid or an invalid character is present in the string.

Options

Accepts the same options as decode/2.

Examples

iex> Base32Crockford.decode!("X011Z5")
973113317
Link to this function encode(number, opts \\ []) View Source
encode(integer(), keyword()) :: binary()

Encodes an integer number into base32-crockford encoded string.

Checksum can be added to the end of the string if the :checksum option is set to true.

For better readability the resulting string can be partitioned by hyphens if the :partitions option is provided.

Options

  • :checksum (boolean) - the check symbol will be added to the end of the string. The check symbol encodes the number modulo 37, 37 being the least prime number greater than 32.

  • :partitions (positive integer) - hyphens (-) will be inserted into symbol strings to partition a string into manageable pieces, improving readability by helping to prevent confusion.

Examples

iex> Base32Crockford.encode(973_113_317)
"X011Z5"

To add a check symbol to the end of the string:

iex> Base32Crockford.encode(973_113_317, checksum: true)
"X011Z5$"

To partition a resulting string into pieces:

iex> Base32Crockford.encode(973_113_317, partitions: 2)
"X01-1Z5"

iex> Base32Crockford.encode(973_113_317, partitions: 3)
"X0-11-Z5"

iex> Base32Crockford.encode(973_113_317, partitions: 4)
"X-0-11-Z5"