ya_rand/
encoding.rs

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