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.
from "std/base32" import { Base32 }decode is for machine-produced input, where anything unexpected means corruption. decodeLoose is for secrets a human transcribed:
let secret = Base32.decodeLoose("JBSW Y3DP EHPK 3PXP")! // as pasted from an authenticator appAPI reference
Base32
pub struct Base32Base32 namespace — call as Base32.encode(...), Base32.decode(...).
Base32.decode
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
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
fn Base32.encode(input: &string): stringEncode bytes as Base32 with '=' padding to a multiple of 8 characters.