From b65525cc6ebfbc380d4e52ee2a0971ec11eb0c3f Mon Sep 17 00:00:00 2001 From: StNicolay Date: Sat, 13 May 2023 21:21:43 +0300 Subject: [PATCH] Updated master_pass.rs to improve readablity --- src/entity/master_pass.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/entity/master_pass.rs b/src/entity/master_pass.rs index e6a7253..0609657 100644 --- a/src/entity/master_pass.rs +++ b/src/entity/master_pass.rs @@ -21,6 +21,7 @@ pub enum Relation {} impl ActiveModelBehavior for ActiveModel {} +/// Hashes the password with Scrypt with the given salt fn hash_password(password: &[u8], salt: &[u8]) -> crate::Result> { let params = Params::new(14, Params::RECOMMENDED_R, Params::RECOMMENDED_P, 64)?; let mut password_hash = vec![0; 64]; @@ -29,6 +30,7 @@ fn hash_password(password: &[u8], salt: &[u8]) -> crate::Result> { } impl ActiveModel { + /// Hashes the password and creates an ActiveModel with all fields set to Set variant pub fn from_unencrypted(user_id: u64, password: &str) -> crate::Result { let mut salt = vec![0; 64]; OsRng.fill_bytes(&mut salt); @@ -48,15 +50,14 @@ impl Entity { master_pass: String, db: &DatabaseConnection, ) -> crate::Result> { - let model = match Self::find_by_id(user_id).one(db).await { - Ok(Some(model)) => model, - Ok(None) => return Ok(None), - Err(err) => return Err(err.into()), + let model = match Self::find_by_id(user_id).one(db).await? { + Some(model) => model, + None => return Ok(None), }; - let (hash, salt) = (model.password_hash, model.salt); + let salt = model.salt; let password_hash = spawn_blocking(move || hash_password(master_pass.as_bytes(), &salt)).await??; - Ok(Some(password_hash == hash)) + Ok(Some(password_hash == model.password_hash)) } /// Checks if the master password for the user exists