From cf28aba2313b281b2762e68b8531534544fd1b0e Mon Sep 17 00:00:00 2001 From: StNicolay Date: Fri, 26 May 2023 14:53:03 +0300 Subject: [PATCH] Now hash password returns an array instead of a vector to remove an allocation during a master password validation --- src/entity/master_pass.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/entity/master_pass.rs b/src/entity/master_pass.rs index e08c826..7f9328c 100644 --- a/src/entity/master_pass.rs +++ b/src/entity/master_pass.rs @@ -20,9 +20,9 @@ impl ActiveModelBehavior for ActiveModel {} /// Hashes the password with Scrypt with the given salt #[inline] -fn hash_password(password: &[u8], salt: &[u8]) -> crate::Result> { +fn hash_password(password: &[u8], salt: &[u8]) -> crate::Result<[u8; 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, ¶ms, &mut password_hash)?; Ok(password_hash) } @@ -31,7 +31,7 @@ impl Model { /// Checks that the given password hash matches the one of the model pub fn verify(&self, password: &str) -> crate::Result { 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 { let mut salt = vec![0; 64]; 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 { user_id: Set(user_id), salt: Set(salt),