ya_rand/
encoding.rs

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