forked from loco-rs/loco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.rs
73 lines (64 loc) · 1.66 KB
/
hash.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use argon2::{
password_hash::SaltString, Argon2, Params, PasswordHash, PasswordHasher, PasswordVerifier,
Version,
};
use crate::{Error, Result};
/// Hashes a plain text password and returns the hashed result.
///
/// # Errors
///
/// Return [`argon2::password_hash::Result`] when could not hash the given
/// password.
///
/// # Example
/// ```rust
/// use loco_rs::hash;
///
/// hash::hash_password("password-to-hash");
/// ```
pub fn hash_password(pass: &str) -> Result<String> {
let arg2 = Argon2::new(
argon2::Algorithm::Argon2id,
argon2::Version::V0x13,
Params::default(),
);
let salt = SaltString::generate(&mut rand::rngs::OsRng);
Ok(arg2
.hash_password(pass.as_bytes(), &salt)
.map_err(|err| Error::Hash(err.to_string()))?
.to_string())
}
/// Verifies a plain text password against a hashed password.
///
/// # Errors
///
/// Return [`argon2::password_hash::Result`] when could verify the given data.
///
/// # Example
/// ```rust
/// use loco_rs::hash;
///
/// hash::verify_password("password", "hashed-password");
/// ```
#[must_use]
pub fn verify_password(pass: &str, hashed_password: &str) -> bool {
let arg2 = Argon2::new(
argon2::Algorithm::Argon2id,
Version::V0x13,
Params::default(),
);
let Ok(hash) = PasswordHash::new(hashed_password) else {
return false;
};
arg2.verify_password(pass.as_bytes(), &hash).is_ok()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn can_hah_password() {
let pass = "password-1234";
let hash_pass = hash_password(pass).unwrap();
assert!(verify_password(pass, &hash_pass));
}
}