ya_rand/
encoding.rs

1/// Specifies parameters for encoding random data into a valid utf-8 `String`.
2///
3/// All provided implementations list their minimum secure length
4/// in the documentation of their unit struct, and it is highly recommended
5/// that custom implementations do the same.
6///
7/// # Safety
8///
9/// The `CHARSET` field must only contain valid ascii characters.
10/// Meaning that all u8 values must be in the interval [0, 128).
11/// Failure to uphold this condition will result in `text` methods
12/// returning invalid `Strings`'s.
13///
14/// The `MIN_LEN` field must be at least ceil(log<sub>`base`</sub>(2<sup>128</sup>)),
15/// where `base` is the length of the `CHARSET` field. Failure to uphold
16/// this condition will result in poor security of generated `String`'s.
17pub unsafe trait Encoder {
18    /// The character set of the encoder implementation.
19    ///
20    /// See trait-level docs for safety comments.
21    const CHARSET: &[u8];
22
23    /// Shortest length `String` that will contain at least 128 bits
24    /// of randomness.
25    ///
26    /// See trait-level docs for safety comments.
27    const MIN_LEN: usize;
28}
29
30/// Base64 encoding, as specified by RFC 4648.
31///
32/// Minimum secure length is 22.
33pub struct Base64;
34unsafe impl Encoder for Base64 {
35    const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
36
37    const MIN_LEN: usize = 22;
38}
39
40/// Base64 encoding for URLs and filenames, as specified by RFC 4648.
41///
42/// Minimum secure length is 22.
43pub struct Base64URL;
44unsafe impl Encoder for Base64URL {
45    const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
46
47    const MIN_LEN: usize = 22;
48}
49
50/// Base62 (alphanumeric) encoding.
51///
52/// Minimum secure length is 22.
53pub struct Base62;
54unsafe impl Encoder for Base62 {
55    const CHARSET: &[u8] = crate::rng::ALPHANUMERIC;
56
57    const MIN_LEN: usize = 22;
58}
59
60/// Base32 encoding, as specified by RFC 4648.
61///
62/// Minimum secure length is 26.
63pub struct Base32;
64unsafe impl Encoder for Base32 {
65    const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
66
67    const MIN_LEN: usize = 26;
68}
69
70/// Base32 encoding using extended hex, as specified by RFC 4648.
71///
72/// Minimum secure length is 26.
73pub struct Base32Hex;
74unsafe impl Encoder for Base32Hex {
75    const CHARSET: &[u8] = b"0123456789ABCDEFGHIJKLMNOPQRSTUV";
76
77    const MIN_LEN: usize = 26;
78}
79
80/// Base16 (hexidecimal) encoding, as specified by RFC 4648.
81///
82/// Minimum secure length is 32.
83pub struct Base16;
84unsafe impl Encoder for Base16 {
85    const CHARSET: &[u8] = b"0123456789ABCDEF";
86
87    const MIN_LEN: usize = 32;
88}
89
90/// Base16 (lowercase hexidecimal) encoding.
91///
92/// Minimum secure length is 32.
93pub struct Base16Lowercase;
94unsafe impl Encoder for Base16Lowercase {
95    const CHARSET: &[u8] = b"0123456789abcdef";
96
97    const MIN_LEN: usize = 32;
98}