Merge pull request #8645 from cakebaker/uucore_checksum_simplify_create_sha3

uucore/checksum: improve API of `create_sha3()`
This commit is contained in:
Sylvestre Ledru
2025-09-16 22:12:36 +02:00
committed by GitHub
2 changed files with 16 additions and 14 deletions
+4 -2
View File
@@ -97,8 +97,10 @@ fn create_algorithm_from_flags(matches: &ArgMatches) -> UResult<HashAlgorithm> {
set_or_err(detect_algo("b3sum", None)?)?;
}
if matches.get_flag("sha3") {
let bits = matches.get_one::<usize>("bits").copied();
set_or_err(create_sha3(bits)?)?;
match matches.get_one::<usize>("bits") {
Some(bits) => set_or_err(create_sha3(*bits)?)?,
None => return Err(ChecksumError::BitsRequiredForSha3.into()),
}
}
if matches.get_flag("sha3-224") {
set_or_err(HashAlgorithm {
+12 -12
View File
@@ -249,34 +249,32 @@ impl UError for ChecksumError {
///
/// # Returns
///
/// Returns a UResult of a tuple containing the algorithm name, the hasher instance, and
/// the output length in bits or an Err if an unsupported output size is provided, or if
/// the `--bits` flag is missing.
pub fn create_sha3(bits: Option<usize>) -> UResult<HashAlgorithm> {
/// Returns a `UResult` with an `HashAlgorithm` or an `Err` if an unsupported
/// output size is provided.
pub fn create_sha3(bits: usize) -> UResult<HashAlgorithm> {
match bits {
Some(224) => Ok(HashAlgorithm {
224 => Ok(HashAlgorithm {
name: "SHA3_224",
create_fn: Box::new(|| Box::new(Sha3_224::new())),
bits: 224,
}),
Some(256) => Ok(HashAlgorithm {
256 => Ok(HashAlgorithm {
name: "SHA3_256",
create_fn: Box::new(|| Box::new(Sha3_256::new())),
bits: 256,
}),
Some(384) => Ok(HashAlgorithm {
384 => Ok(HashAlgorithm {
name: "SHA3_384",
create_fn: Box::new(|| Box::new(Sha3_384::new())),
bits: 384,
}),
Some(512) => Ok(HashAlgorithm {
512 => Ok(HashAlgorithm {
name: "SHA3_512",
create_fn: Box::new(|| Box::new(Sha3_512::new())),
bits: 512,
}),
Some(_) => Err(ChecksumError::InvalidOutputSizeForSha3.into()),
None => Err(ChecksumError::BitsRequiredForSha3.into()),
_ => Err(ChecksumError::InvalidOutputSizeForSha3.into()),
}
}
@@ -459,8 +457,10 @@ pub fn detect_algo(algo: &str, length: Option<usize>) -> UResult<HashAlgorithm>
bits,
})
}
//ALGORITHM_OPTIONS_SHA3 | "sha3" => (
_ if algo.starts_with("sha3") => create_sha3(length),
_ if algo.starts_with("sha3") => {
let bits = length.ok_or(ChecksumError::BitsRequiredForSha3)?;
create_sha3(bits)
}
_ => Err(ChecksumError::UnknownAlgorithm.into()),
}