Skip to content

Base64 ​

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

Import base64 from tyrus:encoding. encode(bytes) produces the standard RFC 4648 base64 representation of the byte array, using the +/ alphabet and mandatory = padding so every non-empty result has a length divisible by 4. decode(text) reverses the encoding strictly: only the canonical form is accepted.

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

const bytes: u8[] = [0xde, 0xad, 0xbe, 0xef, 0x61, 0x62];
const encoded: string = base64.encode(bytes);
print(encoded); // 3q2+72Fi

try {
    const decoded: u8[] = base64.decode(encoded);
    print(decoded); // [222, 173, 190, 239, 97, 98]
} catch (error: EncodingError) {
    print(error.code);
}

A strict decoder rejects anything the canonical encoder could not have produced with ERR_ENCODING_INVALID_BASE64: characters outside the +/ alphabet, padding in the middle, a length not divisible by 4, = where the remainder expects one for 2 bytes or two for 1 byte, and trailing bits that were not zero in the source. Whitespace is never ignored.