From 48dec4d4a0d5a99afe279d20be379f99a2b9ac37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sun, 10 Dec 2023 15:09:37 +0100 Subject: [PATCH] Use Self --- src/key.rs | 4 ++-- src/oath.rs | 18 +++++++++--------- src/oath/hotp.rs | 12 ++++++------ src/oath/totp.rs | 18 +++++++++--------- src/pass/hash_builder.rs | 38 +++++++++++++++++++------------------- src/pass/hasher/argon2.rs | 4 ++-- src/pass/hasher/pbkdf2.rs | 4 ++-- 7 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/key.rs b/src/key.rs index 4634425..5c73bb8 100644 --- a/src/key.rs +++ b/src/key.rs @@ -50,8 +50,8 @@ pub struct KeyBuilder { impl KeyBuilder { /// Create a new random key builder. - pub fn new() -> KeyBuilder { - KeyBuilder { + pub fn new() -> Self { + Self { size: 21, key: None, } diff --git a/src/oath.rs b/src/oath.rs index 9bcc01b..dd316ae 100644 --- a/src/oath.rs +++ b/src/oath.rs @@ -147,21 +147,21 @@ impl From for ErrorCode { } macro_rules! builder_common { - ($t:ty) => { + () => { /// Sets the shared secret. - pub fn key(&mut self, key: &[u8]) -> &mut $t { + pub fn key(&mut self, key: &[u8]) -> &mut Self { self.key = Some(key.to_owned()); self } /// Sets the shared secret. This secret is passed as an ASCII string. - pub fn ascii_key(&mut self, key: &str) -> &mut $t { + pub fn ascii_key(&mut self, key: &str) -> &mut Self { self.key = Some(key.as_bytes().to_vec()); self } /// Sets the shared secret. This secret is passed as an hexadecimal encoded string. - pub fn hex_key(&mut self, key: &str) -> &mut $t { + pub fn hex_key(&mut self, key: &str) -> &mut Self { match hex::decode(key) { Ok(k) => { self.key = Some(k); @@ -174,7 +174,7 @@ macro_rules! builder_common { } /// Sets the shared secret. This secret is passed as a base32 encoded string. - pub fn base32_key(&mut self, key: &str) -> &mut $t { + pub fn base32_key(&mut self, key: &str) -> &mut Self { match base32::decode(base32::Alphabet::RFC4648 { padding: false }, &key) { Some(k) => { self.key = Some(k); @@ -187,7 +187,7 @@ macro_rules! builder_common { } /// Sets the shared secret. This secret is passed as a base64 encoded string. - pub fn base64_key(&mut self, key: &str) -> &mut $t { + pub fn base64_key(&mut self, key: &str) -> &mut Self { use base64::Engine; match base64::engine::general_purpose::STANDARD.decode(key) { Ok(k) => { @@ -213,19 +213,19 @@ macro_rules! builder_common { } /// Sets the number of characters for the code. The minimum and maximum values depends the base. Default is 6. - pub fn output_len(&mut self, output_len: usize) -> &mut $t { + pub fn output_len(&mut self, output_len: usize) -> &mut Self { self.output_len = output_len; self } /// Sets the base used to represents the output code. Default is "0123456789". - pub fn output_base(&mut self, base: &str) -> &mut $t { + pub fn output_base(&mut self, base: &str) -> &mut Self { self.output_base = base.to_string(); self } /// Sets the hash function. Default is Sha1. - pub fn hash_function(&mut self, hash_function: HashFunction) -> &mut $t { + pub fn hash_function(&mut self, hash_function: HashFunction) -> &mut Self { self.hash_function = hash_function; self } diff --git a/src/oath/hotp.rs b/src/oath/hotp.rs index 8b9e414..b178da7 100644 --- a/src/oath/hotp.rs +++ b/src/oath/hotp.rs @@ -122,7 +122,7 @@ impl HOTP { } /// Increments the internal counter. - pub fn increment_counter(&mut self) -> &mut HOTP { + pub fn increment_counter(&mut self) -> &mut Self { self.counter += 1; self } @@ -389,8 +389,8 @@ impl Default for HOTPBuilder { impl HOTPBuilder { /// Generates the base configuration for HOTP code generation. - pub fn new() -> HOTPBuilder { - HOTPBuilder { + pub fn new() -> Self { + Self { key: None, counter: 0, output_len: DEFAULT_OTP_OUT_LEN, @@ -401,16 +401,16 @@ impl HOTPBuilder { } } - builder_common!(HOTPBuilder); + builder_common!(); /// Sets the counter. Default is 0. - pub fn counter(&mut self, counter: u64) -> &mut HOTPBuilder { + pub fn counter(&mut self, counter: u64) -> &mut Self { self.counter = counter; self } /// Sets a look-ahead parameter. Default is 0. - pub fn look_ahead(&mut self, nb: u64) -> &mut HOTPBuilder { + pub fn look_ahead(&mut self, nb: u64) -> &mut Self { self.look_ahead = nb; self } diff --git a/src/oath/totp.rs b/src/oath/totp.rs index 306a8de..9ac2b22 100644 --- a/src/oath/totp.rs +++ b/src/oath/totp.rs @@ -217,8 +217,8 @@ impl Default for TOTPBuilder { impl TOTPBuilder { /// Generates the base configuration for TOTP code generation. - pub fn new() -> TOTPBuilder { - TOTPBuilder { + pub fn new() -> Self { + Self { key: None, timestamp_offset: 0, positive_tolerance: 0, @@ -232,10 +232,10 @@ impl TOTPBuilder { } } - builder_common!(TOTPBuilder); + builder_common!(); /// Sets a custom value for the current Unix time instead of the real one. - pub fn timestamp(&mut self, timestamp: i64) -> &mut TOTPBuilder { + pub fn timestamp(&mut self, timestamp: i64) -> &mut Self { let current_timestamp = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .unwrap() @@ -246,7 +246,7 @@ impl TOTPBuilder { /// Sets the number of periods ahead or behind the current one for which the user code will /// still be considered valid. You should not set a value higher than 2. Default is 0. - pub fn tolerance(&mut self, tolerance: u64) -> &mut TOTPBuilder { + pub fn tolerance(&mut self, tolerance: u64) -> &mut Self { self.positive_tolerance = tolerance; self.negative_tolerance = tolerance; self @@ -254,20 +254,20 @@ impl TOTPBuilder { /// Sets the number of periods ahead the current one for which the user code will /// still be considered valid. You should not set a value higher than 2. Default is 0. - pub fn positive_tolerance(&mut self, tolerance: u64) -> &mut TOTPBuilder { + pub fn positive_tolerance(&mut self, tolerance: u64) -> &mut Self { self.positive_tolerance = tolerance; self } /// Sets the number of periods behind the current one for which the user code will /// still be considered valid. You should not set a value higher than 2. Default is 0. - pub fn negative_tolerance(&mut self, tolerance: u64) -> &mut TOTPBuilder { + pub fn negative_tolerance(&mut self, tolerance: u64) -> &mut Self { self.negative_tolerance = tolerance; self } /// Sets the time step in seconds (X). May not be zero. Default is 30. - pub fn period(&mut self, period: u32) -> &mut TOTPBuilder { + pub fn period(&mut self, period: u32) -> &mut Self { if period == 0 { self.runtime_error = Some(Error::InvalidPeriod); } else { @@ -277,7 +277,7 @@ impl TOTPBuilder { } /// Sets the Unix time to start counting time steps (T0). Default is 0. - pub fn initial_time(&mut self, initial_time: u64) -> &mut TOTPBuilder { + pub fn initial_time(&mut self, initial_time: u64) -> &mut Self { self.initial_time = initial_time; self } diff --git a/src/pass/hash_builder.rs b/src/pass/hash_builder.rs index 9ba1f31..30b33f8 100644 --- a/src/pass/hash_builder.rs +++ b/src/pass/hash_builder.rs @@ -87,14 +87,14 @@ impl Default for HashBuilder { impl HashBuilder { /// Create a new HashBuilder object with default parameters. - pub fn new() -> HashBuilder { - HashBuilder::new_std(PasswordStorageStandard::NoStandard) + pub fn new() -> Self { + Self::new_std(PasswordStorageStandard::NoStandard) } /// Create a new HashBuilder object with default parameters for a specific standard. - pub fn new_std(std: PasswordStorageStandard) -> HashBuilder { + pub fn new_std(std: PasswordStorageStandard) -> Self { match std { - PasswordStorageStandard::NoStandard => HashBuilder { + PasswordStorageStandard::NoStandard => Self { standard: PasswordStorageStandard::NoStandard, normalization: std_default::DEFAULT_NORMALIZATION, min_len: std_default::DEFAULT_PASSWORD_MIN_LEN, @@ -109,7 +109,7 @@ impl HashBuilder { xhmac: XHMAC::None, xhmax_alg: std_default::DEFAULT_XHMAC_ALGORITHM, }, - PasswordStorageStandard::Nist80063b => HashBuilder { + PasswordStorageStandard::Nist80063b => Self { standard: PasswordStorageStandard::Nist80063b, normalization: std_nist::DEFAULT_NORMALIZATION, min_len: std_nist::DEFAULT_PASSWORD_MIN_LEN, @@ -129,12 +129,12 @@ impl HashBuilder { /// Create a new Hasher object from a PHC formatted string. pub fn from_phc(data: &str) -> Result { - HashBuilder::from_phc_internal(data, None) + Self::from_phc_internal(data, None) } /// Create a new Hasher object from a PHC formatted string and an external pepper for an additional HMAC. pub fn from_phc_xhmac(data: &str, pepper: &[u8]) -> Result { - HashBuilder::from_phc_internal(data, Some(pepper.to_vec())) + Self::from_phc_internal(data, Some(pepper.to_vec())) } fn from_phc_internal(data: &str, pepper: Option>) -> Result { @@ -200,7 +200,7 @@ impl HashBuilder { } None => std_default::DEFAULT_XHMAC_ALGORITHM, }; - let hash_builder = HashBuilder { + let hash_builder = Self { standard: PasswordStorageStandard::NoStandard, normalization: norm, min_len: min_l, @@ -252,20 +252,20 @@ impl HashBuilder { } /// Set the way the password will be normalized. - pub fn normalization(&mut self, normalization: Normalization) -> &mut HashBuilder { + pub fn normalization(&mut self, normalization: Normalization) -> &mut Self { self.normalization = normalization; self } /// Set the password hashing algorithm. - pub fn algorithm(&mut self, algorithm: Algorithm) -> &mut HashBuilder { + pub fn algorithm(&mut self, algorithm: Algorithm) -> &mut Self { self.algorithm = algorithm; self.parameters = HashMap::new(); self } /// Set the way the password length will be calculated. - pub fn length_calculation(&mut self, method: LengthCalculationMethod) -> &mut HashBuilder { + pub fn length_calculation(&mut self, method: LengthCalculationMethod) -> &mut Self { self.length_calculation = method; self } @@ -273,49 +273,49 @@ impl HashBuilder { /// Set the salt length. /// /// Unused if a salt is given. - pub fn salt_len(&mut self, len: usize) -> &mut HashBuilder { + pub fn salt_len(&mut self, len: usize) -> &mut Self { self.salt_len = len; self } /// Set the password minimal length. - pub fn min_len(&mut self, len: usize) -> &mut HashBuilder { + pub fn min_len(&mut self, len: usize) -> &mut Self { self.min_len = len; self } /// Set the password maximal length. - pub fn max_len(&mut self, len: usize) -> &mut HashBuilder { + pub fn max_len(&mut self, len: usize) -> &mut Self { self.max_len = len; self } /// Add a parameter that will be used by the password hashing algorithm. - pub fn add_param(&mut self, key: &str, value: &str) -> &mut HashBuilder { + pub fn add_param(&mut self, key: &str, value: &str) -> &mut Self { self.parameters.insert(key.to_string(), value.to_string()); self } /// Set the hashing scheme version number. - pub fn version(&mut self, version: usize) -> &mut HashBuilder { + pub fn version(&mut self, version: usize) -> &mut Self { self.version = version + INTERNAL_VERSION; self } /// Set the hash function that will be used to compute the additional HMAC. - pub fn xhmac(&mut self, hash_func: HashFunction) -> &mut HashBuilder { + pub fn xhmac(&mut self, hash_func: HashFunction) -> &mut Self { self.xhmax_alg = hash_func; self } /// Add an additional HMAC with a pepper before hashing the password. - pub fn xhmac_before(&mut self, pepper: &[u8]) -> &mut HashBuilder { + pub fn xhmac_before(&mut self, pepper: &[u8]) -> &mut Self { self.xhmac = XHMAC::Before(pepper.to_vec()); self } /// Add an additional HMAC with a pepper after hashing the password. - pub fn xhmac_after(&mut self, pepper: &[u8]) -> &mut HashBuilder { + pub fn xhmac_after(&mut self, pepper: &[u8]) -> &mut Self { self.xhmac = XHMAC::After(pepper.to_vec()); self } diff --git a/src/pass/hasher/argon2.rs b/src/pass/hasher/argon2.rs index e4fa70d..dedc7b7 100644 --- a/src/pass/hasher/argon2.rs +++ b/src/pass/hasher/argon2.rs @@ -43,8 +43,8 @@ pub struct Argon2Hash { } impl Argon2Hash { - pub fn new() -> Argon2Hash { - Argon2Hash { + pub fn new() -> Self { + Self { passes: DEFAULT_PASSES, mem_cost: DEFAULT_MEM_COST, lanes: DEFAULT_LANES, diff --git a/src/pass/hasher/pbkdf2.rs b/src/pass/hasher/pbkdf2.rs index ec57889..7727a1d 100644 --- a/src/pass/hasher/pbkdf2.rs +++ b/src/pass/hasher/pbkdf2.rs @@ -34,8 +34,8 @@ pub struct Pbkdf2Hash { } impl Pbkdf2Hash { - pub fn new() -> Pbkdf2Hash { - Pbkdf2Hash { + pub fn new() -> Self { + Self { hash_function: DEFAULT_HASH_FUNCTION, nb_iter: DEFAULT_ITER, salt: KeyBuilder::new()