Added some constants to reduce the amount of magic values

This commit is contained in:
StNicolay 2023-06-27 22:51:11 +03:00
parent bb89f6d349
commit c3af7e9a95
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D

View File

@ -5,12 +5,15 @@ use scrypt::{scrypt, Params};
use sea_orm::ActiveValue::Set;
use subtle::ConstantTimeEq;
static PARAMS: Lazy<Params> = Lazy::new(|| Params::new(14, 8, 1, 64).unwrap());
const HASH_LENGTH: usize = 64;
const SALT_LENGTH: usize = 64;
static PARAMS: Lazy<Params> = Lazy::new(|| Params::new(14, 8, 1, HASH_LENGTH).unwrap());
/// Hashes the password with Scrypt with the given salt
#[inline]
fn hash_password(password: &[u8], salt: &[u8]) -> [u8; 64] {
let mut password_hash = [0; 64];
fn hash_password(password: &[u8], salt: &[u8]) -> [u8; HASH_LENGTH] {
let mut password_hash = [0; HASH_LENGTH];
scrypt(password, salt, &PARAMS, &mut password_hash).unwrap();
password_hash
}
@ -36,13 +39,13 @@ impl MasterPassFromUnencryptedExt for master_pass::ActiveModel {
/// Hashes the password and creates an ActiveModel with all fields set to Set variant
#[inline]
fn from_unencrypted(user_id: u64, password: &str) -> Self {
let mut salt = vec![0; 64];
let mut salt = [0; SALT_LENGTH];
OsRng.fill_bytes(&mut salt);
let password_hash = Set(hash_password(password.as_bytes(), &salt).to_vec());
let password_hash = hash_password(password.as_bytes(), &salt);
Self {
user_id: Set(user_id),
salt: Set(salt),
password_hash,
salt: Set(salt.into()),
password_hash: Set(password_hash.into()),
}
}
}