From 6382ec4c68ca728e1f72316bdf1702331f28424a Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Sun, 16 Feb 2025 16:44:23 +0100 Subject: [PATCH] Avoid get_ in functions Also simplify the hashing function --- src/constants.rs | 20 ++++++++++---------- src/example.rs | 2 +- src/lib.rs | 6 +++--- src/oath_credential_id.rs | 2 +- src/refreshable_oath_credential.rs | 13 +++---------- 5 files changed, 18 insertions(+), 25 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index cb0e2b3..5a826a0 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -134,23 +134,23 @@ pub enum HashAlgo { impl HashAlgo { /// returns a function capable of hashing a byte array /// necessary to be able to validate keys before enrolling them on the hardware key - pub fn get_hash_fun(&self) -> impl Fn(&[u8]) -> Vec { + pub(crate) fn hash(&self, key: &[u8]) -> Vec { match self { - Self::Sha1 => |m: &[u8]| { + Self::Sha1 => { let mut hasher = sha1::Sha1::new(); - hasher.update(m); + hasher.update(key); hasher.finalize().to_vec() - }, - Self::Sha256 => |m: &[u8]| { + } + Self::Sha256 => { let mut hasher = sha2::Sha256::new(); - hasher.update(m); + hasher.update(key); hasher.finalize().to_vec() - }, - Self::Sha512 => |m: &[u8]| { + } + Self::Sha512 => { let mut hasher = sha2::Sha512::new(); - hasher.update(m); + hasher.update(key); hasher.finalize().to_vec() - }, + } } } diff --git a/src/example.rs b/src/example.rs index 84a1025..85bef9b 100644 --- a/src/example.rs +++ b/src/example.rs @@ -60,7 +60,7 @@ fn main() { println!("freshly defined oath: {}", calculated); session.delete_code(cred).unwrap(); */ - println!("YubiKey version is {:?}", session.get_version()); + println!("YubiKey version is {:?}", session.version()); for c in session.list_oath_codes().unwrap() { println!("{}", c); } diff --git a/src/lib.rs b/src/lib.rs index 3d3414a..dccfa3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,7 +32,7 @@ fn hmac_sha1(key: &[u8], message: &[u8]) -> Vec { fn hmac_shorten_key(key: &[u8], algo: HashAlgo) -> Vec { if key.len() > algo.digest_size() { - algo.get_hash_fun()(key) + algo.hash(key) } else { key.to_vec() } @@ -82,7 +82,7 @@ impl OathSession { }) } - pub fn get_version(&self) -> &[u8] { + pub fn version(&self) -> &[u8] { &self.version } @@ -270,7 +270,7 @@ impl OathSession { if cred.id_data.oath_type == OathType::Totp { data.extend(to_tlv( Tag::Challenge, - &time_challenge(Some(timestamp), cred.id_data.get_period()), + &time_challenge(Some(timestamp), cred.id_data.period()), )); } diff --git a/src/oath_credential_id.rs b/src/oath_credential_id.rs index 27ec4c5..201a44d 100644 --- a/src/oath_credential_id.rs +++ b/src/oath_credential_id.rs @@ -56,7 +56,7 @@ impl CredentialIDData { } /// Returns the defined period or default - pub fn get_period(&self) -> Duration { + pub fn period(&self) -> Duration { self.period.unwrap_or(DEFAULT_PERIOD) } diff --git a/src/refreshable_oath_credential.rs b/src/refreshable_oath_credential.rs index 7f6dbe1..5b4324f 100644 --- a/src/refreshable_oath_credential.rs +++ b/src/refreshable_oath_credential.rs @@ -66,18 +66,11 @@ impl<'a> RefreshableOathCredential<'a> { .duration_since(SystemTime::UNIX_EPOCH) .as_ref() .map_or(0, Duration::as_secs); - let time_step = timestamp_seconds / (self.cred.id_data.get_period().as_secs()); + let time_step = timestamp_seconds / (self.cred.id_data.period().as_secs()); let valid_from = SystemTime::UNIX_EPOCH - .checked_add( - self.cred - .id_data - .get_period() - .saturating_mul(time_step as u32), - ) - .unwrap(); - let valid_to = valid_from - .checked_add(self.cred.id_data.get_period()) + .checked_add(self.cred.id_data.period().saturating_mul(time_step as u32)) .unwrap(); + let valid_to = valid_from.checked_add(self.cred.id_data.period()).unwrap(); valid_from..valid_to } OathType::Hotp => {