Crate base64
Correct, fast, and configurable base64 decoding and encoding. Base64 transports binary data efficiently in contexts where only plain text is allowed.
Usage
Use an Engine to decode or encode base64, configured with the base64
alphabet and padding behavior best suited to your application.
Engine setup
There is more than one way to encode a stream of bytes as “base64”. Different applications use different encoding [alphabets][alphabet::Alphabet] and [padding behaviors][engine::general_purpose::GeneralPurposeConfig].
Encoding alphabet
Almost all base64 [alphabets][alphabet::Alphabet] use A-Z, a-z, and
0-9, which gives nearly 64 characters (26 + 26 + 10 = 62), but they differ
in their choice of their final 2.
Most applications use the [standard][alphabet::STANDARD] alphabet specified
in RFC 4648. If that’s all you need, you can get started
quickly by using the pre-configured
[STANDARD][engine::general_purpose::STANDARD] engine, which is also available
in the prelude module as shown here, if you prefer a minimal use
footprint.
use *;
#
Other common alphabets are available in the alphabet module.
URL-safe alphabet
The standard alphabet uses + and / as its two non-alphanumeric tokens,
which cannot be safely used in URL’s without encoding them as %2B and
%2F.
To avoid that, some applications use a [“URL-safe” alphabet][alphabet::URL_SAFE],
which uses - and _ instead. To use that alternative alphabet, use the
[URL_SAFE][engine::general_purpose::URL_SAFE] engine. This example doesn't
use prelude to show what a more explicit use would look like.
use ;
#
Padding characters
Each base64 character represents 6 bits (2⁶ = 64) of the original binary data, and every 3 bytes of input binary data will encode to 4 base64 characters (8 bits × 3 = 6 bits × 4 = 24 bits).
When the input is not an even multiple of 3 bytes in length, canonical base64 encoders insert padding characters at the end, so that the output length is always a multiple of 4:
use ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Canonical encoding ensures that base64 encodings will be exactly the same,
byte-for-byte, regardless of input length. But the = padding characters
aren’t necessary for decoding, and they may be omitted by using a
[NO_PAD][engine::general_purpose::NO_PAD] configuration:
use ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
The pre-configured NO_PAD engines will reject inputs containing padding
= characters. To encode without padding and still accept padding while
decoding, create an [engine][engine::general_purpose::GeneralPurpose] with
that [padding mode][engine::DecodePaddingMode].
# use ;
assert_eq!;
Further customization
Decoding and encoding behavior can be customized by creating an [engine][engine::GeneralPurpose] with an [alphabet][alphabet::Alphabet] and [padding configuration][engine::GeneralPurposeConfig]:
use ;
// bizarro-world base64: +/ as the first symbols instead of the last
let alphabet =
new
.unwrap;
// a very weird config that encodes with padding but requires no padding when decoding...?
let crazy_config = new
.with_decode_allow_trailing_bits
.with_encode_padding
.with_decode_padding_mode;
let crazy_engine = new;
let encoded = crazy_engine.encode;
Memory allocation
The [decode][Engine::decode()] and [encode][Engine::encode()] engine methods
allocate memory for their results – decode returns a Vec<u8> and
encode returns a String. To instead decode or encode into a buffer that
you allocated, use one of the alternative methods:
Decoding
| Method | Output | Allocates memory |
|---|---|---|
Engine::decode |
returns a new Vec<u8> |
always |
Engine::decode_vec |
appends to provided Vec<u8> |
if Vec lacks capacity |
Engine::decode_slice |
writes to provided &[u8] |
never |
Encoding
| Method | Output | Allocates memory |
|---|---|---|
Engine::encode |
returns a new String |
always |
Engine::encode_string |
appends to provided String |
if String lacks capacity |
Engine::encode_slice |
writes to provided &[u8] |
never |
Input and output
The base64 crate can [decode][Engine::decode()] and
[encode][Engine::encode()] values in memory, or
[DecoderReader][read::DecoderReader] and
[EncoderWriter][write::EncoderWriter] provide streaming decoding and
encoding for any [readable][std::io::Read] or [writable][std::io::Write]
byte stream.
Decoding
# use io;
use ;
#
Encoding
# use io;
use ;
#
Display
If you only need a base64 representation for implementing the
[Display][std::fmt::Display] trait, use
[Base64Display][display::Base64Display]:
use ;
let value = new;
assert_eq!;
Panics
If length calculations result in overflowing usize, a panic will result.
Modules
- alphabet Provides [Alphabet] and constants for alphabets commonly used in the wild.
-
display
Enables base64'd output anywhere you might use a
Displayimplementation, like a format string. - engine Provides the [Engine] abstraction and out of the box implementations.
- prelude Preconfigured engines for common use cases.
-
read
Implementations of
io::Readto transparently decode base64. -
write
Implementations of
io::Writeto transparently handle base64.