[−][src]Crate b64ct
Pure Rust implementation of "B64" encoding, a minified subset of the standard Base64 encoding (RFC 4648, section 4) used by the PHC string format.
B64 Encoding
Supports the Base64 character subset: [A-Z]
, [a-z]
, [0-9]
, +
, /
but disallows padding (=
) as well as extra whitespace.
Full spec is given in the the PHC string format specification:
https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md#b64
The B64 encoding is the standard Base64 encoding (RFC 4648, section 4) except that the padding
=
signs are omitted, and extra characters (whitespace) are not allowed:
Input is split into successive groups of bytes. Each group, except possibly the last one, contains exactly three bytes.
For a group of bytes b0, b1 and b2, compute the following value:
x = (b0 << 16) + (b1 << 8) + b2
Then split
x
into four 6-bit valuesy0
,y1
,y2
andy3
such that:
x = (y0 << 18) + (y1 << 12) + (y2 << 6) + y3
Each 6-bit value is encoded into a character in the
[A-Za-z0-9+/]
alphabet, in that order:
A
..Z
= 0 to 25a
..z
= 26 to 510
..9
= 52 to 61+
= 62/
= 63If the last group does not contain exactly three bytes, then:
- The group is completed with one or two bytes of value
0x00
, then processed as above.- The resulting sequence of characters is truncated to its first two characters (if the group initially contained a single byte) or to its first three characters (if the group initially contained two bytes).
A B64-encoded value thus yields a string whose length, taken modulo 4, can be equal to 0, 2 or 3, but not to 1. Take note that a sequence of characters of the right length may still be an invalid encoding if it defines some non-zero trailing bits in the last incomplete group; producers MUST set the trailing bits to 0, while consumers MAY ignore them, or MAY reject such invalid encodings.
Implementation
Implemented without data-dependent branches or lookup tables, thereby providing portable "best effort" constant-time operation.
Adapted from the following constant-time C++ implementation of Base64:
https://github.com/Sc00bz/ConstTimeEncoding/blob/master/base64.cpp
Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com). Derived code is dual licensed MIT + Apache 2 (with permission from Sc00bz).
Structs
InvalidEncodingError | Invalid encoding of provided "B64" string. |
InvalidLengthError | Insufficient output buffer length. |
Enums
Error | Generic error, union of |
Functions
decode | "B64" decode the given source byte slice into the provided destination buffer. |
decode_in_place | Decode B64-encoded string in-place. |
decode_vec | alloc Decode a "B64"-encoded string into a byte vector. |
decoded_len | Get the length of the output from decoding the provided "B64"-encoded input. |
encode | Encode the input byte slice as "B64", writing the result into the provided destination slice, and returning an ASCII-encoded string value. |
encode_string | alloc Encode the input byte slice as a "B64"-encoded |
encoded_len | Get the "B64"-encoded length of the given byte slice. |