Skip to content

Commit 4853729

Browse files
authored
Merge pull request uutils#10772 from RenjiSann/cksum-shake-fix
cksum: Accept SHAKE algorithms
2 parents 99fa1bb + 756a777 commit 4853729

File tree

5 files changed

+273
-103
lines changed

5 files changed

+273
-103
lines changed

src/uu/cksum/benches/cksum_bench.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ macro_rules! bench_shake_algorithm {
5757
let data = text_data::generate_by_size(100, 80);
5858

5959
bencher.bench(|| {
60-
let mut shake = Shake128::new();
60+
let mut shake = Shake128::with_output_bits(256);
6161
shake.hash_update(&data);
6262

6363
// SHAKE algorithms can output any length, use 256 bits (32 bytes) for meaningful comparison
@@ -76,7 +76,7 @@ macro_rules! bench_shake_algorithm {
7676
let data = text_data::generate_by_size(100, 80);
7777

7878
bencher.bench(|| {
79-
let mut shake = Shake256::new();
79+
let mut shake = Shake256::with_output_bits(512);
8080
shake.hash_update(&data);
8181

8282
// SHAKE algorithms can output any length, use 256 bits (32 bytes) for meaningful comparison

src/uu/cksum/src/cksum.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ fn maybe_sanitize_length(
100100
sanitize_sha2_sha3_length_str(algo, s_len).map(Some)
101101
}
102102

103+
// SHAKE128 and SHAKE256 algorithms optionally take a bit length. No
104+
// validation is performed on this length, any value is valid. If the
105+
// given length is not a multiple of 8, the last byte of the output
106+
// will have its extra bits set to zero.
107+
(Some(AlgoKind::Shake128 | AlgoKind::Shake256), Some(len)) => match len.parse::<usize>() {
108+
Ok(0) => Ok(None),
109+
Ok(l) => Ok(Some(l)),
110+
Err(_) => Err(ChecksumError::InvalidLength(len.into()).into()),
111+
},
112+
103113
// For BLAKE2b, if a length is provided, validate it.
104114
(Some(AlgoKind::Blake2b), Some(len)) => calculate_blake2b_length_str(len),
105115

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

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ impl AlgoKind {
114114
ALGORITHM_OPTIONS_SHA256 => Sha256,
115115
ALGORITHM_OPTIONS_SHA384 => Sha384,
116116
ALGORITHM_OPTIONS_SHA512 => Sha512,
117+
118+
ALGORITHM_OPTIONS_SHAKE128 => Shake128,
119+
ALGORITHM_OPTIONS_SHAKE256 => Shake256,
117120
_ => return Err(ChecksumError::UnknownAlgorithm(algo.as_ref().to_string()).into()),
118121
})
119122
}
@@ -247,8 +250,8 @@ pub enum SizedAlgoKind {
247250
Sha3(ShaLength),
248251
// Note: we store Blake2b's length as BYTES.
249252
Blake2b(Option<usize>),
250-
Shake128(usize),
251-
Shake256(usize),
253+
Shake128(Option<usize>),
254+
Shake256(Option<usize>),
252255
}
253256

254257
impl SizedAlgoKind {
@@ -280,8 +283,8 @@ impl SizedAlgoKind {
280283
(ak::Sha1, _) => Ok(Self::Sha1),
281284
(ak::Blake3, _) => Ok(Self::Blake3),
282285

283-
(ak::Shake128, Some(l)) => Ok(Self::Shake128(l)),
284-
(ak::Shake256, Some(l)) => Ok(Self::Shake256(l)),
286+
(ak::Shake128, l) => Ok(Self::Shake128(l)),
287+
(ak::Shake256, l) => Ok(Self::Shake256(l)),
285288
(ak::Sha2, Some(l)) => Ok(Self::Sha2(ShaLength::try_from(l)?)),
286289
(ak::Sha3, Some(l)) => Ok(Self::Sha3(ShaLength::try_from(l)?)),
287290
(algo @ (ak::Sha2 | ak::Sha3), None) => {
@@ -298,7 +301,6 @@ impl SizedAlgoKind {
298301
(ak::Sha256, None) => Ok(Self::Sha2(ShaLength::Len256)),
299302
(ak::Sha384, None) => Ok(Self::Sha2(ShaLength::Len384)),
300303
(ak::Sha512, None) => Ok(Self::Sha2(ShaLength::Len512)),
301-
(_, None) => Err(ChecksumError::LengthRequired(kind.to_uppercase().into()).into()),
302304
}
303305
}
304306

@@ -322,45 +324,49 @@ impl SizedAlgoKind {
322324
pub fn create_digest(&self) -> Box<dyn Digest + 'static> {
323325
use ShaLength::*;
324326
match self {
325-
Self::Sysv => Box::new(SysV::new()),
326-
Self::Bsd => Box::new(Bsd::new()),
327-
Self::Crc => Box::new(Crc::new()),
328-
Self::Crc32b => Box::new(CRC32B::new()),
329-
Self::Md5 => Box::new(Md5::new()),
330-
Self::Sm3 => Box::new(Sm3::new()),
331-
Self::Sha1 => Box::new(Sha1::new()),
332-
Self::Blake3 => Box::new(Blake3::new()),
333-
Self::Sha2(Len224) => Box::new(Sha224::new()),
334-
Self::Sha2(Len256) => Box::new(Sha256::new()),
335-
Self::Sha2(Len384) => Box::new(Sha384::new()),
336-
Self::Sha2(Len512) => Box::new(Sha512::new()),
337-
Self::Sha3(Len224) => Box::new(Sha3_224::new()),
338-
Self::Sha3(Len256) => Box::new(Sha3_256::new()),
339-
Self::Sha3(Len384) => Box::new(Sha3_384::new()),
340-
Self::Sha3(Len512) => Box::new(Sha3_512::new()),
341-
Self::Blake2b(Some(byte_len)) => Box::new(Blake2b::with_output_bytes(*byte_len)),
342-
Self::Blake2b(None) => Box::new(Blake2b::new()),
343-
Self::Shake128(_) => Box::new(Shake128::new()),
344-
Self::Shake256(_) => Box::new(Shake256::new()),
327+
Self::Sysv => Box::new(SysV::default()),
328+
Self::Bsd => Box::new(Bsd::default()),
329+
Self::Crc => Box::new(Crc::default()),
330+
Self::Crc32b => Box::new(CRC32B::default()),
331+
Self::Md5 => Box::new(Md5::default()),
332+
Self::Sm3 => Box::new(Sm3::default()),
333+
Self::Sha1 => Box::new(Sha1::default()),
334+
Self::Blake3 => Box::new(Blake3::default()),
335+
Self::Sha2(Len224) => Box::new(Sha224::default()),
336+
Self::Sha2(Len256) => Box::new(Sha256::default()),
337+
Self::Sha2(Len384) => Box::new(Sha384::default()),
338+
Self::Sha2(Len512) => Box::new(Sha512::default()),
339+
Self::Sha3(Len224) => Box::new(Sha3_224::default()),
340+
Self::Sha3(Len256) => Box::new(Sha3_256::default()),
341+
Self::Sha3(Len384) => Box::new(Sha3_384::default()),
342+
Self::Sha3(Len512) => Box::new(Sha3_512::default()),
343+
Self::Blake2b(len_opt) => {
344+
Box::new(len_opt.map(Blake2b::with_output_bytes).unwrap_or_default())
345+
}
346+
Self::Shake128(len_opt) => {
347+
Box::new(len_opt.map(Shake128::with_output_bits).unwrap_or_default())
348+
}
349+
Self::Shake256(len_opt) => {
350+
Box::new(len_opt.map(Shake256::with_output_bits).unwrap_or_default())
351+
}
345352
}
346353
}
347354

348355
pub fn bitlen(&self) -> usize {
349-
use SizedAlgoKind::*;
350356
match self {
351-
Sysv => 512,
352-
Bsd => 1024,
353-
Crc => 256,
354-
Crc32b => 32,
355-
Md5 => 128,
356-
Sm3 => 512,
357-
Sha1 => 160,
358-
Blake3 => 256,
359-
Sha2(len) => len.as_usize(),
360-
Sha3(len) => len.as_usize(),
361-
Blake2b(len) => len.unwrap_or(512),
362-
Shake128(len) => *len,
363-
Shake256(len) => *len,
357+
Self::Sysv => 512,
358+
Self::Bsd => 1024,
359+
Self::Crc => 256,
360+
Self::Crc32b => 32,
361+
Self::Md5 => 128,
362+
Self::Sm3 => 512,
363+
Self::Sha1 => 160,
364+
Self::Blake3 => 256,
365+
Self::Sha2(len) => len.as_usize(),
366+
Self::Sha3(len) => len.as_usize(),
367+
Self::Blake2b(len) => len.unwrap_or(Blake2b::DEFAULT_BYTE_SIZE * 8),
368+
Self::Shake128(len) => len.unwrap_or(Shake128::DEFAULT_BIT_SIZE),
369+
Self::Shake256(len) => len.unwrap_or(Shake256::DEFAULT_BIT_SIZE),
364370
}
365371
}
366372
pub fn is_legacy(&self) -> bool {

0 commit comments

Comments
 (0)