Skip to content

Commit f3931a9

Browse files
committed
cksum: Fix -a blake3/shake*
1 parent fdb7bd7 commit f3931a9

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

src/uu/cksum/src/cksum.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,16 @@ fn maybe_sanitize_length(
100100
sanitize_sha2_sha3_length_str(algo, s_len).map(Some)
101101
}
102102

103-
// For BLAKE2b, if a length is provided, validate it.
103+
// For BLAKE2b and SHAKE, if a length is provided, validate it.
104104
(Some(AlgoKind::Blake2b), Some(len)) => calculate_blake2b_length_str(len),
105105

106+
(Some(AlgoKind::Shake128 | AlgoKind::Shake256), Some(s_len)) => {
107+
let len = s_len
108+
.parse::<usize>()
109+
.map_err(|_| ChecksumError::InvalidLength(s_len.to_string()))?;
110+
Ok(Some(len))
111+
}
112+
106113
// For any other provided algorithm, check if length is 0.
107114
// Otherwise, this is an error.
108115
(_, Some(len)) if len.parse::<u32>() == Ok(0_u32) => Ok(None),

src/uucore/src/lib/features/checksum/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ impl AlgoKind {
114114
ALGORITHM_OPTIONS_SHA256 => Sha256,
115115
ALGORITHM_OPTIONS_SHA384 => Sha384,
116116
ALGORITHM_OPTIONS_SHA512 => Sha512,
117+
118+
// Extension
119+
ALGORITHM_OPTIONS_BLAKE3 => Blake3,
120+
ALGORITHM_OPTIONS_SHAKE128 => Shake128,
121+
ALGORITHM_OPTIONS_SHAKE256 => Shake256,
122+
117123
_ => return Err(ChecksumError::UnknownAlgorithm(algo.as_ref().to_string()).into()),
118124
})
119125
}

tests/by-util/test_cksum.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,6 +3071,36 @@ mod debug_flag {
30713071
}
30723072
}
30733073

3074+
#[test]
3075+
#[cfg(target_os = "linux")]
3076+
fn test_dev_null() {
3077+
new_ucmd!()
3078+
.arg("-a")
3079+
.arg("blake3b")
3080+
.arg("/dev/null")
3081+
.succeeds()
3082+
.stdout_contains(
3083+
"BLAKE3 (/dev/null) = af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262",
3084+
);
3085+
}
3086+
3087+
#[test]
3088+
fn test_shake() {
3089+
let (at, mut ucmd) = at_and_ucmd!();
3090+
3091+
at.touch("a");
3092+
3093+
ucmd.arg("a")
3094+
.arg("-a")
3095+
.arg("shake128")
3096+
.arg("-l")
3097+
.arg("128")
3098+
.succeeds()
3099+
.stdout_contains(
3100+
"SHAKE128 (a) = 7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26",
3101+
);
3102+
}
3103+
30743104
#[test]
30753105
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
30763106
fn test_check_file_with_io_error() {

0 commit comments

Comments
 (0)