Updated master_pass.rs to improve readablity
This commit is contained in:
parent
4d153d8c44
commit
b65525cc6e
@ -21,6 +21,7 @@ pub enum Relation {}
|
|||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
|
|
||||||
|
/// Hashes the password with Scrypt with the given salt
|
||||||
fn hash_password(password: &[u8], salt: &[u8]) -> crate::Result<Vec<u8>> {
|
fn hash_password(password: &[u8], salt: &[u8]) -> crate::Result<Vec<u8>> {
|
||||||
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 = vec![0; 64];
|
||||||
@ -29,6 +30,7 @@ fn hash_password(password: &[u8], salt: &[u8]) -> crate::Result<Vec<u8>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ActiveModel {
|
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> {
|
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);
|
||||||
@ -48,15 +50,14 @@ impl Entity {
|
|||||||
master_pass: String,
|
master_pass: String,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> crate::Result<Option<bool>> {
|
) -> crate::Result<Option<bool>> {
|
||||||
let model = match Self::find_by_id(user_id).one(db).await {
|
let model = match Self::find_by_id(user_id).one(db).await? {
|
||||||
Ok(Some(model)) => model,
|
Some(model) => model,
|
||||||
Ok(None) => return Ok(None),
|
None => return Ok(None),
|
||||||
Err(err) => return Err(err.into()),
|
|
||||||
};
|
};
|
||||||
let (hash, salt) = (model.password_hash, model.salt);
|
let salt = model.salt;
|
||||||
let password_hash =
|
let password_hash =
|
||||||
spawn_blocking(move || hash_password(master_pass.as_bytes(), &salt)).await??;
|
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
|
/// Checks if the master password for the user exists
|
||||||
|
Loading…
x
Reference in New Issue
Block a user