Add generate_random_sequence() function
This commit is contained in:
parent
a9cb1c6a83
commit
f1972be8db
5 changed files with 30 additions and 11 deletions
|
@ -1,6 +1,6 @@
|
|||
use base64;
|
||||
use rand;
|
||||
use rand::prelude::*;
|
||||
|
||||
use crate::utils::random::generate_random_sequence;
|
||||
|
||||
pub fn render_authorization_page() -> String {
|
||||
let page = r#"<!DOCTYPE html>
|
||||
|
@ -41,8 +41,7 @@ pub fn render_authorization_page() -> String {
|
|||
const ACCESS_TOKEN_SIZE: usize = 20;
|
||||
|
||||
pub fn generate_access_token() -> String {
|
||||
let mut rng = rand::thread_rng();
|
||||
let value: [u8; ACCESS_TOKEN_SIZE] = rng.gen();
|
||||
let value: [u8; ACCESS_TOKEN_SIZE] = generate_random_sequence();
|
||||
base64::encode_config(value, base64::URL_SAFE_NO_PAD)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
use hex;
|
||||
use rand;
|
||||
use rand::prelude::*;
|
||||
|
||||
use crate::utils::random::generate_random_sequence;
|
||||
|
||||
const INVITE_CODE_LENGTH: usize = 32;
|
||||
|
||||
pub fn generate_invite_code() -> String {
|
||||
let mut rng = rand::thread_rng();
|
||||
let value: [u8; INVITE_CODE_LENGTH / 2] = rng.gen();
|
||||
let value: [u8; INVITE_CODE_LENGTH / 2] = generate_random_sequence();
|
||||
hex::encode(value)
|
||||
}
|
||||
|
||||
|
|
|
@ -9,4 +9,5 @@ pub mod id;
|
|||
pub mod markdown;
|
||||
pub mod multibase;
|
||||
pub mod passwords;
|
||||
pub mod random;
|
||||
pub mod urls;
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
use rand::Rng;
|
||||
use super::random::generate_random_sequence;
|
||||
|
||||
pub fn hash_password(password: &str) -> Result<String, argon2::Error> {
|
||||
let mut rng = rand::thread_rng();
|
||||
let salt: [u8; 32] = rng.gen();
|
||||
let salt: [u8; 32] = generate_random_sequence();
|
||||
let config = argon2::Config::default();
|
||||
|
||||
argon2::hash_encoded(password.as_bytes(), &salt, &config)
|
||||
|
@ -14,3 +13,16 @@ pub fn verify_password(
|
|||
) -> Result<bool, argon2::Error> {
|
||||
argon2::verify_encoded(password_hash, password.as_bytes())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_verify_password() {
|
||||
let password = "$test123";
|
||||
let password_hash = hash_password(password).unwrap();
|
||||
let result = verify_password(&password_hash, password);
|
||||
assert_eq!(result.is_ok(), true);
|
||||
}
|
||||
}
|
||||
|
|
8
src/utils/random.rs
Normal file
8
src/utils/random.rs
Normal file
|
@ -0,0 +1,8 @@
|
|||
use rand::Rng;
|
||||
|
||||
pub fn generate_random_sequence<const LEN: usize>() -> [u8; LEN] {
|
||||
let mut rng = rand::thread_rng();
|
||||
let mut value = [0u8; LEN];
|
||||
rng.fill(&mut value[..]);
|
||||
value
|
||||
}
|
Loading…
Reference in a new issue