Skip to content

std/base32 ​

Base32 encode/decode (RFC 4648), pure Milo. The encoding otpauth:// URIs use for TOTP/HOTP secrets, and what DNS, S/MIME, and many license keys use for case-insensitive, human-transcribable binary. Alphabet A–Z 2–7, MSB-first, = padding. Input and output are byte strings, matching std/base64.

milo
from "std/base32" import { Base32 }

decode is for machine-produced input, where anything unexpected means corruption. decodeLoose is for secrets a human transcribed:

milo
let secret = Base32.decodeLoose("JBSW Y3DP EHPK 3PXP")!   // as pasted from an authenticator app

API reference ​

Base32 ​

milo
pub struct Base32

Base32 namespace — call as Base32.encode(...), Base32.decode(...).

Base32.decode ​

milo
fn Base32.decode(input: &string): Result<string>

Strict RFC 4648 decode. Requires a multiple-of-8 padded length and A–Z2–7 symbols (lowercase accepted); whitespace, '-' and any other byte are errors.

Base32.decodeLoose ​

milo
fn Base32.decodeLoose(input: &string): Result<string>

Decode a human-transcribed secret: ASCII whitespace, '-' and '=' are skipped and padding is optional. Non-alphabet bytes are still an error.

Base32.encode ​

milo
fn Base32.encode(input: &string): string

Encode bytes as Base32 with '=' padding to a multiple of 8 characters.