1use std::{fs, io::Read, path};
2
3use sha2::{Digest, Sha256};
4
5pub fn hash_file(file: &path::Path) -> Result<String, std::io::Error> {
7 let mut hasher = Sha256::new();
8 let mut file = fs::File::open(file)?;
9 let mut buffer = [0; 1024];
10
11 loop {
12 let bytes_read = file.read(&mut buffer)?;
13 if bytes_read == 0 {
14 break;
15 }
16 hasher.update(&buffer[..bytes_read]);
17 }
18
19 let result = hasher.finalize();
20 Ok(format!("{:x}", result))
21}
22
23pub fn hash_string<S>(input: S) -> String
25where
26 S: Into<String>,
27{
28 let mut hasher = Sha256::new();
29 hasher.update(input.into());
30 let result = hasher.finalize();
31 format!("{:x}", result)
32}