Skip to content

Commit

Permalink
Use Self
Browse files Browse the repository at this point in the history
  • Loading branch information
breard-r committed Dec 10, 2023
1 parent 086a334 commit 48dec4d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
18 changes: 9 additions & 9 deletions src/oath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,21 @@ impl From<Error> 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);
Expand All @@ -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);
Expand All @@ -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) => {
Expand All @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions src/oath/hotp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand Down
18 changes: 9 additions & 9 deletions src/oath/totp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -246,28 +246,28 @@ 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
}

/// 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 {
Expand All @@ -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
}
Expand Down
38 changes: 19 additions & 19 deletions src/pass/hash_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -129,12 +129,12 @@ impl HashBuilder {

/// Create a new Hasher object from a PHC formatted string.
pub fn from_phc(data: &str) -> Result<Hasher, Error> {
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<Hasher, Error> {
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<Vec<u8>>) -> Result<Hasher, Error> {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -252,70 +252,70 @@ 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
}

/// 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
}
Expand Down
4 changes: 2 additions & 2 deletions src/pass/hasher/argon2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/pass/hasher/pbkdf2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 48dec4d

Please sign in to comment.