// 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 cryptocol::hash::MD5; fn main() { let args: Vec = env::args().collect(); if args.len() < 3 { help(); return; } let arg = &args[1][..]; match arg { "--text" | "-t" => { get_hash_value_from_text(&args[2][..]); }, "--file" | "-f" => { get_hash_value_from_file(&args[2][..]); }, "--check" | "-c" => { check_files(&args[2][..]) }, _ => { help(); }, } } fn get_hash_value_from_text(txt: &str) { let mut hash = MD5::new(); hash.digest_str(txt); println!("Hash value:\t{}", hash.get_hash_value_in_string()); } fn get_hash_value_from_file(file: &str) { if let Ok(contents) = fs::read(file) { let mut hash = MD5::new(); hash.digest_vec(&contents); println!("Hash value:\t{}", hash.get_hash_value_in_string()); } else { println!("File Error!"); } } fn check_files(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) { let mut hash = MD5::new(); hash.digest_vec(&contents); if hash.to_string() == h { println!("{} ---> OK", item); } else { println!("{} ---> Corrupted", item); } } line.clear(); } }, _ => { println!("File open error"); return; } } } fn help() { println!("This is an MD5 hash value extractor from a text or a file, using cryptocol."); println!("Usage: md5_app