Skip to content

Commit df9c40b

Browse files
committed
update: remove rnd from security-iv-printable-prefix feature
1 parent 4605783 commit df9c40b

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

crates/shadowsocks/src/context.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,11 @@ impl Context {
7373
{
7474
const SECURITY_PRINTABLE_PREFIX_LEN: usize = 6;
7575
if nonce.len() >= SECURITY_PRINTABLE_PREFIX_LEN {
76-
use rand::Rng;
76+
// Printable characters use base64 letters instead
77+
static ASCII_PRINTABLE_CHARS: &[u8] = br##"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"##;
7778

78-
// Printable characters follows definition of isprint in C/C++
79-
static ASCII_PRINTABLE_CHARS: &[u8] = br##"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ "##;
80-
81-
let mut rng = rand::thread_rng();
8279
for b in nonce.iter_mut().take(SECURITY_PRINTABLE_PREFIX_LEN) {
83-
*b = ASCII_PRINTABLE_CHARS[rng.gen_range::<usize, _>(0..ASCII_PRINTABLE_CHARS.len())];
80+
*b = ASCII_PRINTABLE_CHARS[(*b as usize) % ASCII_PRINTABLE_CHARS.len()];
8481
}
8582
}
8683
}
@@ -166,3 +163,19 @@ impl Context {
166163
self.replay_policy
167164
}
168165
}
166+
167+
#[cfg(test)]
168+
mod tests {
169+
use crate::config::ServerType;
170+
use crate::context::Context;
171+
use byte_string::ByteStr;
172+
173+
#[test]
174+
fn generate_nonce() {
175+
let mut salt = vec![0u8; 64];
176+
let context = Context::new(ServerType::Server);
177+
context.generate_nonce(&mut salt, false);
178+
println!("generate nonce printable ascii: {:?}", ByteStr::new(&salt));
179+
}
180+
181+
}

0 commit comments

Comments
 (0)