Skip to content

Commit

Permalink
minor code tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Feb 12, 2025
1 parent 923ef32 commit 4bd3d51
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
8 changes: 3 additions & 5 deletions des/src/des.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,9 @@ impl KeyInit for Des {

#[inline]
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
let is_weak = super::weak_key_test(&key.0);
if is_weak == 0 {
Ok(())
} else {
Err(WeakKeyError)
match super::weak_key_test(&key.0) {
0 => Ok(()),
_ => Err(WeakKeyError),
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions des/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ pub use crate::tdes::{TdesEde2, TdesEde3, TdesEee2, TdesEee3};

/// Checks whether the key is weak.
///
/// Returns 1 if key is weak and 0 otherwise.
/// Returns 1 if the key is weak; otherwise, returns 0.
fn weak_key_test(key: &[u8; 8]) -> u8 {
let key = u64::from_ne_bytes(*key);
let mut is_weak = 0u8;

for &weak_key in crate::consts::WEAK_KEYS {
is_weak |= u8::from(key == weak_key);
}
Expand Down
7 changes: 3 additions & 4 deletions des/src/tdes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ fn weak_key_test(key: &[u8]) -> Result<(), WeakKeyError> {
is_weak |= super::weak_key_test(des_key);
}

if is_weak == 0 {
Ok(())
} else {
Err(WeakKeyError)
match is_weak {
0 => Ok(()),
_ => Err(WeakKeyError),
}
}

Expand Down

0 comments on commit 4bd3d51

Please sign in to comment.