1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

b2sum: add support to -l option

This commit is contained in:
Marras Antoine 2023-04-10 18:31:18 +02:00
parent f82f92ec9f
commit ae1113d7dc
2 changed files with 53 additions and 6 deletions

View file

@ -65,7 +65,23 @@ fn detect_algo(
"sha256sum" => ("SHA256", Box::new(Sha256::new()) as Box<dyn Digest>, 256), "sha256sum" => ("SHA256", Box::new(Sha256::new()) as Box<dyn Digest>, 256),
"sha384sum" => ("SHA384", Box::new(Sha384::new()) as Box<dyn Digest>, 384), "sha384sum" => ("SHA384", Box::new(Sha384::new()) as Box<dyn Digest>, 384),
"sha512sum" => ("SHA512", Box::new(Sha512::new()) as Box<dyn Digest>, 512), "sha512sum" => ("SHA512", Box::new(Sha512::new()) as Box<dyn Digest>, 512),
"b2sum" => ("BLAKE2", Box::new(Blake2b::new()) as Box<dyn Digest>, 512), "b2sum" => match matches.get_one::<usize>("length") {
Some(length_in_bits) => {
// blake2 output size must be a multiple of 8 bits
if length_in_bits % 8 == 0 {
let length_in_bytes = length_in_bits / 8;
(
"BLAKE2",
Box::new(Blake2b::with_output_bytes(length_in_bytes)),
*length_in_bits,
)
} else {
crash!(1, "Invalid length (expected a multiple of 8)")
}
}
// by default, blake2 uses 64bytes (512 bits)
None => ("BLAKE2", Box::new(Blake2b::new()) as Box<dyn Digest>, 512),
},
"b3sum" => ("BLAKE3", Box::new(Blake3::new()) as Box<dyn Digest>, 256), "b3sum" => ("BLAKE3", Box::new(Blake3::new()) as Box<dyn Digest>, 256),
"sha3sum" => match matches.get_one::<usize>("bits") { "sha3sum" => match matches.get_one::<usize>("bits") {
Some(224) => ( Some(224) => (
@ -379,6 +395,22 @@ pub fn uu_app_common() -> Command {
) )
} }
pub fn uu_app_length() -> Command {
uu_app_opt_length(uu_app_common())
}
fn uu_app_opt_length(command: Command) -> Command {
// Needed for variable-length output sums (e.g. SHAKE)
command.arg(
Arg::new("length")
.short('l')
.long("length")
.help("digest length in bits; must not exceed the max for the blake2 algorithm (512) and must be a multiple of 8")
.value_name("BITS")
.value_parser(parse_bit_num),
)
}
pub fn uu_app_b3sum() -> Command { pub fn uu_app_b3sum() -> Command {
uu_app_b3sum_opts(uu_app_common()) uu_app_b3sum_opts(uu_app_common())
} }
@ -454,7 +486,7 @@ fn uu_app(binary_name: &str) -> Command {
uu_app_common() uu_app_common()
} }
// b2sum supports the md5sum options plus -l/--length. // b2sum supports the md5sum options plus -l/--length.
"b2sum" => uu_app_common(), // TODO: Implement -l/--length "b2sum" => uu_app_length(),
// These have never been part of GNU Coreutils, but can function with the same // These have never been part of GNU Coreutils, but can function with the same
// options as md5sum. // options as md5sum.
"sha3-224sum" | "sha3-256sum" | "sha3-384sum" | "sha3-512sum" => uu_app_common(), "sha3-224sum" | "sha3-256sum" | "sha3-384sum" | "sha3-512sum" => uu_app_common(),

View file

@ -38,10 +38,25 @@ pub trait Digest {
} }
} }
pub struct Blake2b(blake2b_simd::State); /// first element of the tuple is the blake2b state
/// second is the number of output bits
pub struct Blake2b(blake2b_simd::State, usize);
impl Blake2b {
/// Return a new Blake2b instance with a custom output bytes length
pub fn with_output_bytes(output_bytes: usize) -> Self {
let mut params = blake2b_simd::Params::new();
params.hash_length(output_bytes);
let state = params.to_state();
Self(state, output_bytes * 8)
}
}
impl Digest for Blake2b { impl Digest for Blake2b {
fn new() -> Self { fn new() -> Self {
Self(blake2b_simd::State::new()) // by default, Blake2b output is 512 bits long (= 64B)
Self::with_output_bytes(64)
} }
fn hash_update(&mut self, input: &[u8]) { fn hash_update(&mut self, input: &[u8]) {
@ -54,11 +69,11 @@ impl Digest for Blake2b {
} }
fn reset(&mut self) { fn reset(&mut self) {
*self = Self::new(); *self = Self::with_output_bytes(self.output_bytes());
} }
fn output_bits(&self) -> usize { fn output_bits(&self) -> usize {
512 self.1
} }
} }