Updated master_pass.rs to improve readablity

This commit is contained in:
StNicolay 2023-05-13 21:21:43 +03:00
parent 4d153d8c44
commit b65525cc6e
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D

View File

@ -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<Vec<u8>> {
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<Vec<u8>> {
}
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<Self> {
let mut salt = vec![0; 64];
OsRng.fill_bytes(&mut salt);
@ -48,15 +50,14 @@ impl Entity {
master_pass: String,
db: &DatabaseConnection,
) -> crate::Result<Option<bool>> {
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