Base32 v0.1.0

Source
Base32 in the RFC 4648 and Crockford alphabets.

Base32Variant

pub enum Base32Variant {
    Standard,
    StandardUnpadded,
    Crockford
}

Which Base32 alphabet a text uses, and whether it is padded. Copy, carrying no payload.

Source

Cases

NameDescription
StandardThe RFC 4648 alphabet, A through Z then 2 through 7, padded with = to a multiple of eight characters.
StandardUnpaddedThe RFC 4648 alphabet without padding, which is how one-time-password secrets are usually written.
CrockfordCrockford's alphabet, 0 through 9 then the letters except I, L, O and U, without padding. Read leniently, as its specification asks: either case, I and L as 1, O as 0, and hyphens skipped.

Base32EncodedLength

pub func Base32EncodedLength(
    length: uint,
    form: Base32Variant
) -> uint

How many characters the Base32 form of length bytes occupies in form. Eight characters for every five bytes, with a final partial group taking two, four, five or seven characters, or eight with padding. Cannot fail for any length a slice in memory can have; runs in constant time and allocates nothing.

Source

Parameters

NameTypeDescription
lengthuintthe number of bytes to encode
formBase32Variantthe alphabet and padding to encode with

Returns: the number of characters Base32Encode writes

Base32Encode

pub func Base32Encode(
    bytes: byte[..],
    into: var char8[..],
    form: Base32Variant
) -> uint ! FormatError

pub func Base32Encode(
    text: char8[..],
    into: var char8[..],
    form: Base32Variant
) -> uint ! FormatError

Writes bytes as Base32 into into. Letters are written in upper case in both alphabets. The RFC 4648 variants run in time that depends only on the length of bytes. Reports InsufficientCapacity when into is shorter than Base32EncodedLength(bytes.length, form), leaving it exactly as it was; room past the encoded form is not touched.

OverloadSummary
pub func Base32Encode(bytes: byte[..], into: var char8[..], form: Base32Variant) -> uint ! FormatErrorWrites bytes as Base32 into into.
pub func Base32Encode(text: char8[..], into: var char8[..], form: Base32Variant) -> uint ! FormatErrorWrites the bytes of text as Base32 into into.

Source

Parameters

NameTypeDescription
bytesbyte[..]the bytes to encode
intovar char8[..]the destination, at least Base32EncodedLength(bytes.length, form) characters
formBase32Variantthe alphabet and padding to encode with
textchar8[..]the text whose bytes to encode

Returns: the number of characters written

Base32DecodedLength

pub func Base32DecodedLength(
    text: char8[..],
    form: Base32Variant
) -> uint ! DecodeError

How many bytes the Base32 text decodes to, after checking its length and the placement of its padding. Only the shape is checked; the characters are checked by Base32Decode. The padded variant needs a multiple of eight characters; every variant needs the data characters to leave a final group of zero, two, four, five or seven. Hyphens in Crockford text are not counted. Reports InvalidLength at the end of the text, or InvalidPadding at the first = that is out of place. Runs in time linear in the length and allocates nothing.

Source

Parameters

NameTypeDescription
textchar8[..]the Base32 text
formBase32Variantthe alphabet and padding the text is expected to use

Returns: the number of bytes Base32Decode writes for it

Base32Decode

pub func Base32Decode(
    text: char8[..],
    into: var byte[..],
    form: Base32Variant
) -> uint ! DecodeError

Reads Base32 text in form into into. The RFC 4648 variants run in time that depends only on the length of text when the text is valid. Reports, in this order of precedence, InvalidLength or InvalidPadding for a text of the wrong shape, InvalidCharacter at the first character outside the alphabet, NonCanonical at a last character whose unused bits are not zero, and InsufficientCapacity with the length needed when into is too short. Every refusal leaves into untouched, because the whole text is checked before anything is written.

Source

Parameters

NameTypeDescription
textchar8[..]the Base32 text to decode
intovar byte[..]the destination, at least Base32DecodedLength(text, form) bytes
formBase32Variantthe alphabet and padding the text is expected to use

Returns: the number of bytes written