Bump argon2 #12
+26
-5
@@ -3,15 +3,34 @@ use argon2::{
|
|||||||
PasswordVerifier,
|
PasswordVerifier,
|
||||||
password_hash::{
|
password_hash::{
|
||||||
PasswordHasher,
|
PasswordHasher,
|
||||||
SaltString,
|
// SaltString,
|
||||||
rand_core::OsRng, // Secure random number generator
|
// rand_core::OsRng, // Secure random number generator
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub struct SaltString {
|
||||||
|
pub raw: String,
|
||||||
|
pub in_bytes: Vec<u8>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SaltString {
|
||||||
|
pub fn generate() -> Result<SaltString, argon2::Error> {
|
||||||
|
Err(argon2::Error::SaltTooLong)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_b64(s: &str) -> Result<SaltString, argon2::password_hash::Error> {
|
||||||
|
Err(argon2::password_hash::Error::SaltInvalid)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_string(&self) -> String {
|
||||||
|
self.raw.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn generate_salt() -> Result<SaltString, argon2::Error> {
|
pub fn generate_salt() -> Result<SaltString, argon2::Error> {
|
||||||
// Generate a random salt
|
// Generate a random salt
|
||||||
// SaltString::generate uses OsRng internally for cryptographic security
|
// SaltString::generate uses OsRng internally for cryptographic security
|
||||||
Ok(SaltString::generate(&mut OsRng))
|
SaltString::generate()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_salt(s: &str) -> Result<SaltString, argon2::password_hash::Error> {
|
pub fn get_salt(s: &str) -> Result<SaltString, argon2::password_hash::Error> {
|
||||||
@@ -31,7 +50,9 @@ pub fn hash_password(
|
|||||||
// Hash the password with the salt
|
// Hash the password with the salt
|
||||||
// The output is a PasswordHash string format that includes algorithm, version,
|
// The output is a PasswordHash string format that includes algorithm, version,
|
||||||
// parameters, salt, and the hash itself.
|
// parameters, salt, and the hash itself.
|
||||||
Ok(argon2.hash_password(password_bytes, salt)?.to_string())
|
// Ok(argon2.hash_password(password_bytes, salt)?.to_string())
|
||||||
|
// Ok(argon2.hash_password(password_bytes)?.to_string())
|
||||||
|
Ok(argon2.hash_password_with_salt(password_bytes, &salt.in_bytes)?.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn verify_password(
|
pub fn verify_password(
|
||||||
@@ -49,7 +70,7 @@ pub fn verify_password(
|
|||||||
// This automatically uses the correct salt and parameters embedded in `parsed_hash`
|
// This automatically uses the correct salt and parameters embedded in `parsed_hash`
|
||||||
match Argon2::default().verify_password(password_bytes, &parsed_hash) {
|
match Argon2::default().verify_password(password_bytes, &parsed_hash) {
|
||||||
Ok(()) => Ok(true), // Passwords match
|
Ok(()) => Ok(true), // Passwords match
|
||||||
Err(argon2::password_hash::Error::Password) => Ok(false), // Passwords don't match
|
Err(argon2::password_hash::Error::PasswordInvalid) => Ok(false), // Passwords don't match
|
||||||
Err(e) => Err(e), // Some other error occurred (e.g., invalid hash format)
|
Err(e) => Err(e), // Some other error occurred (e.g., invalid hash format)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user