Expand description
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
Function | Output | Allocates |
---|---|---|
encode | Returns a new String | Always |
encode_with_buffer | Returns a &str within the buffer | Only if the buffer needs to grow |
encode_slice | Writes to provided &mut [u8] | Never |
§Decoding
Function | Output | Allocates |
---|---|---|
decode | Returns a new Vec<u8> | Always |
decode_with_buffer | Returns a &[u8] within the buffer | Only if the buffer needs to grow |
decode_slice | Writes 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 Size | radix64 Throughput | base64 Throughput |
---|---|---|
3 bytes | 508 MiB/s | 344 MiB/s |
32 bytes | 2.09 GiB/s | 1.30 GiB/s |
128 bytes | 4.16 GiB/s | 1.92 GiB/s |
8192 bytes | 5.94 GiB/s | 2.25 GiB/s |
§Decoding
Input Byte Size | radix64 Throughput | base64 Throughput |
---|---|---|
3 bytes | 293 MiB/s | 178 MiB/s |
32 bytes | 1.47 GiB/s | 973 MiB/s |
128 bytes | 3.85 GiB/s | 1.55 GiB/s |
8192 bytes | 8.21 GiB/s | 1.93 GiB/s |
§Without any SIMD optimizations (–no-default-features)
§Encoding
Input Byte Size | radix64 Throughput | base64 Throughput |
---|---|---|
3 bytes | 530 MiB/s | 347 MiB/s |
32 bytes | 1.54 GiB/s | 1.29 GiB/s |
128 bytes | 2.01 GiB/s | 1.91 GiB/s |
8192 bytes | 2.25 GiB/s | 2.20 GiB/s |
§Decoding
Input Byte Size | radix64 Throughput | base64 Throughput |
---|---|---|
3 bytes | 302 MiB/s | 178 MiB/s |
32 bytes | 969 MiB/s | 976 MiB/s |
128 bytes | 1.59 GiB/s | 1.55 GiB/s |
8192 bytes | 2.04 GiB/s | 1.98 GiB/s |
Modules§
- The different varieties of base64.
- Utilities for encoding and decoding from std::io::Read and std::io::Write.
Structs§
- A custom defined alphabet and padding.
- Display is a convenience wrapper that provides a Display impl for the passed in data.
Enums§
- Errors that can occur during decoding.
Constants§
- Encode and Decode using the
crypt(3)
character set. - Encode and Decode using a fast alphabet with no padding.
- Encode and Decode using the standard characer set with padding.
- Encode and Decode using the standard characer set without padding.
- Encode and Decode using the URL safe characer set with padding.
- Encode and Decode using the URL safe characer set without padding.
Traits§
- Config represents a base64 configuration.