Skip to content

Commit

Permalink
Fixed issue with wrong assignment of bytes to fields that take bits
Browse files Browse the repository at this point in the history
  • Loading branch information
SarthakSingh31 authored and sylvestre committed Sep 16, 2024
1 parent 566c3a1 commit b48fc5a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
5 changes: 2 additions & 3 deletions src/uu/cksum/src/cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ where
(
if let Some(length) = options.length {
// Multiply by 8 here, as we want to print the length in bits.
format!("BLAKE2b-{} (", length * 8)
format!("BLAKE2b-{} (", length)
} else {
"BLAKE2b (".to_owned()
},
Expand Down Expand Up @@ -552,9 +552,8 @@ mod tests {

#[test]
fn test_calculate_length() {
assert_eq!(calculate_blake2b_length(256).unwrap(), Some(32));
assert_eq!(calculate_blake2b_length(256).unwrap(), Some(256));
assert_eq!(calculate_blake2b_length(512).unwrap(), None);
assert_eq!(calculate_blake2b_length(256).unwrap(), Some(32));
calculate_blake2b_length(255).unwrap_err();

calculate_blake2b_length(33).unwrap_err();
Expand Down
18 changes: 9 additions & 9 deletions src/uucore/src/lib/features/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub fn detect_algo(algo: &str, length: Option<usize>) -> UResult<HashAlgorithm>
} else {
Ok(HashAlgorithm {
name: ALGORITHM_OPTIONS_BLAKE2B,
create_fn: Box::new(move || Box::new(Blake2b::with_output_bytes(bits))),
create_fn: Box::new(move || Box::new(Blake2b::with_output_bytes(bits / 8))),
bits,
})
}
Expand All @@ -248,7 +248,7 @@ pub fn detect_algo(algo: &str, length: Option<usize>) -> UResult<HashAlgorithm>
ALGORITHM_OPTIONS_SM3 => Ok(HashAlgorithm {
name: ALGORITHM_OPTIONS_SM3,
create_fn: Box::new(|| Box::new(Sm3::new())),
bits: 512,
bits: 256,
}),
ALGORITHM_OPTIONS_SHAKE128 | "shake128sum" => {
let bits =
Expand Down Expand Up @@ -438,7 +438,7 @@ fn identify_algo_name_and_length(
let bits = caps.name("bits").map_or(Some(None), |m| {
let bits_value = m.as_str().parse::<usize>().unwrap();
if bits_value % 8 == 0 {
Some(Some(bits_value / 8))
Some(Some(bits_value))
} else {
*properly_formatted = false;
None // Return None to signal a divisibility issue
Expand Down Expand Up @@ -516,7 +516,7 @@ where
get_expected_checksum(filename_to_check, &caps, &chosen_regex)?;

// If the algo_name is provided, we use it, otherwise we try to detect it
let (algo_name, length) = if is_algo_based_format {
let (algo_name, length_in_bits) = if is_algo_based_format {
identify_algo_name_and_length(
&caps,
algo_name_input,
Expand All @@ -530,7 +530,7 @@ where
if algo_name_input == Some(ALGORITHM_OPTIONS_BLAKE2B) {
// division by 2 converts the length of the Blake2b checksum from hexadecimal
// characters to bytes, as each byte is represented by two hexadecimal characters.
let length = Some(expected_checksum.len() / 2);
let length = Some(expected_checksum.len() * 4);
(ALGORITHM_OPTIONS_BLAKE2B.to_string(), length)
} else {
(a.to_lowercase(), length_input)
Expand All @@ -545,9 +545,9 @@ where
properly_formatted = false;
continue;
}
let mut algo = detect_algo(&algo_name, length)?;
let mut algo = detect_algo(&algo_name, length_in_bits)?;

if algo.bits != (expected_checksum.len() * 4) {
if length_in_bits.unwrap_or(algo.bits) != (expected_checksum.len() * 4) {
res.bad_format += 1;

Check warning on line 551 in src/uucore/src/lib/features/checksum.rs

View check run for this annotation

Codecov / codecov/patch

src/uucore/src/lib/features/checksum.rs#L551

Added line #L551 was not covered by tests
continue;
}
Expand Down Expand Up @@ -704,7 +704,7 @@ pub fn calculate_blake2b_length(length: usize) -> UResult<Option<usize>> {
// So, don't show it
Ok(None)
} else {
Ok(Some(n / 8))
Ok(Some(n))
}
}
}
Expand Down Expand Up @@ -776,7 +776,7 @@ mod tests {
assert!(calculate_blake2b_length(10).is_err());
assert!(calculate_blake2b_length(520).is_err());
assert_eq!(calculate_blake2b_length(512).unwrap(), None);
assert_eq!(calculate_blake2b_length(256).unwrap(), Some(32));
assert_eq!(calculate_blake2b_length(256).unwrap(), Some(256));
}

#[test]
Expand Down

0 comments on commit b48fc5a

Please sign in to comment.