Base32

class Base32 extends Object

Converts binary data to and from the RFC 4648 Base32 alphabet. Call both methods on the class itself.

#Import Ks { Base32 }

Encode

Text := Base32.Encode(Value , Encoding := "UTF-8")

Value is a String, StringBuffer or Buffer. Text is converted with Encoding, whose names match FileEncoding. A Buffer retains its bytes. The result is an uppercase String using A-Z and 2-7, with trailing = padding.

An unrecognized encoding raises ValueError; an input which holds no bytes raises TypeError.

Decode

Data := Base32.Decode(Text)

Returns a Buffer containing the decoded bytes. Lowercase letters and omitted trailing padding are accepted, as commonly found in TOTP secrets. Empty input returns an empty Buffer.

Invalid characters, whitespace, impossible lengths, malformed padding and nonzero unused bits raise ValueError.

Examples

#Import Ks { Base32 }
Encoded := Base32.Encode("foobar") ; MZXW6YTBOI======
Bytes := Base32.Decode("mzxw6ytboi")
FileAppend StrGet(Bytes, Bytes.Size, "UTF-8"), "*"

For a complete one-time password example using a Base32 secret and Hex.Decode to read HMAC bytes, see Crypt.Hmac.

Base64, Hex, Crypt.Hmac, Buffer