Skip to content

Commit dfda5e0

Browse files
committed
util(cksum): Add support for sha2 and sha3
1 parent 8f6e720 commit dfda5e0

File tree

6 files changed

+164
-92
lines changed

6 files changed

+164
-92
lines changed

src/uu/cksum/locales/en-US.ftl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ cksum-after-help = DIGEST determines the digest algorithm and default output for
88
- crc32b: (only available through cksum)
99
- md5: (equivalent to md5sum)
1010
- sha1: (equivalent to sha1sum)
11-
- sha224: (equivalent to sha224sum)
12-
- sha256: (equivalent to sha256sum)
13-
- sha384: (equivalent to sha384sum)
14-
- sha512: (equivalent to sha512sum)
11+
- sha2: (equivalent to sha{"{224,256,384,512}"}sum)
12+
- sha3: (only available through cksum)
1513
- blake2b: (equivalent to b2sum)
1614
- sm3: (only available through cksum)
1715

src/uu/cksum/locales/fr-FR.ftl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ cksum-after-help = DIGEST détermine l'algorithme de condensé et le format de s
88
- crc32b : (disponible uniquement via cksum)
99
- md5 : (équivalent à md5sum)
1010
- sha1 : (équivalent à sha1sum)
11-
- sha224 : (équivalent à sha224sum)
12-
- sha256 : (équivalent à sha256sum)
13-
- sha384 : (équivalent à sha384sum)
14-
- sha512 : (équivalent à sha512sum)
11+
- sha2: (équivalent à sha{"{224,256,384,512}"}sum)
12+
- sha3 : (disponible uniquement via cksum)
1513
- blake2b : (équivalent à b2sum)
1614
- sm3 : (disponible uniquement via cksum)
1715

src/uu/cksum/src/cksum.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ use std::iter;
1414
use std::path::Path;
1515
use uucore::checksum::{
1616
ALGORITHM_OPTIONS_BLAKE2B, ALGORITHM_OPTIONS_BSD, ALGORITHM_OPTIONS_CRC,
17-
ALGORITHM_OPTIONS_CRC32B, ALGORITHM_OPTIONS_SYSV, ChecksumError, ChecksumOptions,
18-
ChecksumVerbose, HashAlgorithm, LEGACY_ALGORITHMS, SUPPORTED_ALGORITHMS,
19-
calculate_blake2b_length, detect_algo, digest_reader, perform_checksum_validation,
17+
ALGORITHM_OPTIONS_CRC32B, ALGORITHM_OPTIONS_SHA2, ALGORITHM_OPTIONS_SHA3,
18+
ALGORITHM_OPTIONS_SYSV, ChecksumError, ChecksumOptions, ChecksumVerbose, HashAlgorithm,
19+
LEGACY_ALGORITHMS, SUPPORTED_ALGORITHMS, calculate_blake2b_length, detect_algo, digest_reader,
20+
perform_checksum_validation,
2021
};
2122
use uucore::translate;
2223

@@ -382,13 +383,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
382383

383384
let input_length = matches.get_one::<usize>(options::LENGTH);
384385

385-
let length = match input_length {
386-
None | Some(0) => None,
387-
Some(length) if algo_name == ALGORITHM_OPTIONS_BLAKE2B => {
388-
calculate_blake2b_length(*length)?
389-
}
386+
let length = match (input_length, algo_name) {
387+
// Length for sha2 and sha3 should be saved, it will be validated
388+
// afterwards if necessary.
389+
(Some(len), ALGORITHM_OPTIONS_SHA2 | ALGORITHM_OPTIONS_SHA3) => Some(*len),
390+
(None | Some(0), _) => None,
391+
// Length for Blake2b if saved only if it's not zero.
392+
(Some(len), ALGORITHM_OPTIONS_BLAKE2B) => calculate_blake2b_length(*len)?,
393+
// a --length flag set with any other algorithm is an error.
390394
_ => {
391-
return Err(ChecksumError::LengthOnlyForBlake2b.into());
395+
return Err(ChecksumError::LengthOnlyForBlake2bSha2Sha3.into());
392396
}
393397
};
394398

src/uu/hashsum/src/hashsum.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn create_algorithm_from_flags(matches: &ArgMatches) -> UResult<HashAlgorithm> {
101101
if matches.get_flag("sha3") {
102102
match matches.get_one::<usize>("bits") {
103103
Some(bits) => set_or_err(create_sha3(*bits)?)?,
104-
None => return Err(ChecksumError::BitsRequiredForSha3.into()),
104+
None => return Err(ChecksumError::LengthRequired("SHA3".into()).into()),
105105
}
106106
}
107107
if matches.get_flag("sha3-224") {
@@ -139,7 +139,7 @@ fn create_algorithm_from_flags(matches: &ArgMatches) -> UResult<HashAlgorithm> {
139139
create_fn: Box::new(|| Box::new(Shake128::new())),
140140
bits: *bits,
141141
})?,
142-
None => return Err(ChecksumError::BitsRequiredForShake128.into()),
142+
None => return Err(ChecksumError::LengthRequired("SHAKE128".into()).into()),
143143
}
144144
}
145145
if matches.get_flag("shake256") {
@@ -149,7 +149,7 @@ fn create_algorithm_from_flags(matches: &ArgMatches) -> UResult<HashAlgorithm> {
149149
create_fn: Box::new(|| Box::new(Shake256::new())),
150150
bits: *bits,
151151
})?,
152-
None => return Err(ChecksumError::BitsRequiredForShake256.into()),
152+
None => return Err(ChecksumError::LengthRequired("SHAKE256".into()).into()),
153153
}
154154
}
155155

0 commit comments

Comments
 (0)