Expand description
Fast base64 encoding and decoding with SIMD acceleration, constant-time
operations, and const fn support.
Four alphabet variants are available via Alphabet:
| Variant | Characters | Padding |
|---|---|---|
Standard | A-Za-z0-9+/ | = |
StandardNoPadding | A-Za-z0-9+/ | none |
Url | A-Za-z0-9-_ | = |
UrlNoPadding | A-Za-z0-9-_ | none |
§Feature flags
| Flag | Description |
|---|---|
std | std::error::Error trait impls (enabled by default) |
alloc | String/Vec-returning convenience APIs |
serde | Serde serialize/deserialize helpers |
§Performance
The encode_into and decode_into functions
automatically dispatch to SIMD-accelerated paths (AVX2 on x86/x86_64,
NEON on aarch64). When a constant-time guarantee is required, use
encode_into_constant_time or decode_into_constant_time.
§const fn support
encode_array and decode_array are const fn, enabling base64
encoding and decoding at compile time.
§Examples
use base64::{Alphabet, encode, decode};
let encoded = encode(b"hello world", Alphabet::Standard);
assert_eq!(encoded, "aGVsbG8gd29ybGQ=");
let decoded = decode(b"aGVsbG8gd29ybGQ=", Alphabet::Standard).unwrap();
assert_eq!(decoded, b"hello world");
let url = encode(b"hello world", Alphabet::Url);
assert_eq!(url, "aGVsbG8gd29ybGQ=");Enums§
- Alphabet
- The base64 alphabet used for encoding and decoding.
- Decode
Error - Errors that can occur during base64 decoding.
- Encode
Error - Errors that can occur during base64 encoding.
Functions§
- decode
alloc - Decode Decodes a base64 string into bytes.
- decode_
array - Decodes a base64 string into a fixed-size array at compile time.
- decode_
into - Decodes a base64 string into an existing buffer.
- decode_
into_ constant_ time - Constant-time base64 decoding. Processes all input data without secret-dependent branches or memory accesses, making it suitable for cryptographic applications.
- encode
alloc - Encodes bytes to a base64 string using the given
Alphabet. - encode_
array - Encodes
datainto a fixed-size array at compile time. - encode_
into - Encodes bytes into an existing buffer.
- encode_
into_ constant_ time - Constant-time base64 encoding. Processes all input data without secret-dependent branches or memory accesses, making it suitable for cryptographic applications.
- encode_
into_ string alloc - Appends the base64-encoded representation of
datato aString. - encoded_
length - Encode Returns the size in bytes of the input data after base64 encoding.