CrockfordBase32 (crockford_base32 v0.2.0)

The main module implements Douglas Crockford's Base32 encoding.

Link to this section Summary

Functions

Decode the encoded to an string, all hyphen(s) are removed and ignore the encoded's case.

Decode the encoded to an integer, all hyphen(s) are removed and ignore the encoded's case.

Encode an integer or a binary in Crockford's Base32.

Link to this section Functions

Link to this function

decode_to_binary(string, opts \\ [])

Decode the encoded to an string, all hyphen(s) are removed and ignore the encoded's case.

If the encoded string be with a check symbol, require to use checksum: true in decoding.

example

Example

iex> CrockfordBase32.decode_to_binary("C5H66")
{:ok, "abc"}
iex> CrockfordBase32.decode_to_binary("C5H66C", checksum: true)
{:ok, "abc"}
iex> CrockfordBase32.decode_to_binary("C5H66D", checksum: true)
{:error, "invalid_checksum"}

options

Options

The same to the options of decode_to_integer/2.

Link to this function

decode_to_integer(string, opts \\ [])

Specs

decode_to_integer(String.t(), Keyword.t()) ::
  {:ok, integer()} | {:error, String.t()}

Decode the encoded to an integer, all hyphen(s) are removed and ignore the encoded's case.

If the encoded string be with a check symbol, require to use checksum: true in decoding.

example

Example

iex> CrockfordBase32.decode_to_integer("16JD", checksum: true)
{:ok, 1234}
iex> CrockfordBase32.decode_to_integer("16j")
{:ok, 1234}
iex> CrockfordBase32.decode_to_integer("16j*", checksum: true)
{:error, "invalid_checksum"}

options

Options

  • :checksum, optional, a boolean, by defaults to false means expect input the encoded string without a check symbol in its tail, if set it as true, please ensure input encoded is a string be with a check symbol, or return {:error, "invalid_checksum"}.
Link to this function

encode(value, opts \\ [])

Specs

encode(integer() | binary(), Keyword.t()) :: String.t()

Encode an integer or a binary in Crockford's Base32.

After encoded, the return string only contains these characters set("0123456789ABCDEFGHJKMNPQRSTVWXYZ", 10 digits and 22 letters, excluding "I", "L", "O" and "U"), if set checksum: true, there would be with one of these check symbols("*~$=U") or the previous 10 digits and 22 letters as the last character.

example

Example

iex> CrockfordBase32.encode(1234567)
"15NM7"
iex> CrockfordBase32.encode(1234567, checksum: true)
"15NM7S"
iex> CrockfordBase32.encode(1234567, split_size: 3)
"15N-M7"
iex> CrockfordBase32.encode(1234567, split_size: 3, checksum: true)
"15N-M7S"

options

Options

  • :checksum, optional, a boolean, by defaults to false, if set it as true will calculate a check symbol and append it to the return string.
  • :split_size, optional, a positive integer, if set it will use it as a step size to split the return string with hyphen(s).