thirtytwo
Base32 encoding and decoding for Gleam, targeting both Erlang and JavaScript.
Supports five base32 variants:
- RFC 4648 (
encode/decode) — the standard alphabet with optional padding and case-insensitive decoding. - Extended Hex (
hex_encode/hex_decode) — preserves sort order of the underlying binary data (RFC 4648 §7). - Crockford (
crockford_encode/crockford_decode) — omits I, L, O, U to reduce ambiguity; supports hyphens, character aliases, and an optional mod-37 check digit. - z-base-32 (
z_base_32_encode/z_base_32_decode) — human-oriented lowercase alphabet optimized for readability. - Geohash (
geohash_encode/geohash_decode) — the alphabet used by the Geohash geocoding system.
Examples
thirtytwo.encode(<<"wibble":utf8>>, padding: True)
// -> "O5UWEYTMMU======"
thirtytwo.decode("O5UWEYTMMU======")
// -> Ok(<<"wibble":utf8>>)
thirtytwo.crockford_encode(<<"wibble":utf8>>, check: False)
// -> "EXMP4RKCCM"
Values
pub fn crockford_decode(
input: String,
check check: Bool,
) -> Result(BitArray, Nil)
Decode a Crockford base32 string. Hyphens are stripped, decoding is
case-insensitive, and the aliases O→0, I/L→1 are normalized. When
check is True, the trailing check digit is validated.
Returns Error(Nil) on invalid input or a failed check.
pub fn crockford_encode(
input: BitArray,
check check: Bool,
) -> String
Encode a bit array using Crockford’s base32 alphabet. The output is
always unpadded and uppercase. The input must be byte-aligned;
non-byte-aligned bit arrays produce undefined results. When check
is True, a mod-37 check digit is appended.
pub fn decode(input: String) -> Result(BitArray, Nil)
Decode a standard RFC 4648 base32 string. Padding is optional and
decoding is case-insensitive. Returns Error(Nil) on invalid input.
pub fn encode(input: BitArray, padding padding: Bool) -> String
Encode a bit array using the standard RFC 4648 base32 alphabet.
The input must be byte-aligned; non-byte-aligned bit arrays produce
undefined results.
When padding is True, the output is padded with = to a multiple of 8.
pub fn geohash_decode(input: String) -> Result(BitArray, Nil)
Decode a Geohash base32 string. Decoding is case-sensitive.
Returns Error(Nil) on invalid input.
pub fn geohash_encode(input: BitArray) -> String
Encode a bit array using the Geohash base32 alphabet. The output is always lowercase and unpadded. The input must be byte-aligned; non-byte-aligned bit arrays produce undefined results.
pub fn hex_decode(input: String) -> Result(BitArray, Nil)
Decode a base32hex string. Padding is optional and decoding is
case-insensitive. Returns Error(Nil) on invalid input.
pub fn hex_encode(
input: BitArray,
padding padding: Bool,
) -> String
Encode a bit array using the base32hex alphabet (RFC 4648 section 7).
Preserves sort order of the underlying data. The input must be
byte-aligned; non-byte-aligned bit arrays produce undefined results.
When padding is True, the output is padded with = to a multiple of 8.
pub fn z_base_32_decode(input: String) -> Result(BitArray, Nil)
Decode a z-base-32 string. Decoding is case-sensitive.
Returns Error(Nil) on invalid input.
pub fn z_base_32_encode(input: BitArray) -> String
Encode a bit array using the z-base-32 alphabet. The output is always lowercase and unpadded. The input must be byte-aligned; non-byte-aligned bit arrays produce undefined results.