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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use std::{env, io};
use lazy_static::lazy_static;
use rand::Rng;
use ring::rand::{SecureRandom, SystemRandom};
pub fn string(n: usize) -> String {
let bytes = bytes(n).unwrap();
let mut d = bs58::encode(&bytes[..]).into_string();
if n > 0 && d.len() > n {
d.truncate(n);
}
d
}
pub fn bytes(n: usize) -> Result<Vec<u8>, String> {
let mut d: Vec<u8> = vec![0u8; n];
secure_random().fill(&mut d).map_err(|e| e.to_string())?;
Ok(d)
}
fn secure_random() -> &'static dyn SecureRandom {
use std::ops::Deref;
lazy_static! {
static ref RANDOM: SystemRandom = SystemRandom::new();
}
RANDOM.deref()
}
#[test]
fn test_string() {
use log::info;
let _ = env_logger::builder()
.filter_level(log::LevelFilter::Info)
.is_test(true)
.try_init();
let word1 = string(100);
let word2 = string(100);
assert_eq!(word1.len(), 100);
assert_eq!(word2.len(), 100);
assert_ne!(word1, word2);
info!("word1: {:?}", word1);
info!("word2: {:?}", word2);
}
pub fn tmp_path(n: usize, sfx: Option<&str>) -> io::Result<String> {
let tmp_dir = env::temp_dir();
let tmp_file_path = tmp_dir.join(format!("{}{}", string(n), sfx.unwrap_or("")));
let tmp_file_path = tmp_file_path.as_os_str().to_str().unwrap();
Ok(String::from(tmp_file_path))
}
#[test]
fn test_temp_path() {
let _ = env_logger::builder()
.filter_level(log::LevelFilter::Info)
.is_test(true)
.try_init();
use log::info;
let p1 = tmp_path(10, Some(".zstd")).unwrap();
let p2 = tmp_path(10, Some(".zstd")).unwrap();
assert_ne!(p1, p2);
info!("p1: {:?}", p1);
info!("p2: {:?}", p2);
}
pub fn secure_u8() -> Result<u8, String> {
let mut d: Vec<u8> = vec![0u8; 1];
secure_random().fill(&mut d).map_err(|e| e.to_string())?;
Ok(d[0])
}
pub fn u8() -> u8 {
let mut rng = rand::thread_rng();
rng.gen()
}
pub fn u16() -> u16 {
let mut rng = rand::thread_rng();
rng.gen()
}
pub fn u32() -> u32 {
let mut rng = rand::thread_rng();
rng.gen()
}
pub fn u64() -> u64 {
let mut rng = rand::thread_rng();
rng.gen()
}