1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2024 PARK Youngho.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, 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;
type HASH = MD5;
fn main()
{
let args: Vec<String> = 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 = HASH::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 = HASH::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 = HASH::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 <option> <source>");
println!("options description");
println!("--text, -t : <source> is a text to get a hash code.");
println!(" The text should be enclosed by ' or \".");
println!("--file, -f : <source> is the name of the file to get a hash code.");
println!("--check, -c : <source> is the name of the file that contains pairs");
println!(" of file and its hash code.");
println!("--help, -h : print this help message on screen\n");
println!("Examples:");
println!("\tmd5_app -t 'How are you doing?'");
println!("\tmd5_app -f linuxmint-21.3-cinnamon-64bit.iso");
println!("\tmd5_app -c CHECKSUM");
}