1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use rand::{thread_rng, Rng};
use sha2::Digest;
pub fn generate_random_alphanumeric(length: usize) -> String {
thread_rng()
.sample_iter(rand::distributions::Alphanumeric)
.take(length)
.map(char::from)
.collect()
}
pub fn sha_256_hash(input: &str) -> String {
let mut hasher = sha2::Sha256::new();
hasher.update(input);
format!("{:x}", hasher.finalize())
}
#[cfg(test)]
mod test {
use std::collections::HashSet;
use crate::generate_random_alphanumeric;
#[test]
fn test_generate_random_alphanumeric() {
let sample_size = 1000;
let mut set: HashSet<String> = HashSet::default();
for _ in 0..sample_size {
let s = generate_random_alphanumeric(32);
if set.contains(&s) {
panic!("Duplicate key found in set");
}
set.insert(s);
}
}
}