Skip to content

Commit 2f4ee5f

Browse files
authored
scram: generate random nonce with one call (#182)
This has two benefits: 1. takes 1 lock instead of 18 2. generates multiple bytes per internal random invocation
1 parent 9bf611e commit 2f4ee5f

File tree

2 files changed

+2
-11
lines changed

2 files changed

+2
-11
lines changed

examples/scram.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ impl SimpleQueryHandler for DummyProcessor {
3636
}
3737

3838
pub fn random_salt() -> Vec<u8> {
39-
let mut buf = vec![0u8; 10];
40-
for v in buf.iter_mut() {
41-
*v = rand::random::<u8>();
42-
}
43-
buf
39+
Vec::from(rand::random::<[u8; 10]>())
4440
}
4541

4642
const ITERATIONS: usize = 4096;

src/api/auth/scram.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,7 @@ pub fn gen_salted_password(password: &str, salt: &[u8], iters: usize) -> Vec<u8>
6363
}
6464

6565
pub fn random_nonce() -> String {
66-
let mut buf = [0u8; 18];
67-
for v in buf.iter_mut() {
68-
*v = rand::random::<u8>();
69-
}
70-
71-
STANDARD.encode(buf)
66+
STANDARD.encode(rand::random::<[u8; 18]>())
7267
}
7368

7469
impl<A, P> SASLScramAuthStartupHandler<A, P> {

0 commit comments

Comments
 (0)