1pub struct Rng;
2
3impl Rng {
4 pub fn generate_key(length: usize) -> String {
5 let mut rng = Rng {};
6 let ascii = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
7
8 (0..length)
9 .map(|_| {
10 let idx = rand_core::RngCore::next_u64(&mut rng) as usize % ascii.len();
11 ascii.chars().nth(idx).unwrap()
12 })
13 .collect()
14 }
15}
16
17impl rand_core::RngCore for Rng {
18 fn next_u32(&mut self) -> u32 {
19 self.next_u64() as u32
20 }
21
22 fn next_u64(&mut self) -> u64 {
23 let mut w = [0u8; 8];
24 fill_8_bytes(&mut w);
25 u64::from_be_bytes(w)
26 }
27
28 fn fill_bytes(&mut self, dest: &mut [u8]) {
29 for chunk in dest.chunks_mut(8) {
30 let mut w = [0u8; 8];
31 fill_8_bytes(&mut w);
32 chunk.copy_from_slice(&w);
33 }
34 }
35
36 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
37 self.fill_bytes(dest);
38 Ok(())
39 }
40}
41
42fn fill_8_bytes(w: &mut [u8; 8]) {
43 let val = ft_sdk::env::random().to_be_bytes();
44
45 for (i, byte) in val.iter().enumerate() {
46 w[i] = *byte;
47 }
48}
49
50impl rand_core::CryptoRng for Rng {}