Store hashed passwords (#7)
Use if let instead of unwrap Default to invalid password if could not calculate Move hash password methdo and return Result Rename get_password method Default to empty password when no pwd is received Store hashed passwords Store passwords hashed with Argon2 and verify password with that stored hash. Co-authored-by: Guillem Nieto <gnieto.talo@gmail.com>
This commit is contained in:
parent
abcce95dd8
commit
fa9e127a1e
6 changed files with 148 additions and 21 deletions
16
src/utils.rs
16
src/utils.rs
|
@ -3,6 +3,7 @@ use std::{
|
|||
convert::TryInto,
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
use argon2::{Config, Variant};
|
||||
|
||||
pub fn millis_since_unix_epoch() -> u64 {
|
||||
SystemTime::now()
|
||||
|
@ -39,3 +40,18 @@ pub fn random_string(length: usize) -> String {
|
|||
.take(length)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Calculate a new hash for the given password
|
||||
pub fn calculate_hash(password: &str) -> Result<String, argon2::Error> {
|
||||
let hashing_config = Config {
|
||||
variant: Variant::Argon2id,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let salt = random_string(32);
|
||||
argon2::hash_encoded(
|
||||
password.as_bytes(),
|
||||
salt.as_bytes(),
|
||||
&hashing_config,
|
||||
)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue