Skip to content

Commit d66817f

Browse files
AldanTanneoRenjiSann
authored andcommitted
add length support for shake128 and shake256
1 parent 0f891cc commit d66817f

3 files changed

Lines changed: 95 additions & 17 deletions

File tree

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,15 @@ pub enum SizedAlgoKind {
250250
Sha3(ShaLength),
251251
// Note: we store Blake2b's length as BYTES.
252252
Blake2b(Option<usize>),
253+
// Shake* length are stored in bits.
253254
Shake128(Option<usize>),
254255
Shake256(Option<usize>),
255256
}
256257

257258
impl SizedAlgoKind {
258-
pub fn from_unsized(kind: AlgoKind, byte_length: Option<usize>) -> UResult<Self> {
259+
pub fn from_unsized(kind: AlgoKind, output_length: Option<usize>) -> UResult<Self> {
259260
use AlgoKind as ak;
260-
match (kind, byte_length) {
261+
match (kind, output_length) {
261262
(
262263
ak::Sysv
263264
| ak::Bsd
@@ -305,19 +306,26 @@ impl SizedAlgoKind {
305306
}
306307

307308
pub fn to_tag(self) -> String {
308-
use SizedAlgoKind::*;
309309
match self {
310-
Md5 => "MD5".into(),
311-
Sm3 => "SM3".into(),
312-
Sha1 => "SHA1".into(),
313-
Blake3 => "BLAKE3".into(),
314-
Sha2(len) => format!("SHA{}", len.as_usize()),
315-
Sha3(len) => format!("SHA3-{}", len.as_usize()),
316-
Blake2b(Some(byte_len)) => format!("BLAKE2b-{}", byte_len * 8),
317-
Blake2b(None) => "BLAKE2b".into(),
318-
Shake128(_) => "SHAKE128".into(),
319-
Shake256(_) => "SHAKE256".into(),
320-
Sysv | Bsd | Crc | Crc32b => panic!("Should not be used for tagging"),
310+
Self::Md5 => "MD5".into(),
311+
Self::Sm3 => "SM3".into(),
312+
Self::Sha1 => "SHA1".into(),
313+
Self::Blake3 => "BLAKE3".into(),
314+
Self::Sha2(len) => format!("SHA{}", len.as_usize()),
315+
Self::Sha3(len) => format!("SHA3-{}", len.as_usize()),
316+
Self::Blake2b(Some(byte_len)) => format!("BLAKE2b-{}", byte_len * 8),
317+
Self::Blake2b(None) => "BLAKE2b".into(),
318+
Self::Shake128(opt_bit_len) => format!(
319+
"SHAKE128-{}",
320+
opt_bit_len.unwrap_or(Shake128::DEFAULT_BIT_SIZE)
321+
),
322+
Self::Shake256(opt_bit_len) => format!(
323+
"SHAKE256-{}",
324+
opt_bit_len.unwrap_or(Shake256::DEFAULT_BIT_SIZE)
325+
),
326+
Self::Sysv | Self::Bsd | Self::Crc | Self::Crc32b => {
327+
panic!("Should not be used for tagging")
328+
}
321329
}
322330
}
323331

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::checksum::{
1919
};
2020
use crate::error::{FromIo, UError, UIoError, UResult, USimpleError};
2121
use crate::quoting_style::{QuotingStyle, locale_aware_escape_name};
22-
use crate::sum::DigestOutput;
22+
use crate::sum::{self, DigestOutput};
2323
use crate::{
2424
os_str_as_bytes, os_str_from_bytes, read_os_string_lines, show, show_warning_caps, translate,
2525
};
@@ -643,6 +643,7 @@ fn identify_algo_name_and_length(
643643
AlgoKind::Sha2 | AlgoKind::Sha3 if [224, 256, 384, 512].contains(&bitlen) => {
644644
Some(bitlen)
645645
}
646+
AlgoKind::Shake128 | AlgoKind::Shake256 => Some(bitlen),
646647
// Either
647648
// the algo based line is provided with a bit length
648649
// with an algorithm that does not support it (only Blake2B does).
@@ -741,6 +742,9 @@ fn process_algo_based_line(
741742
// checksum with it.
742743
let digest_char_length_hint = match (algo_kind, algo_byte_len) {
743744
(AlgoKind::Blake2b, Some(byte_len)) => Some(byte_len),
745+
(AlgoKind::Shake128 | AlgoKind::Shake256, Some(bit_len)) => Some(bit_len.div_ceil(8)),
746+
(AlgoKind::Shake128, None) => Some(sum::Shake128::DEFAULT_BIT_SIZE.div_ceil(8)),
747+
(AlgoKind::Shake256, None) => Some(sum::Shake256::DEFAULT_BIT_SIZE.div_ceil(8)),
744748
_ => None,
745749
};
746750

tests/by-util/test_cksum.rs

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,13 +3150,19 @@ fn test_check_checkfile_with_io_error() {
31503150
"ac"
31513151
)]
31523152
fn test_shake128(#[case] args: &[&str], #[case] expected: &str) {
3153+
let bit_len = if args.is_empty() || args[1] == "0" {
3154+
"256"
3155+
} else {
3156+
args[1]
3157+
};
3158+
31533159
new_ucmd!()
31543160
.arg("-a")
31553161
.arg("shake128")
31563162
.args(args)
31573163
.pipe_in("xxx")
31583164
.succeeds()
3159-
.stdout_only(format!("SHAKE128 (-) = {expected}\n"));
3165+
.stdout_only(format!("SHAKE128-{bit_len} (-) = {expected}\n"));
31603166
}
31613167

31623168
#[rstest]
@@ -3213,11 +3219,71 @@ fn test_shake128(#[case] args: &[&str], #[case] expected: &str) {
32133219
"2f"
32143220
)]
32153221
fn test_shake256(#[case] args: &[&str], #[case] expected: &str) {
3222+
let bit_len = if args.is_empty() || args[1] == "0" {
3223+
"512"
3224+
} else {
3225+
args[1]
3226+
};
3227+
32163228
new_ucmd!()
32173229
.arg("-a")
32183230
.arg("shake256")
32193231
.args(args)
32203232
.pipe_in("xxx")
32213233
.succeeds()
3222-
.stdout_only(format!("SHAKE256 (-) = {expected}\n"));
3234+
.stdout_only(format!("SHAKE256-{bit_len} (-) = {expected}\n"));
3235+
}
3236+
3237+
#[test]
3238+
fn test_check_shake128_no_length() {
3239+
const INPUT_SHAKE128_CORRECT_LEN: &str =
3240+
"SHAKE128 (bar) = ac8549b2861a151896ab721bd29d7a20c1a3d1f75b31266f786f20d963fb0fdf";
3241+
const INPUT_SHAKE128_WRONG_LEN: &str = "SHAKE128 (bar) = ac8549b2861a151896ab721bd29d7a20";
3242+
3243+
let scene = TestScenario::new(util_name!());
3244+
let at = &scene.fixtures;
3245+
3246+
at.write("bar", "xxx");
3247+
3248+
scene
3249+
.ucmd()
3250+
.arg("-a")
3251+
.arg("shake128")
3252+
.arg("-c")
3253+
.pipe_in(INPUT_SHAKE128_CORRECT_LEN)
3254+
.succeeds();
3255+
3256+
scene
3257+
.ucmd()
3258+
.arg("-a")
3259+
.arg("shake128")
3260+
.arg("-c")
3261+
.pipe_in(INPUT_SHAKE128_WRONG_LEN)
3262+
.fails()
3263+
.stderr_only("cksum: 'standard input': no properly formatted checksum lines found\n");
3264+
}
3265+
3266+
#[test]
3267+
fn test_check_shake256_no_length() {
3268+
const INPUT_SHAKE256_CORRECT_LEN: &str = "SHAKE256 (bar) = 2fa631503c3ea5fe85131dbfa24805185474740e6dcb5f2a64f69d932bcb55f7b24958f3e3c4cc0e71f1fe6f054cd3fb28b9efb62b4f8f3fbe6d50d90f5c6eba";
3269+
const INPUT_SHAKE256_WRONG_LEN: &str =
3270+
"SHAKE256 (bar) = 2fa631503c3ea5fe85131dbfa24805185474740e6dcb5f2a64f69d932bcb55f7";
3271+
3272+
let scene = TestScenario::new(util_name!());
3273+
let at = &scene.fixtures;
3274+
3275+
at.write("bar", "xxx");
3276+
3277+
scene
3278+
.ucmd()
3279+
.arg("-c")
3280+
.pipe_in(INPUT_SHAKE256_CORRECT_LEN)
3281+
.succeeds();
3282+
3283+
scene
3284+
.ucmd()
3285+
.arg("-c")
3286+
.pipe_in(INPUT_SHAKE256_WRONG_LEN)
3287+
.fails()
3288+
.stderr_only("cksum: 'standard input': no properly formatted checksum lines found\n");
32233289
}

0 commit comments

Comments
 (0)