-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum_sha256.rs
78 lines (69 loc) · 1.9 KB
/
checksum_sha256.rs
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
use std::fs::File;
use std::io::{self, Read};
use std::io::stdin;
use crypto::digest::Digest;
use crypto::sha2::Sha256;
fn main() {
let mut file_path = String::new();
let mut input_hashsum = String::new();
println!("Enter your checksum: ");
match stdin().read_line(&mut input_hashsum) {
Ok(bytes) => {
println!("Ok. bytes entered = {}",
bytes
);
}
Err(err) => {
println!("Error: {}",
err
)},
}
println!("Enter file path for checking checksum: ");
match stdin().read_line(&mut file_path) {
Ok(bytes) => {
println!("Ok. bytes entered = {}", bytes);
file_path = file_path
.trim()
.to_string();
},
Err(err) => {
println!("Error: {}",
err
)},
}
match calculate_hash(&file_path) {
Ok(hash) => {
println!("Checksum of {}: {}",
file_path,
hash
);
let check = check_sum(&input_hashsum
.trim()
.to_string(),
&hash
);
println!("Is checksum of {} correct: {}",
file_path,
check
);
},
Err(err) => {println!("Error: {}",
err
)},
}
}
fn calculate_hash(file_path: &str) -> io::Result<String> {
let mut file = File::open(file_path)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
let mut hasher = Sha256::new();
hasher.input(&buffer);
println!("Read bytes {} from file. Ok.", buffer.len());
Ok(hasher.result_str())
}
fn check_sum(input_hash: &String, output_hash: &String) -> bool {
if *input_hash != *output_hash {
return false;
}
return true;
}