Skip to main content

Crate base64

Crate base64 

Source
Expand description

Fast base64 encoding and decoding with SIMD acceleration, constant-time operations, and const fn support.

Four alphabet variants are available via Alphabet:

VariantCharactersPadding
StandardA-Za-z0-9+/=
StandardNoPaddingA-Za-z0-9+/none
UrlA-Za-z0-9-_=
UrlNoPaddingA-Za-z0-9-_none

§Feature flags

FlagDescription
stdstd::error::Error trait impls (enabled by default)
allocString/Vec-returning convenience APIs
serdeSerde 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.
DecodeError
Errors that can occur during base64 decoding.
EncodeError
Errors that can occur during base64 encoding.

Functions§

decodealloc
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.
encodealloc
Encodes bytes to a base64 string using the given Alphabet.
encode_array
Encodes data into 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_stringalloc
Appends the base64-encoded representation of data to a String.
encoded_length
Encode Returns the size in bytes of the input data after base64 encoding.