Now hash password returns an array instead of a vector to remove an allocation during a master password validation

This commit is contained in:
StNicolay 2023-05-26 14:53:03 +03:00
parent e39762916d
commit cf28aba231
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D

View File

@ -20,9 +20,9 @@ impl ActiveModelBehavior for ActiveModel {}
/// Hashes the password with Scrypt with the given salt /// Hashes the password with Scrypt with the given salt
#[inline] #[inline]
fn hash_password(password: &[u8], salt: &[u8]) -> crate::Result<Vec<u8>> { fn hash_password(password: &[u8], salt: &[u8]) -> crate::Result<[u8; 64]> {
let params = Params::new(14, Params::RECOMMENDED_R, Params::RECOMMENDED_P, 64)?; let params = Params::new(14, Params::RECOMMENDED_R, Params::RECOMMENDED_P, 64)?;
let mut password_hash = vec![0; 64]; let mut password_hash = [0; 64];
scrypt(password, salt, &params, &mut password_hash)?; scrypt(password, salt, &params, &mut password_hash)?;
Ok(password_hash) Ok(password_hash)
} }
@ -31,7 +31,7 @@ impl Model {
/// Checks that the given password hash matches the one of the model /// Checks that the given password hash matches the one of the model
pub fn verify(&self, password: &str) -> crate::Result<bool> { pub fn verify(&self, password: &str) -> crate::Result<bool> {
let hashed = hash_password(password.as_bytes(), &self.salt)?; let hashed = hash_password(password.as_bytes(), &self.salt)?;
Ok(hashed == self.password_hash) Ok(hashed == self.password_hash.as_slice())
} }
} }
@ -41,7 +41,7 @@ impl ActiveModel {
pub fn from_unencrypted(user_id: u64, password: &str) -> crate::Result<Self> { pub fn from_unencrypted(user_id: u64, password: &str) -> crate::Result<Self> {
let mut salt = vec![0; 64]; let mut salt = vec![0; 64];
OsRng.fill_bytes(&mut salt); OsRng.fill_bytes(&mut salt);
let password_hash = Set(hash_password(password.as_ref(), &salt)?); let password_hash = Set(hash_password(password.as_bytes(), &salt)?.to_vec());
Ok(Self { Ok(Self {
user_id: Set(user_id), user_id: Set(user_id),
salt: Set(salt), salt: Set(salt),