Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/uu/hashsum/src/hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
))
Expand Down Expand Up @@ -327,7 +327,7 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
// least somewhat better from a user's perspective.
let matches = command.try_get_matches_from(args)?;

let (name, algo, bits) = detect_algo(&binary_name, &matches)?;
let (algoname, algo, bits) = detect_algo(&binary_name, &matches)?;

let binary = if matches.get_flag("binary") {
true
Expand Down Expand Up @@ -355,7 +355,7 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
}

let opts = Options {
algoname: name,
algoname,
digest: algo,
output_bits: bits,
binary,
Expand Down Expand Up @@ -792,17 +792,22 @@ 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 {
// with zero, we don't escape the filename
print!("{} {}{}\0", sum, binary_marker, filename.display());
print!("{sum} {binary_marker}{}\0", filename.display());
} else {
println!("{}{} {}{}", prefix, sum, binary_marker, escaped_filename);
println!("{prefix}{sum} {binary_marker}{escaped_filename}");
}
}
if bad_format > 0 && failed_cksum == 0 && correct_format == 0 && !options.status {
Expand Down
24 changes: 24 additions & 0 deletions tests/by-util/test_hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!());
Expand Down