From e8f3c47416601e4f5c77353a9b92bb4b359389b5 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 24 Nov 2019 09:16:27 +0800 Subject: [PATCH] use get_digest_by_algorithm --- src/main.rs | 84 ++++++++++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/src/main.rs b/src/main.rs index 02e75bc..95da813 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,52 +85,28 @@ fn main() -> XResult<()> { let the_algo = options.algorithm.to_uppercase(); if options.file_name_list.is_empty() { - match the_algo.as_str() { - "MD5" => println!("{} - ({})", calc_digest_stdin(&mut Md5::new())?, the_algo), - "SHA1" | "SHA-1" => println!("{} - ({})", calc_digest_stdin(&mut Sha1::new())?, the_algo), - "SHA224" | "SHA-224" => println!("{} - ({})", calc_digest_stdin(&mut Sha224::new())?, the_algo), - "SHA256" | "SHA-256" => println!("{} - ({})", calc_digest_stdin(&mut Sha256::new())?, the_algo), - "SHA384" | "SHA-384" => println!("{} - ({})", calc_digest_stdin(&mut Sha384::new())?, the_algo), - "SHA512" | "SHA-512" => println!("{} - ({})", calc_digest_stdin(&mut Sha512::new())?, the_algo), - "SHA512-224" => println!("{} - ({})", calc_digest_stdin(&mut Sha512Trunc224::new())?, the_algo), - "SHA512-256" => println!("{} - ({})", calc_digest_stdin(&mut Sha512Trunc256::new())?, the_algo), - "SHA3-224" => println!("{} - ({})", calc_digest_stdin(&mut Sha3::sha3_224())?, the_algo), - "SHA3-256" => println!("{} - ({})", calc_digest_stdin(&mut Sha3::sha3_256())?, the_algo), - "SHA3-384" => println!("{} - ({})", calc_digest_stdin(&mut Sha3::sha3_384())?, the_algo), - "SHA3-512" => println!("{} - ({})", calc_digest_stdin(&mut Sha3::sha3_512())?, the_algo), - "SHAKE-128" => println!("{} - ({})", calc_digest_stdin(&mut Sha3::shake_128())?, the_algo), - "SHAKE-256" => println!("{} - ({})", calc_digest_stdin(&mut Sha3::shake_256())?, the_algo), - "KECCAK-224" => println!("{} - ({})", calc_digest_stdin(&mut Sha3::keccak224())?, the_algo), - "KECCAK-256" => println!("{} - ({})", calc_digest_stdin(&mut Sha3::keccak256())?, the_algo), - "KECCAK-384" => println!("{} - ({})", calc_digest_stdin(&mut Sha3::keccak384())?, the_algo), - "KECCAK-512" => println!("{} - ({})", calc_digest_stdin(&mut Sha3::keccak512())?, the_algo), - "SM3" => println!("{} - ({})", hex::encode(Sm3Hash::new(&read_full_stdin()?).get_hash()), the_algo), - _ => print_message(MessageType::ERROR, &format!("Unknown algorithm: {}", options.algorithm)), + let boxed_digest = get_digest_by_algorithm(the_algo.as_str()); + match boxed_digest { + Some(mut digest) => { + println!("{} - ({})", calc_digest_stdin(&mut *digest)?, the_algo); + }, + None => match the_algo.as_str() { + "SM3" => println!("{} - ({})", hex::encode(Sm3Hash::new(&read_full_stdin()?).get_hash()), the_algo), + _ => print_message(MessageType::ERROR, &format!("Unknown algorithm: {}", options.algorithm)), + }, }; } else { for f in options.file_name_list { let the_fn = f.as_str(); - match the_algo.as_str() { - "MD5" => println!("{} - {} ({})", calc_file_digest(&mut Md5::new(), the_fn)?, the_fn, the_algo), - "SHA1" | "SHA-1" => println!("{} - {} ({})", calc_file_digest(&mut Sha1::new(), the_fn)?, the_fn, the_algo), - "SHA224" | "SHA-224" => println!("{} - {} ({})", calc_file_digest(&mut Sha224::new(), the_fn)?, the_fn, the_algo), - "SHA256" | "SHA-256" => println!("{} - {} ({})", calc_file_digest(&mut Sha256::new(), the_fn)?, the_fn, the_algo), - "SHA384" | "SHA-384" => println!("{} - {} ({})", calc_file_digest(&mut Sha384::new(), the_fn)?, the_fn, the_algo), - "SHA512" | "SHA-512" => println!("{} - {} ({})", calc_file_digest(&mut Sha512::new(), the_fn)?, the_fn, the_algo), - "SHA512-224" => println!("{} - {} ({})", calc_file_digest(&mut Sha512Trunc224::new(), the_fn)?, the_fn, the_algo), - "SHA512-256" => println!("{} - {} ({})", calc_file_digest(&mut Sha512Trunc256::new(), the_fn)?, the_fn, the_algo), - "SHA3-224" => println!("{} - {} ({})", calc_file_digest(&mut Sha3::sha3_224(), the_fn)?, the_fn, the_algo), - "SHA3-256" => println!("{} - {} ({})", calc_file_digest(&mut Sha3::sha3_256(), the_fn)?, the_fn, the_algo), - "SHA3-384" => println!("{} - {} ({})", calc_file_digest(&mut Sha3::sha3_384(), the_fn)?, the_fn, the_algo), - "SHA3-512" => println!("{} - {} ({})", calc_file_digest(&mut Sha3::sha3_512(), the_fn)?, the_fn, the_algo), - "SHAKE-128" => println!("{} - {} ({})", calc_file_digest(&mut Sha3::shake_128(), the_fn)?, the_fn, the_algo), - "SHAKE-256" => println!("{} - {} ({})", calc_file_digest(&mut Sha3::shake_256(), the_fn)?, the_fn, the_algo), - "KECCAK-224" => println!("{} - {} ({})", calc_file_digest(&mut Sha3::keccak224(), the_fn)?, the_fn, the_algo), - "KECCAK-256" => println!("{} - {} ({})", calc_file_digest(&mut Sha3::keccak256(), the_fn)?, the_fn, the_algo), - "KECCAK-384" => println!("{} - {} ({})", calc_file_digest(&mut Sha3::keccak384(), the_fn)?, the_fn, the_algo), - "KECCAK-512" => println!("{} - {} ({})", calc_file_digest(&mut Sha3::keccak512(), the_fn)?, the_fn, the_algo), - "SM3" => println!("{} - {} ({})", hex::encode(Sm3Hash::new(&read_file_full(the_fn)?).get_hash()), the_fn, the_algo), - _ => print_message(MessageType::ERROR, &format!("Unknown algorithm: {}", options.algorithm)), + let boxed_digest = get_digest_by_algorithm(the_algo.as_str()); + match boxed_digest { + Some(mut digest) => { + println!("{} - {} ({})", calc_file_digest(&mut *digest, the_fn)?, the_fn, the_algo); + }, + None => match the_algo.as_str() { + "SM3" => println!("{} - {} ({})", hex::encode(Sm3Hash::new(&read_file_full(the_fn)?).get_hash()), the_fn, the_algo), + _ => print_message(MessageType::ERROR, &format!("Unknown algorithm: {}", options.algorithm)), + }, }; } } @@ -152,6 +128,30 @@ KECCAK-224, KECCAK-256, KECCAK-384, KECCAK-512, SM3 "#, VERSION, &GIT_HASH[0..7]); } +fn get_digest_by_algorithm(algo: &str) -> Option> { + match algo { + "MD5" => Some(Box::new(Md5::new())), + "SHA1" | "SHA-1" => Some(Box::new(Sha1::new())), + "SHA224" | "SHA-224" => Some(Box::new(Sha224::new())), + "SHA256" | "SHA-256" => Some(Box::new(Sha256::new())), + "SHA384" | "SHA-384" => Some(Box::new(Sha384::new())), + "SHA512" | "SHA-512" => Some(Box::new(Sha512::new())), + "SHA512-224" => Some(Box::new(Sha512Trunc224::new())), + "SHA512-256" => Some(Box::new(Sha512Trunc256::new())), + "SHA3-224" => Some(Box::new(Sha3::sha3_224())), + "SHA3-256" => Some(Box::new(Sha3::sha3_256())), + "SHA3-384" => Some(Box::new(Sha3::sha3_384())), + "SHA3-512" => Some(Box::new(Sha3::sha3_512())), + "SHAKE-128" => Some(Box::new(Sha3::shake_128())), + "SHAKE-256" => Some(Box::new(Sha3::shake_256())), + "KECCAK-224" => Some(Box::new(Sha3::keccak224())), + "KECCAK-256" => Some(Box::new(Sha3::keccak256())), + "KECCAK-384" => Some(Box::new(Sha3::keccak384())), + "KECCAK-512" => Some(Box::new(Sha3::keccak512())), + _ => None, + } +} + fn read_file_full(file_name: &str) -> XResult> { let mut buf: [u8; BUFF_SIZE] = [0u8; BUFF_SIZE]; let mut f = File::open(file_name)?;