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
use sha2::{Digest, Sha256};
use std::str;
use std::{fs, io};
use crate::error::ValidateError;
pub struct HashChecker;
impl HashChecker {
pub fn check(filename: &str, expected_hash: &str) -> Result<(), ValidateError> {
let mut result = Ok(());
if filename != "stdout" && (!expected_hash.is_empty()) {
let actual_hash = HashChecker::sha256sum(filename);
if actual_hash != expected_hash {
result = Err(ValidateError::Sha256Mismatch);
}
match result {
Ok(()) => println!("✅ Checksum OK."),
Err(ValidateError::Sha256Mismatch) => println!(
"❌ Checksum verification failed for {}:\n expected: {}\n got: {}",
filename, expected_hash, actual_hash
),
}
}
result
}
fn sha256sum(filename: &str) -> String {
let mut hasher = Sha256::new();
let mut file = fs::File::open(filename).unwrap();
io::copy(&mut file, &mut hasher).unwrap();
let computed_hash = hasher.finalize();
drop(file);
format!("{:x}", computed_hash)
}
}
#[cfg(test)]
mod tests {
use super::*;
macro_rules! assert_err {
($expression:expr, $($pattern:tt)+) => {
match $expression {
$($pattern)+ => (),
ref e => panic!("expected `{}` but got `{:?}`", stringify!($($pattern)+), e),
}
}
}
#[test]
fn test_check_api_fails_when_checksum_mismatch() {
let expected = "AAAAbea8f23421c6306df712af6f416a3f570ecf5652b45fd6d409019fe6d4fe";
let _ = assert_err!(
HashChecker::check("LICENSE.md", expected),
Err(ValidateError::Sha256Mismatch)
);
}
#[test]
fn test_sha256sum_api() {
let expected = "fa701768a0ddfd65fe175ecf9865b6046f151bb05d0d4ad2cef5acb1d4c60c6b";
let actual = HashChecker::sha256sum("LICENSE.md");
assert_eq!(actual, expected);
}
#[test]
fn test_check_api_works_when_typical() {
let expected = "fa701768a0ddfd65fe175ecf9865b6046f151bb05d0d4ad2cef5acb1d4c60c6b";
let is_match = HashChecker::check("LICENSE.md", expected).is_ok();
assert!(is_match);
}
}