wasmium_random/csprng/
numeric.rs1use crate::{WasmiumRandom, NUMERIC};
2use nanorand::{ChaCha8, Rng};
3
4impl WasmiumRandom {
5 pub fn secure_numeric12() -> [u8; 12] {
7 let mut rng = ChaCha8::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..=9);
13 random_bytes[index as usize] = NUMERIC[random as usize];
14 });
15
16 random_bytes
17 }
18
19 pub fn secure_numeric24() -> [u8; 24] {
21 let mut rng = ChaCha8::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..=9);
27 random_bytes[index as usize] = NUMERIC[random as usize];
28 });
29
30 random_bytes
31 }
32
33 pub fn secure_numeric32() -> [u8; 32] {
35 let mut rng = ChaCha8::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..=9);
41 random_bytes[index as usize] = NUMERIC[random as usize];
42 });
43
44 random_bytes
45 }
46
47 pub fn secure_numeric64() -> [u8; 64] {
49 let mut rng = ChaCha8::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..=9);
55 random_bytes[index as usize] = NUMERIC[random as usize];
56 });
57
58 random_bytes
59 }
60
61 pub fn secure_numeric128() -> [u8; 128] {
63 let mut rng = ChaCha8::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..=9);
69 random_bytes[index as usize] = NUMERIC[random as usize];
70 });
71
72 random_bytes
73 }
74
75 pub fn secure_numeric256() -> [u8; 256] {
77 let mut rng = ChaCha8::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..=9);
83 random_bytes[index as usize] = NUMERIC[random as usize];
84 });
85
86 random_bytes
87 }
88
89 pub fn secure_numeric512() -> [u8; 512] {
91 let mut rng = ChaCha8::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..=9);
97 random_bytes[index as usize] = NUMERIC[random as usize];
98 });
99
100 random_bytes
101 }
102
103 pub fn secure_numeric1024() -> [u8; 1024] {
105 let mut rng = ChaCha8::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..=9);
111 random_bytes[index as usize] = NUMERIC[random as usize];
112 });
113
114 random_bytes
115 }
116
117 pub fn secure_numeric2048() -> [u8; 2048] {
119 let mut rng = ChaCha8::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..=9);
125 random_bytes[index as usize] = NUMERIC[random as usize];
126 });
127
128 random_bytes
129 }
130}
131
132#[cfg(test)]
133mod test_length {
134 use crate::WasmiumRandom;
135 #[test]
136 fn test_secure_numeric() {
137 assert_eq!(12_usize, WasmiumRandom::secure_numeric12().len());
138 assert_eq!(24_usize, WasmiumRandom::secure_numeric24().len());
139 assert_eq!(32_usize, WasmiumRandom::secure_numeric32().len());
140 assert_eq!(64_usize, WasmiumRandom::secure_numeric64().len());
141 assert_eq!(128_usize, WasmiumRandom::secure_numeric128().len());
142 assert_eq!(256_usize, WasmiumRandom::secure_numeric256().len());
143 assert_eq!(512_usize, WasmiumRandom::secure_numeric512().len());
144 assert_eq!(1024_usize, WasmiumRandom::secure_numeric1024().len());
145 assert_eq!(2048_usize, WasmiumRandom::secure_numeric2048().len());
146 }
147}