Crate base64ct[−][src]
Pure Rust implementation of Base64 encoding (RFC 4648, section 4)
with a constant-time no_std
-friendly implementation.
About
This crate implements several Base64 variants in constant-time.
The padded variants require (=
) padding. Unpadded variants expressly
reject such padding.
Whitespace is expressly disallowed.
Minimum Supported Rust Version
This crate requires Rust 1.47 at a minimum.
We may change the MSRV in the future, but it will be accompanied by a minor version bump.
Usage
Allocating (enable alloc
crate feature)
use base64ct::{Base64, Encoding}; let bytes = b"example bytestring!"; let encoded = Base64::encode_string(bytes); assert_eq!(encoded, "ZXhhbXBsZSBieXRlc3RyaW5nIQ=="); let decoded = Base64::decode_vec(&encoded).unwrap(); assert_eq!(decoded, bytes);
Heapless no_std
usage
use base64ct::{Base64, Encoding}; const BUF_SIZE: usize = 128; let bytes = b"example bytestring!"; assert!(Base64::encoded_len(bytes) <= BUF_SIZE); let mut enc_buf = [0u8; BUF_SIZE]; let encoded = Base64::encode(bytes, &mut enc_buf).unwrap(); assert_eq!(encoded, "ZXhhbXBsZSBieXRlc3RyaW5nIQ=="); let mut dec_buf = [0u8; BUF_SIZE]; let decoded = Base64::decode(encoded, &mut dec_buf).unwrap(); assert_eq!(decoded, bytes);
Implementation
Implemented using bitwise arithmetic alone without any lookup tables or data-dependent branches, thereby providing portable “best effort” constant-time operation.
Not constant-time with respect to message length (only data).
Adapted from the following constant-time C++ implementation of Base64:
https://github.com/Sc00bz/ConstTimeEncoding/blob/master/base64.cpp
Copyright (c) 2014 Steve “Sc00bz” Thomas (steve at tobtu dot com). Derived code is dual licensed MIT + Apache 2 (with permission from Sc00bz).
Structs
Base64 | Standard Base64 encoding with |
Base64Bcrypt | bcrypt Base64 encoding. |
Base64Crypt |
|
Base64Unpadded | Standard Base64 encoding without padding. |
Base64Url | URL-safe Base64 encoding with |
Base64UrlUnpadded | URL-safe Base64 encoding without padding. |
InvalidEncodingError | Invalid encoding of provided Base64 string. |
InvalidLengthError | Insufficient output buffer length. |
Enums
Error | Generic error, union of |
Traits
Encoding | Base64 encoding trait. |