base64ct/lib.rs
1#![no_std]
2#![cfg_attr(docsrs, feature(doc_auto_cfg))]
3#![doc(
4 html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
5 html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
6)]
7#![doc = include_str!("../README.md")]
8#![warn(
9 clippy::integer_arithmetic,
10 clippy::mod_module_files,
11 clippy::panic,
12 clippy::panic_in_result_fn,
13 clippy::unwrap_used,
14 missing_docs,
15 rust_2018_idioms,
16 unsafe_code,
17 unused_lifetimes,
18 unused_qualifications
19)]
20
21//! # Usage
22//!
23//! ## Allocating (enable `alloc` crate feature)
24//!
25//! ```
26//! # #[cfg(feature = "alloc")]
27//! # {
28//! use base64ct::{Base64, Encoding};
29//!
30//! let bytes = b"example bytestring!";
31//! let encoded = Base64::encode_string(bytes);
32//! assert_eq!(encoded, "ZXhhbXBsZSBieXRlc3RyaW5nIQ==");
33//!
34//! let decoded = Base64::decode_vec(&encoded).unwrap();
35//! assert_eq!(decoded, bytes);
36//! # }
37//! ```
38//!
39//! ## Heapless `no_std` usage
40//!
41//! ```
42//! use base64ct::{Base64, Encoding};
43//!
44//! const BUF_SIZE: usize = 128;
45//!
46//! let bytes = b"example bytestring!";
47//! assert!(Base64::encoded_len(bytes) <= BUF_SIZE);
48//!
49//! let mut enc_buf = [0u8; BUF_SIZE];
50//! let encoded = Base64::encode(bytes, &mut enc_buf).unwrap();
51//! assert_eq!(encoded, "ZXhhbXBsZSBieXRlc3RyaW5nIQ==");
52//!
53//! let mut dec_buf = [0u8; BUF_SIZE];
54//! let decoded = Base64::decode(encoded, &mut dec_buf).unwrap();
55//! assert_eq!(decoded, bytes);
56//! ```
57//!
58//! # Implementation
59//!
60//! Implemented using integer arithmetic alone without any lookup tables or
61//! data-dependent branches, thereby providing portable "best effort"
62//! constant-time operation.
63//!
64//! Not constant-time with respect to message length (only data).
65//!
66//! Adapted from the following constant-time C++ implementation of Base64:
67//!
68//! <https://github.com/Sc00bz/ConstTimeEncoding/blob/master/base64.cpp>
69//!
70//! Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com).
71//! Derived code is dual licensed MIT + Apache 2 (with permission from Sc00bz).
72
73#[cfg(feature = "alloc")]
74#[macro_use]
75extern crate alloc;
76#[cfg(feature = "std")]
77extern crate std;
78
79mod alphabet;
80mod decoder;
81mod encoder;
82mod encoding;
83mod errors;
84mod line_ending;
85
86#[cfg(test)]
87mod test_vectors;
88
89pub use crate::{
90 alphabet::{
91 bcrypt::Base64Bcrypt,
92 crypt::Base64Crypt,
93 shacrypt::Base64ShaCrypt,
94 standard::{Base64, Base64Unpadded},
95 url::{Base64Url, Base64UrlUnpadded},
96 },
97 decoder::Decoder,
98 encoder::Encoder,
99 encoding::Encoding,
100 errors::{Error, InvalidEncodingError, InvalidLengthError},
101 line_ending::LineEnding,
102};
103
104/// Minimum supported line width.
105const MIN_LINE_WIDTH: usize = 4;