hashsum: rename the blake2b algo with --tag + manage the different length

This commit is contained in:
Sylvestre Ledru 2024-05-18 10:04:48 +02:00
parent 7a46ea371f
commit a9a11f486e
2 changed files with 35 additions and 6 deletions

View file

@ -59,7 +59,7 @@ struct Options {
/// greater than 512.
fn create_blake2b(matches: &ArgMatches) -> UResult<(&'static str, Box<dyn Digest>, usize)> {
match matches.get_one::<usize>("length") {
Some(0) | None => Ok(("BLAKE2", Box::new(Blake2b::new()) as Box<dyn Digest>, 512)),
Some(0) | None => Ok(("BLAKE2b", Box::new(Blake2b::new()) as Box<dyn Digest>, 512)),
Some(length_in_bits) => {
if *length_in_bits > 512 {
return Err(USimpleError::new(
@ -71,7 +71,7 @@ fn create_blake2b(matches: &ArgMatches) -> UResult<(&'static str, Box<dyn Digest
if length_in_bits % 8 == 0 {
let length_in_bytes = length_in_bits / 8;
Ok((
"BLAKE2",
"BLAKE2b",
Box::new(Blake2b::with_output_bytes(length_in_bytes)),
*length_in_bits,
))
@ -792,10 +792,15 @@ where
.map_err_context(|| "failed to read input".to_string())?;
let (escaped_filename, prefix) = escape_filename(filename);
if options.tag {
println!(
"{}{} ({}) = {}",
prefix, options.algoname, escaped_filename, sum
);
if options.algoname == "BLAKE2b" && options.digest.output_bits() != 512 {
// special case for BLAKE2b with non-default output length
println!(
"BLAKE2b-{} ({escaped_filename}) = {sum}",
options.digest.output_bits()
);
} else {
println!("{prefix}{} ({escaped_filename}) = {sum}", options.algoname);
}
} else if options.nonames {
println!("{sum}");
} else if options.zero {

View file

@ -243,6 +243,30 @@ fn test_invalid_b2sum_length_option_too_large() {
.code_is(1);
}
#[test]
fn test_check_b2sum_tag_output() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
scene
.ccmd("b2sum")
.arg("--length=0")
.arg("--tag")
.arg("f")
.succeeds()
.stdout_only("BLAKE2b (f) = 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce\n");
scene
.ccmd("b2sum")
.arg("--length=128")
.arg("--tag")
.arg("f")
.succeeds()
.stdout_only("BLAKE2b-128 (f) = cae66941d9efbd404e4d88758ea67670\n");
}
#[test]
fn test_check_file_not_found_warning() {
let scene = TestScenario::new(util_name!());