Skip to content

Base64Url ​

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

Import base64Url from tyrus:encoding. encode(bytes) produces the URL-safe RFC 4648 base64 representation, substituting - for + and _ for / and omitting the = padding. The alphabet contains no characters that need percent-encoding in URLs or within JSON strings, and the unpadded form stays the shortest one. decode(text) reverses the encoding strictly: padding and standard-alphabet characters are rejected with ERR_ENCODING_INVALID_BASE64.

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

const bytes: u8[] = [0xfb, 0xff, 0x00, 0xde, 0xad];
const encoded: string = base64Url.encode(bytes);
print(encoded); // -_8A3q0

try {
    const decoded: u8[] = base64Url.decode(encoded);
    print(decoded); // [251, 255, 0, 222, 173]
} catch (error: EncodingError) {
    print(error.code);
}

Because padding is omitted, the length of a valid input is never 1 modulo 4; anything else is rejected. Unlike the standard alphabet, the URL-safe variant is not self-delimiting, so encode and decode are exact inverses of each other and never match input with =.