wasmium_random/prng/
alphabet.rs

1use crate::{WasmiumRandom, ALPHABET};
2use nanorand::{Rng, WyRand};
3
4impl WasmiumRandom {
5    /// Generate alphabet bytes of size `12`
6    pub fn wyrand_alphabet12() -> [u8; 12] {
7        let mut rng = WyRand::new();
8        let mut random = 0_u8;
9        let mut random_bytes: [u8; 12] = [0; 12];
10
11        (0..12).for_each(|index| {
12            random = rng.generate_range(0_u8..=51);
13            random_bytes[index as usize] = ALPHABET[random as usize].as_bytes()[0];
14        });
15
16        random_bytes
17    }
18
19    /// Generate alphabet bytes of size `24`
20    pub fn wyrand_alphabet24() -> [u8; 24] {
21        let mut rng = WyRand::new();
22        let mut random = 0_u8;
23        let mut random_bytes: [u8; 24] = [0; 24];
24
25        (0..24).for_each(|index| {
26            random = rng.generate_range(0_u8..=51);
27            random_bytes[index as usize] = ALPHABET[random as usize].as_bytes()[0];
28        });
29
30        random_bytes
31    }
32
33    /// Generate alphabet bytes of size `32`
34    pub fn wyrand_alphabet32() -> [u8; 32] {
35        let mut rng = WyRand::new();
36        let mut random = 0_u8;
37        let mut random_bytes: [u8; 32] = [0; 32];
38
39        (0..32).for_each(|index| {
40            random = rng.generate_range(0_u8..=51);
41            random_bytes[index as usize] = ALPHABET[random as usize].as_bytes()[0];
42        });
43
44        random_bytes
45    }
46
47    /// Generate alphabet bytes of size `64`
48    pub fn wyrand_alphabet64() -> [u8; 64] {
49        let mut rng = WyRand::new();
50        let mut random = 0_u8;
51        let mut random_bytes: [u8; 64] = [0; 64];
52
53        (0..64).for_each(|index| {
54            random = rng.generate_range(0_u8..=51);
55            random_bytes[index as usize] = ALPHABET[random as usize].as_bytes()[0];
56        });
57
58        random_bytes
59    }
60
61    /// Generate alphabet bytes of size `128`
62    pub fn wyrand_alphabet128() -> [u8; 128] {
63        let mut rng = WyRand::new();
64        let mut random = 0_u8;
65        let mut random_bytes: [u8; 128] = [0; 128];
66
67        (0..128).for_each(|index| {
68            random = rng.generate_range(0_u8..=51);
69            random_bytes[index as usize] = ALPHABET[random as usize].as_bytes()[0];
70        });
71
72        random_bytes
73    }
74
75    /// Generate alphabet bytes of size `256`
76    pub fn wyrand_alphabet256() -> [u8; 256] {
77        let mut rng = WyRand::new();
78        let mut random = 0_u8;
79        let mut random_bytes: [u8; 256] = [0; 256];
80
81        (0..256).for_each(|index| {
82            random = rng.generate_range(0_u8..=51);
83            random_bytes[index as usize] = ALPHABET[random as usize].as_bytes()[0];
84        });
85
86        random_bytes
87    }
88
89    /// Generate alphabet bytes of size `512`
90    pub fn wyrand_alphabet512() -> [u8; 512] {
91        let mut rng = WyRand::new();
92        let mut random = 0_u8;
93        let mut random_bytes: [u8; 512] = [0; 512];
94
95        (0..512).for_each(|index| {
96            random = rng.generate_range(0_u8..=51);
97            random_bytes[index as usize] = ALPHABET[random as usize].as_bytes()[0];
98        });
99
100        random_bytes
101    }
102
103    /// Generate alphabet bytes of size `1024`
104    pub fn wyrand_alphabet1024() -> [u8; 1024] {
105        let mut rng = WyRand::new();
106        let mut random = 0_u8;
107        let mut random_bytes: [u8; 1024] = [0; 1024];
108
109        (0..1024).for_each(|index| {
110            random = rng.generate_range(0_u8..=51);
111            random_bytes[index as usize] = ALPHABET[random as usize].as_bytes()[0];
112        });
113
114        random_bytes
115    }
116
117    /// Generate english alphabet bytes of size `2048`
118    pub fn wyrand_alphabet2048() -> [u8; 2048] {
119        let mut rng = WyRand::new();
120        let mut random = 0_u8;
121        let mut random_bytes: [u8; 2048] = [0; 2048];
122
123        (0..2048).for_each(|index| {
124            random = rng.generate_range(0_u8..=51);
125            random_bytes[index as usize] = ALPHABET[random as usize].as_bytes()[0];
126        });
127
128        random_bytes
129    }
130}
131
132#[cfg(test)]
133mod test_length {
134    use crate::WasmiumRandom;
135    #[test]
136    fn test_wyrand_alphabet() {
137        assert_eq!(12_usize, WasmiumRandom::wyrand_alphabet12().len());
138        assert_eq!(24_usize, WasmiumRandom::wyrand_alphabet24().len());
139        assert_eq!(32_usize, WasmiumRandom::wyrand_alphabet32().len());
140        assert_eq!(64_usize, WasmiumRandom::wyrand_alphabet64().len());
141        assert_eq!(128_usize, WasmiumRandom::wyrand_alphabet128().len());
142        assert_eq!(256_usize, WasmiumRandom::wyrand_alphabet256().len());
143        assert_eq!(512_usize, WasmiumRandom::wyrand_alphabet512().len());
144        assert_eq!(1024_usize, WasmiumRandom::wyrand_alphabet1024().len());
145        assert_eq!(2048_usize, WasmiumRandom::wyrand_alphabet2048().len());
146    }
147}