CrockfordBase32 (crockford_base32 v0.9.0)
Copy MarkdownEncodes and decodes non-negative integers and bitstrings with Crockford's Base32.
Summary
Functions
Generates fixed-width encode/1 and decode/1 functions in the calling module.
:bits_size sets the exact bit length. :type is :bitstring (default) or
:integer; :alphabet accepts an optional custom charlist.
@spec decode_to_bitstring(bitstring(), Keyword.t()) :: {:ok, bitstring()} | :error | :error_checksum
Decodes a Crockford Base32 string to a bitstring.
Decoding ignores hyphens and is case-insensitive. Because final zero padding is ambiguous, use a fixed-width encoder when the original bit length must be preserved.
Checksums
With checksum: true, the final non-hyphen symbol is treated as a check
symbol and excluded from the payload. A mismatched valid check symbol returns
:error_checksum; malformed input returns :error.
Use this option only for values encoded with checksum: true; otherwise the
final payload symbol is interpreted as a check symbol.
Examples
iex> CrockfordBase32.decode_to_bitstring("C5H66")
{:ok, "abc"}
iex> CrockfordBase32.decode_to_bitstring("C5H-66C", checksum: true)
{:ok, "abc"}
iex> CrockfordBase32.decode_to_bitstring("C5H66D", checksum: true)
:error_checksum
@spec decode_to_integer(String.t(), Keyword.t()) :: {:ok, non_neg_integer()} | :error | :error_checksum
Decodes a Crockford Base32 string to a non-negative integer.
Decoding ignores hyphens and is case-insensitive. I and L are aliases for
1, and O is an alias for 0.
Checksums
With checksum: true, the final non-hyphen symbol is treated as a check
symbol and excluded from the payload. A mismatched valid check symbol returns
:error_checksum; malformed input returns :error.
Use this option only for values encoded with checksum: true; otherwise the
final payload symbol is interpreted as a check symbol.
Examples
iex> CrockfordBase32.decode_to_integer("16-j")
{:ok, 1234}
iex> CrockfordBase32.decode_to_integer("16-JD", checksum: true)
{:ok, 1234}
iex> CrockfordBase32.decode_to_integer("16J1", checksum: true)
:error_checksum
@spec encode(non_neg_integer() | bitstring(), Keyword.t()) :: String.t()
Encodes a non-negative integer or bitstring.
Bitstrings may have any bit length. Output uses the default Crockford alphabet,
"0123456789ABCDEFGHJKMNPQRSTVWXYZ".
Checksums
Setting checksum: true appends one Crockford check symbol after the payload.
The symbol is calculated modulo 37; for bitstrings, the calculation uses the
original, unpadded bits. It helps detect accidental transcription errors, but
is not a cryptographic integrity or authentication mechanism.
Check symbols extend the output alphabet with "*~$=U". split_size is
applied after the check symbol is appended, so it is grouped with the rest of
the output.
Options
:checksum- set totrueto append a check symbol.:split_size- a positive group size; groups are separated by hyphens.
Examples
iex> CrockfordBase32.encode(1234)
"16J"
iex> CrockfordBase32.encode(1234, checksum: true)
"16JD"
iex> CrockfordBase32.encode("abc", checksum: true, split_size: 3)
"C5H-66C"