[][src]Crate radix64

Base64 encoding and decoding.

Quick Examples

Encode a message using standard base64 alphabet

use radix64::STD;
assert_eq!(STD.encode("my message"), "bXkgbWVzc2FnZQ==");

Encode multiple messages while reusing a single buffer. This can be much more efficient when encoding many messages.

use radix64::STD;
let mut buffer = Vec::new();
assert_eq!(STD.encode_with_buffer("my message", &mut buffer), "bXkgbWVzc2FnZQ==");
assert_eq!(STD.encode_with_buffer("my message2", &mut buffer), "bXkgbWVzc2FnZTI=");
assert_eq!(STD.encode_with_buffer("my message3", &mut buffer), "bXkgbWVzc2FnZTM=");

Decode a message using URL safe alphabet

use radix64::URL_SAFE;
assert_eq!(URL_SAFE.decode("ABCD").unwrap(), &[0, 16, 131]);

Decode multiple messages while reusing a single buffer. This can be much more efficient when decoding many messages.

use radix64::URL_SAFE;
let mut buffer = Vec::new();
assert_eq!(URL_SAFE.decode_with_buffer("ABCD", &mut buffer).unwrap(), &[0, 16, 131]);
assert_eq!(URL_SAFE.decode_with_buffer("ABCE", &mut buffer).unwrap(), &[0, 16, 132]);
assert_eq!(URL_SAFE.decode_with_buffer("ABCF", &mut buffer).unwrap(), &[0, 16, 133]);

Decode data from stdin.

use radix64::{STD, io::DecodeReader};
let mut reader = DecodeReader::new(STD, std::io::stdin());
let mut decoded = Vec::new();
reader.read_to_end(&mut decoded)?;

Encode data to stdout.

use radix64::{STD, io::EncodeWriter};
let mut writer = EncodeWriter::new(STD, std::io::stdout());
writer.write_all("my message".as_bytes())?;

Configs

There are a variety of base64 configurations. There are constants defined representing the most common varieties and the ability to define a custom configuration using CustomConfig. Each configuration has a set of methods for encoding and decoding. The methods are as follows:

Encoding

FunctionOutputAllocates
encodeReturns a new StringAlways
encode_with_bufferReturns a &str within the bufferOnly if the buffer needs to grow
encode_sliceWrites to provided &mut [u8]Never

Decoding

FunctionOutputAllocates
decodeReturns a new Vec<u8>Always
decode_with_bufferReturns a &[u8] within the bufferOnly if the buffer needs to grow
decode_sliceWrites to provided &mut [u8]Never

Performance

The provided configurations STD, URL_SAFE, and CRYPT (along with the NO_PAD alternatives) each provide an AVX2 optimized implementation. When running on an AVX2 enabled CPU this can be dramatically faster. This library also strives to perform efficiently when not using AVX2. Here is a summary of results compared with the base64 (v0.10.1) crate. These results were run on an AVX2 enabled workstation and are only meant to serve as a reference. Performance measurements can be very fickle, always measure a representative workload on your system for the most accurate comparisons.

With AVX2 enabled

Encoding

Input Byte Sizeradix64 Throughputbase64 Throughput
3 bytes508 MiB/s344 MiB/s
32 bytes2.09 GiB/s1.30 GiB/s
128 bytes4.16 GiB/s1.92 GiB/s
8192 bytes5.94 GiB/s2.25 GiB/s

Decoding

Input Byte Sizeradix64 Throughputbase64 Throughput
3 bytes293 MiB/s178 MiB/s
32 bytes1.47 GiB/s973 MiB/s
128 bytes3.85 GiB/s1.55 GiB/s
8192 bytes8.21 GiB/s1.93 GiB/s

Without any SIMD optimizations (--no-default-features)

Encoding

Input Byte Sizeradix64 Throughputbase64 Throughput
3 bytes530 MiB/s347 MiB/s
32 bytes1.54 GiB/s1.29 GiB/s
128 bytes2.01 GiB/s1.91 GiB/s
8192 bytes2.25 GiB/s2.20 GiB/s

Decoding

Input Byte Sizeradix64 Throughputbase64 Throughput
3 bytes302 MiB/s178 MiB/s
32 bytes969 MiB/s976 MiB/s
128 bytes1.59 GiB/s1.55 GiB/s
8192 bytes2.04 GiB/s1.98 GiB/s

Modules

configs

The different varieties of base64.

io

Utilities for encoding and decoding from std::io::Read and std::io::Write.

Structs

CustomConfig

A custom defined alphabet and padding.

Display

Display is a convenience wrapper that provides a Display impl for the passed in data.

Enums

DecodeError

Errors that can occur during decoding.

Constants

CRYPT

Encode and Decode using the crypt(3) character set.

FAST

Encode and Decode using a fast alphabet with no padding.

STD

Encode and Decode using the standard characer set with padding.

STD_NO_PAD

Encode and Decode using the standard characer set without padding.

URL_SAFE

Encode and Decode using the URL safe characer set with padding.

URL_SAFE_NO_PAD

Encode and Decode using the URL safe characer set without padding.

Traits

Config

Config represents a base64 configuration.