CrockfordBase32 (crockford_base32 v0.7.2)

The main module implements Douglas Crockford's Base32 encoding.

Summary

Functions

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

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

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

Functions

Link to this function

decode_to_bitstring(input, opts \\ [])

@spec decode_to_bitstring(bitstring(), Keyword.t()) ::
  {:ok, bitstring()} | :error | :error_checksum

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

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

Example

iex> CrockfordBase32.decode_to_bitstring("C5H66")
{:ok, "abc"}
iex> CrockfordBase32.decode_to_bitstring("C5H66C", checksum: true)
{:ok, "abc"}
iex> CrockfordBase32.decode_to_bitstring("C5H66D", checksum: true)
:error_checksum

Options

The same to the options of decode_to_integer/2.

Link to this function

decode_to_integer(string, opts \\ [])

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

Decode the encoded string 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

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_checksum

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_checksum.
Link to this function

encode(value, opts \\ [])

@spec encode(integer() | bitstring(), Keyword.t()) :: String.t()

Encode an integer or a bitstring 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

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

  • :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).