// Copyright 2024 PARK Youngho. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your option. // This file may not be copied, modified, or distributed // except according to those terms. use std::{ io, env, fs }; use std::io::BufRead; use std::convert::From; use std::fmt::{ Debug, Display }; use cryptocol::number::SmallUInt; use cryptocol::hash::*; fn main() { let args: Vec = env::args().collect(); if args.len() < 4 { help(); return; } match &args[1][..] { "md4" => { print_hash_value(&args[2][..], MD4::new(), &args[3][..]); }, "md5" => { print_hash_value(&args[2][..], MD5::new(), &args[3][..]); }, "sha1" => { print_hash_value(&args[2][..], SHA1::new(), &args[3][..]); }, "sha2_256" => { print_hash_value(&args[2][..], SHA2_256::new(), &args[3][..]); }, "sha2_224" => { print_hash_value(&args[2][..], SHA2_224::new(), &args[3][..]); }, "sha2_512" => { print_hash_value(&args[2][..], SHA2_512::new(), &args[3][..]); }, "sha2_512_256" => { print_hash_value(&args[2][..], SHA2_512_256::new(), &args[3][..]); }, "sha2_384" => { print_hash_value(&args[2][..], SHA2_384::new(), &args[3][..]); }, "sha2_512_t_224" => { print_hash_value(&args[2][..], SHA2_512_t_224::new(), &args[3][..]); }, _ => { help(); }, } } fn print_hash_value(text_or_file: &str, hash: H, src: &str) { match text_or_file { "--text" | "-t" => { get_hash_value_from_text(hash, src); }, "--file" | "-f" => { get_hash_value_from_file(hash, src); }, "--check" | "-c" => { check_files(hash, src) }, _ => { help(); }, } } fn get_hash_value_from_text(mut hash: H, txt: &str) { hash.digest_str(txt); println!("Hash value:\t{}", hash.get_hash_value_in_string()); } fn get_hash_value_from_file(mut hash: H, file: &str) { if let Ok(contents) = fs::read(file) { hash.digest_vec(&contents); println!("Hash value:\t{}", hash.get_hash_value_in_string()); } else { println!("File Error!"); } } fn check_files(mut hash: H, file_list: &str) { let mut reader; match fs::File::open(file_list) { Ok(file) => { reader = io::BufReader::new(file); let mut line = String::new(); while let Ok(n) = reader.read_line(&mut line) { if n == 0 { break; } let txt = line.trim(); if txt.chars().nth(0).unwrap() == '#' { line.clear(); continue; } let elem: Vec<&str> = txt.split_whitespace().collect(); let item = elem[0]; let h = String::from(elem[1]).to_uppercase(); if let Ok(contents) = fs::read(item) { hash.digest_vec(&contents); if hash.get_hash_value_in_string() == h { println!("{} ---> OK", item); } else { println!("{} ---> Corrupted", item); } } line.clear(); } }, _ => { println!("File open error"); return; } } } fn help() { println!("This is a hash value extractor from a text or a file, using cryptocol."); println!("Usage: hash_app