aboutsummaryrefslogtreecommitdiff
path: root/src/crypto.rs
diff options
context:
space:
mode:
authorJeremy Lin <[email protected]>2020-08-22 16:07:53 -0700
committerJeremy Lin <[email protected]>2020-08-22 16:07:53 -0700
commitd9684bef6be88517621cf50f0f81d5709ea199d2 (patch)
treee14d0a06cc7c9890fd0709c035bbd1e3743f79a5 /src/crypto.rs
parenteba22c2d9429b9f9bad5baff9b1d571b469645c9 (diff)
downloadvaultwarden-d9684bef6be88517621cf50f0f81d5709ea199d2.tar.gz
vaultwarden-d9684bef6be88517621cf50f0f81d5709ea199d2.zip
Generate tokens more simply and uniformly
Diffstat (limited to 'src/crypto.rs')
-rw-r--r--src/crypto.rs16
1 files changed, 10 insertions, 6 deletions
diff --git a/src/crypto.rs b/src/crypto.rs
index c2da8fbd..7d0440c7 100644
--- a/src/crypto.rs
+++ b/src/crypto.rs
@@ -55,17 +55,21 @@ pub fn get_random(mut array: Vec<u8>) -> Vec<u8> {
}
pub fn generate_token(token_size: u32) -> Result<String, Error> {
+ // A u64 can represent all whole numbers up to 19 digits long.
if token_size > 19 {
- err!("Generating token failed")
+ err!("Token size is limited to 19 digits")
}
- // 8 bytes to create an u64 for up to 19 token digits
- let bytes = get_random(vec![0; 8]);
- let mut bytes_array = [0u8; 8];
- bytes_array.copy_from_slice(&bytes);
+ let low: u64 = 0;
+ let high: u64 = 10u64.pow(token_size);
- let number = u64::from_be_bytes(bytes_array) % 10u64.pow(token_size);
+ // Generate a random number in the range [low, high), then format it as a
+ // token of fixed width, left-padding with 0 as needed.
+ use rand::{thread_rng, Rng};
+ let mut rng = thread_rng();
+ let number: u64 = rng.gen_range(low, high);
let token = format!("{:0size$}", number, size = token_size as usize);
+
Ok(token)
}