Skip to content
Kavachकवच

Standard Library: std.base64

● Intermediate5 min

Every type, method and function of std.base64: what the spec lists, then each declaration in its source, with its examples and the line that declares it.

Spec 14.20 · learn it in Base64.

A declaration below shows what a program writes against: a type's fields, a function's parameters, what it gives, its contracts (require, ensure) and its examples. kavach test runs every example, so each one is true of the code.

What the spec lists

FunctionGives
base64.encode(data, url: false, pad: true) -> Stringdata (Bytes) as base64 text: URL-safe when url, with = padding when pad
base64.decode(text, url: false) -> Bytes ! Base64ErrorThe bytes text encodes, in the alphabet url picks

Decoding is strict, so one value has one text, which signed tokens rely on. Padding may be left out, but when it is there it must be right. The errors:

Base64ErrorWhen
BadCharacter(at: Int)A character outside the alphabet, at this character index
BadLengthA length no encoding gives
BadPadding= where it doesn't belong, or the wrong number of them
ExtraBitsBits after the last byte that aren't zero

std.base64 · base64.kav

use std.base64: 1 type and 0 functions. stdlib/base64/base64.kav

Base64Error type

Why text isn't base64: a character outside the alphabet (at its index), a length no encoding gives, wrong = padding, or bits after the last byte that aren't zero.

pub type Base64Error = BadCharacter(at: Int) | BadLength | BadPadding | ExtraBits