shadowsocks_crypto/utils.rs
1//! Common utilities
2
3/// Generate random bytes into `iv_or_salt`
4pub fn random_iv_or_salt(iv_or_salt: &mut [u8]) {
5 use rand::Rng;
6
7 // Gen IV or Gen Salt by KEY-LEN
8 if iv_or_salt.is_empty() {
9 return;
10 }
11
12 let mut rng = rand::rng();
13 loop {
14 rng.fill(iv_or_salt);
15
16 // https://stackoverflow.com/questions/65367552/checking-a-vecu8-to-see-if-its-all-zero
17 let (prefix, aligned, suffix) = unsafe { iv_or_salt.align_to::<u128>() };
18 let is_zeros =
19 prefix.iter().all(|&x| x == 0) && aligned.iter().all(|&x| x == 0) && suffix.iter().all(|&x| x == 0);
20
21 if !is_zeros {
22 break;
23 }
24 }
25}