1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

hashsum: split/doc detect_algo into smaller functions

This commit is contained in:
Sylvestre Ledru 2023-04-14 22:58:22 +02:00
parent 4e23b33954
commit 59c0f51208

View file

@ -50,31 +50,23 @@ struct Options {
zero: bool, zero: bool,
} }
#[allow(clippy::cognitive_complexity)] /// Creates a Blake2b hasher instance based on the specified length argument.
fn detect_algo( ///
program: &str, /// # Returns
matches: &ArgMatches, ///
) -> (&'static str, Box<dyn Digest + 'static>, usize) { /// Returns a tuple containing the algorithm name, the hasher instance, and the output length in bits.
let mut alg: Option<Box<dyn Digest>> = None; ///
let mut name: &'static str = ""; /// # Panics
let mut output_bits = 0; ///
match program { /// Panics if the length is not a multiple of 8 or if it is greater than 512.
"md5sum" => ("MD5", Box::new(Md5::new()) as Box<dyn Digest>, 128), fn create_blake2b(matches: &ArgMatches) -> (&'static str, Box<dyn Digest>, usize) {
"sha1sum" => ("SHA1", Box::new(Sha1::new()) as Box<dyn Digest>, 160), match matches.get_one::<usize>("length") {
"sha224sum" => ("SHA224", Box::new(Sha224::new()) as Box<dyn Digest>, 224),
"sha256sum" => ("SHA256", Box::new(Sha256::new()) as Box<dyn Digest>, 256),
"sha384sum" => ("SHA384", Box::new(Sha384::new()) as Box<dyn Digest>, 384),
"sha512sum" => ("SHA512", Box::new(Sha512::new()) as Box<dyn Digest>, 512),
"b2sum" => match matches.get_one::<usize>("length") {
// by default, blake2 uses 64 bytes (512 bits)
// --length=0 falls back to default behavior
Some(0) | None => ("BLAKE2", Box::new(Blake2b::new()) as Box<dyn Digest>, 512), Some(0) | None => ("BLAKE2", Box::new(Blake2b::new()) as Box<dyn Digest>, 512),
Some(length_in_bits) => { Some(length_in_bits) => {
if *length_in_bits > 512 { if *length_in_bits > 512 {
crash!(1, "Invalid length (maximum digest length is 512 bits)") crash!(1, "Invalid length (maximum digest length is 512 bits)")
} }
// blake2 output size must be a multiple of 8 bits
if length_in_bits % 8 == 0 { if length_in_bits % 8 == 0 {
let length_in_bytes = length_in_bits / 8; let length_in_bytes = length_in_bits / 8;
( (
@ -86,9 +78,20 @@ fn detect_algo(
crash!(1, "Invalid length (expected a multiple of 8)") crash!(1, "Invalid length (expected a multiple of 8)")
} }
} }
}, }
"b3sum" => ("BLAKE3", Box::new(Blake3::new()) as Box<dyn Digest>, 256), }
"sha3sum" => match matches.get_one::<usize>("bits") {
/// Creates a SHA3 hasher instance based on the specified bits argument.
///
/// # Returns
///
/// Returns a tuple containing the algorithm name, the hasher instance, and the output length in bits.
///
/// # Panics
///
/// Panics if an unsupported output size is provided, or if the `--bits` flag is missing.
fn create_sha3(matches: &ArgMatches) -> (&'static str, Box<dyn Digest>, usize) {
match matches.get_one::<usize>("bits") {
Some(224) => ( Some(224) => (
"SHA3-224", "SHA3-224",
Box::new(Sha3_224::new()) as Box<dyn Digest>, Box::new(Sha3_224::new()) as Box<dyn Digest>,
@ -114,7 +117,73 @@ fn detect_algo(
"Invalid output size for SHA3 (expected 224, 256, 384, or 512)" "Invalid output size for SHA3 (expected 224, 256, 384, or 512)"
), ),
None => crash!(1, "--bits required for SHA3"), None => crash!(1, "--bits required for SHA3"),
}, }
}
/// Creates a SHAKE-128 hasher instance based on the specified bits argument.
///
/// # Returns
///
/// Returns a tuple containing the algorithm name, the hasher instance, and the output length in bits.
///
/// # Panics
///
/// Panics if the `--bits` flag is missing.
fn create_shake128(matches: &ArgMatches) -> (&'static str, Box<dyn Digest>, usize) {
match matches.get_one::<usize>("bits") {
Some(bits) => (
"SHAKE128",
Box::new(Shake128::new()) as Box<dyn Digest>,
*bits,
),
None => crash!(1, "--bits required for SHAKE-128"),
}
}
/// Creates a SHAKE-256 hasher instance based on the specified bits argument.
///
/// # Returns
///
/// Returns a tuple containing the algorithm name, the hasher instance, and the output length in bits.
///
/// # Panics
///
/// Panics if the `--bits` flag is missing.
fn create_shake256(matches: &ArgMatches) -> (&'static str, Box<dyn Digest>, usize) {
match matches.get_one::<usize>("bits") {
Some(bits) => (
"SHAKE256",
Box::new(Shake256::new()) as Box<dyn Digest>,
*bits,
),
None => crash!(1, "--bits required for SHAKE-256"),
}
}
/// Detects the hash algorithm from the program name or command-line arguments.
///
/// # Arguments
///
/// * `program` - A string slice containing the program name.
/// * `matches` - A reference to the `ArgMatches` object containing the command-line arguments.
///
/// # Returns
///
/// Returns a tuple containing the algorithm name, the hasher instance, and the output length in bits.
fn detect_algo(
program: &str,
matches: &ArgMatches,
) -> (&'static str, Box<dyn Digest + 'static>, usize) {
let (name, alg, output_bits) = match program {
"md5sum" => ("MD5", Box::new(Md5::new()) as Box<dyn Digest>, 128),
"sha1sum" => ("SHA1", Box::new(Sha1::new()) as Box<dyn Digest>, 160),
"sha224sum" => ("SHA224", Box::new(Sha224::new()) as Box<dyn Digest>, 224),
"sha256sum" => ("SHA256", Box::new(Sha256::new()) as Box<dyn Digest>, 256),
"sha384sum" => ("SHA384", Box::new(Sha384::new()) as Box<dyn Digest>, 384),
"sha512sum" => ("SHA512", Box::new(Sha512::new()) as Box<dyn Digest>, 512),
"b2sum" => create_blake2b(matches),
"b3sum" => ("BLAKE3", Box::new(Blake3::new()) as Box<dyn Digest>, 256),
"sha3sum" => create_sha3(matches),
"sha3-224sum" => ( "sha3-224sum" => (
"SHA3-224", "SHA3-224",
Box::new(Sha3_224::new()) as Box<dyn Digest>, Box::new(Sha3_224::new()) as Box<dyn Digest>,
@ -135,32 +204,39 @@ fn detect_algo(
Box::new(Sha3_512::new()) as Box<dyn Digest>, Box::new(Sha3_512::new()) as Box<dyn Digest>,
512, 512,
), ),
"shake128sum" => match matches.get_one::<usize>("bits") { "shake128sum" => create_shake128(matches),
Some(bits) => ( "shake256sum" => create_shake256(matches),
"SHAKE128", _ => create_algorithm_from_flags(matches),
Box::new(Shake128::new()) as Box<dyn Digest>, };
*bits, (name, alg, output_bits)
), }
None => crash!(1, "--bits required for SHAKE-128"),
}, /// Creates a hasher instance based on the command-line flags.
"shake256sum" => match matches.get_one::<usize>("bits") { ///
Some(bits) => ( /// # Arguments
"SHAKE256", ///
Box::new(Shake256::new()) as Box<dyn Digest>, /// * `matches` - A reference to the `ArgMatches` object containing the command-line arguments.
*bits, ///
), /// # Returns
None => crash!(1, "--bits required for SHAKE-256"), ///
}, /// Returns a tuple containing the algorithm name, the hasher instance, and the output length in bits.
_ => { ///
{ /// # Panics
///
/// Panics if multiple hash algorithms are specified or if a required flag is missing.
fn create_algorithm_from_flags(matches: &ArgMatches) -> (&'static str, Box<dyn Digest>, usize) {
let mut alg: Option<Box<dyn Digest>> = None;
let mut name: &'static str = "";
let mut output_bits = 0;
let mut set_or_crash = |n, val, bits| { let mut set_or_crash = |n, val, bits| {
if alg.is_some() { if alg.is_some() {
crash!(1, "You cannot combine multiple hash algorithms!") crash!(1, "You cannot combine multiple hash algorithms!");
}; };
name = n; name = n;
alg = Some(val); alg = Some(val);
output_bits = bits; output_bits = bits;
}; };
if matches.get_flag("md5") { if matches.get_flag("md5") {
set_or_crash("MD5", Box::new(Md5::new()), 128); set_or_crash("MD5", Box::new(Md5::new()), 128);
} }
@ -186,33 +262,8 @@ fn detect_algo(
set_or_crash("BLAKE3", Box::new(Blake3::new()), 256); set_or_crash("BLAKE3", Box::new(Blake3::new()), 256);
} }
if matches.get_flag("sha3") { if matches.get_flag("sha3") {
match matches.get_one::<usize>("bits") { let (n, val, bits) = create_sha3(matches);
Some(224) => set_or_crash( set_or_crash(n, val, bits);
"SHA3-224",
Box::new(Sha3_224::new()) as Box<dyn Digest>,
224,
),
Some(256) => set_or_crash(
"SHA3-256",
Box::new(Sha3_256::new()) as Box<dyn Digest>,
256,
),
Some(384) => set_or_crash(
"SHA3-384",
Box::new(Sha3_384::new()) as Box<dyn Digest>,
384,
),
Some(512) => set_or_crash(
"SHA3-512",
Box::new(Sha3_512::new()) as Box<dyn Digest>,
512,
),
Some(_) => crash!(
1,
"Invalid output size for SHA3 (expected 224, 256, 384, or 512)"
),
None => crash!(1, "--bits required for SHA3"),
}
} }
if matches.get_flag("sha3-224") { if matches.get_flag("sha3-224") {
set_or_crash("SHA3-224", Box::new(Sha3_224::new()), 224); set_or_crash("SHA3-224", Box::new(Sha3_224::new()), 224);
@ -238,11 +289,9 @@ fn detect_algo(
None => crash!(1, "--bits required for SHAKE-256"), None => crash!(1, "--bits required for SHAKE-256"),
} }
} }
}
let alg = alg.unwrap_or_else(|| crash!(1, "You must specify hash algorithm!")); let alg = alg.unwrap_or_else(|| crash!(1, "You must specify hash algorithm!"));
(name, alg, output_bits) (name, alg, output_bits)
}
}
} }
// TODO: return custom error type // TODO: return custom error type