Skip to content

Base32 ​

Signaturebase32.encode(bytes: u8[]): string
Signaturebase32.decode(text: string): Result<u8[], EncodingError>

Import base32 from tyrus:encoding. encode(bytes) produces the canonical uppercase RFC 4648 base32 representation, using the A-Z and 2-7 alphabet with mandatory = padding so every non-empty result has a length divisible by 8. decode(text) reverses the encoding strictly: only the canonical form is accepted.

tyrus
import { EncodingError, base32 } from "tyrus:encoding";

const bytes: u8[] = [0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72];
const encoded: string = base32.encode(bytes);
print(encoded); // MZXW6YTBOI======

try {
    const decoded: u8[] = base32.decode(encoded);
    print(decoded); // [102, 111, 111, 98, 97, 114]
} catch (error: EncodingError) {
    print(error.code);
}

A strict decoder rejects anything the canonical encoder could not have produced with ERR_ENCODING_INVALID_BASE32: lowercase letters, characters outside the A-Z/2-7 alphabet, a length not divisible by 8, = in the middle, padding counts that do not match the byte remainder, and trailing bits that were not zero in the source. Whitespace is never ignored. Variants such as Crockford Base32 or unpadded base32 are not part of this module.