More clippy fixes

This commit is contained in:
2023-11-19 14:45:46 +03:00
parent 6ae745fcd4
commit bfd68194e6
17 changed files with 79 additions and 51 deletions

View File

@ -5,6 +5,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lints]
workspace = true
[dependencies]
sha2 = "0.10"
scrypt = { version = "0.11", default-features = false, features = ["std"] }

View File

@ -12,8 +12,9 @@ pub struct Cipher {
impl Cipher {
/// Creates a new cipher from a master password and the salt
#[inline]
#[must_use]
pub fn new(password: &[u8], salt: &[u8]) -> Self {
let key = pbkdf2_hmac_array::<Sha256, 32>(password, salt, 480000);
let key = pbkdf2_hmac_array::<Sha256, 32>(password, salt, 480_000);
Self {
chacha: ChaCha20Poly1305::new(&key.into()),
@ -33,11 +34,12 @@ impl Cipher {
#[inline]
pub fn decrypt(&self, value: &[u8]) -> crate::Result<Vec<u8>> {
let (data, nonce) = value.split_at(value.len() - 12);
self.chacha.decrypt(nonce.into(), data).map_err(Into::into)
}
}
pub trait AccountFromUnencryptedExt {
pub trait FromUnencryptedExt {
fn from_unencrypted(
user_id: u64,
name: String,
@ -47,8 +49,8 @@ pub trait AccountFromUnencryptedExt {
) -> crate::Result<account::ActiveModel>;
}
impl AccountFromUnencryptedExt for account::ActiveModel {
/// Encryptes the provided data by the master password and creates the ActiveModel with all fields set to Set variant
impl FromUnencryptedExt for account::ActiveModel {
/// Encryptes the provided data by the master password and creates the `ActiveModel` with all fields set to Set variant
#[inline]
fn from_unencrypted(
user_id: u64,

View File

@ -11,6 +11,8 @@ static PARAMS: Lazy<Params> = Lazy::new(|| Params::new(14, 8, 1, HASH_LENGTH).un
/// Hashes the bytes with Scrypt with the given salt
#[inline]
#[must_use]
#[allow(clippy::missing_panics_doc)]
pub fn hash_scrypt(bytes: &[u8], salt: &[u8]) -> [u8; HASH_LENGTH] {
let mut hash = [0; HASH_LENGTH];
scrypt(bytes, salt, &PARAMS, &mut hash).unwrap();
@ -29,6 +31,7 @@ where
impl HashedBytes<[u8; HASH_LENGTH], [u8; SALT_LENGTH]> {
#[inline]
#[must_use]
pub fn new(bytes: &[u8]) -> Self {
let mut salt = [0; 64];
OsRng.fill_bytes(&mut salt);
@ -45,6 +48,7 @@ where
U: AsRef<[u8]>,
{
#[inline]
#[must_use]
pub fn verify(&self, bytes: &[u8]) -> bool {
let hash = hash_scrypt(bytes, self.salt.as_ref());
hash.ct_eq(self.hash.as_ref()).into()

View File

@ -1,4 +1,5 @@
//! Functions to encrypt the database models
#![allow(clippy::missing_errors_doc)]
pub mod account;
pub mod hashing;

View File

@ -2,12 +2,12 @@ use super::hashing::HashedBytes;
use entity::master_pass;
use sea_orm::ActiveValue::Set;
pub trait MasterPassFromUnencryptedExt {
pub trait FromUnencryptedExt {
fn from_unencrypted(user_id: u64, password: &str) -> master_pass::ActiveModel;
}
impl MasterPassFromUnencryptedExt for master_pass::ActiveModel {
/// Hashes the password and creates an ActiveModel with all fields set to Set variant
impl FromUnencryptedExt 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 hash = HashedBytes::new(password.as_bytes());

View File

@ -25,6 +25,7 @@ bitflags::bitflags! {
/// Returns true if the generated master password is valid.
/// It checks that it has at least one lowercase, one uppercase, one number and one punctuation char
#[inline]
#[must_use]
fn check_generated_password<const LENGTH: usize>(password: &[u8; LENGTH]) -> bool {
let mut flags = PasswordFlags::empty();
for &byte in password {
@ -33,7 +34,7 @@ fn check_generated_password<const LENGTH: usize>(password: &[u8; LENGTH]) -> boo
b'A'..=b'Z' => flags |= PasswordFlags::UPPERCASE,
b'0'..=b'9' => flags |= PasswordFlags::NUMBER,
b'!'..=b'/' | b':'..=b'@' | b'['..=b'`' | b'{'..=b'~' => {
flags |= PasswordFlags::SPECIAL_CHARACTER
flags |= PasswordFlags::SPECIAL_CHARACTER;
}
_ => (),
}
@ -46,6 +47,7 @@ fn check_generated_password<const LENGTH: usize>(password: &[u8; LENGTH]) -> boo
/// Continuously generates the password until it passes the checks
#[inline]
#[must_use]
fn generate_password<R, const LENGTH: usize>(rng: &mut R) -> ArrayString<LENGTH>
where
R: Rng + CryptoRng,
@ -59,6 +61,8 @@ where
}
#[inline]
#[must_use]
#[allow(clippy::module_name_repetitions)]
pub fn generate_passwords<const AMOUNT: usize, const LENGTH: usize>(
) -> [ArrayString<LENGTH>; AMOUNT] {
let mut rng = thread_rng();
@ -66,6 +70,7 @@ pub fn generate_passwords<const AMOUNT: usize, const LENGTH: usize>(
}
#[inline]
#[must_use]
pub fn check_master_pass(password: &str) -> PasswordValidity {
let mut count = 0;
let mut chars = password.chars();
@ -74,13 +79,13 @@ pub fn check_master_pass(password: &str) -> PasswordValidity {
for char in &mut chars {
count += 1;
if char.is_lowercase() {
flags.remove(PasswordValidity::NO_LOWERCASE)
flags.remove(PasswordValidity::NO_LOWERCASE);
} else if char.is_uppercase() {
flags.remove(PasswordValidity::NO_UPPERCASE)
flags.remove(PasswordValidity::NO_UPPERCASE);
} else if char.is_ascii_digit() {
flags.remove(PasswordValidity::NO_NUMBER)
flags.remove(PasswordValidity::NO_NUMBER);
} else if char.is_ascii_punctuation() {
flags.remove(PasswordValidity::NO_SPECIAL_CHARACTER)
flags.remove(PasswordValidity::NO_SPECIAL_CHARACTER);
}
if flags == PasswordValidity::TOO_SHORT {
@ -90,7 +95,7 @@ pub fn check_master_pass(password: &str) -> PasswordValidity {
}
if count >= 8 {
flags.remove(PasswordValidity::TOO_SHORT)
flags.remove(PasswordValidity::TOO_SHORT);
}
flags
@ -102,6 +107,6 @@ mod tests {
#[test]
fn chars_must_be_ascii() {
assert!(CHARS.is_ascii())
assert!(CHARS.is_ascii());
}
}

View File

@ -1,2 +1,2 @@
pub use crate::account::*;
pub use crate::master_pass::*;
pub use crate::account::{DecryptAccountExt as _, FromUnencryptedExt as _};
pub use crate::master_pass::FromUnencryptedExt as _;