Skip to main content

wal_backup/
util.rs

1use sha2::{Digest, Sha256};
2use std::io::Read;
3
4#[allow(dead_code)]
5pub fn compute_sha256(data: &[u8]) -> String {
6    let mut hasher = Sha256::new();
7    hasher.update(data);
8    format!("sha256:{}", hex::encode(hasher.finalize()))
9}
10
11pub fn compute_sha256_file(path: &std::path::Path) -> std::io::Result<String> {
12    let mut file = std::fs::File::open(path)?;
13    let mut hasher = Sha256::new();
14    let mut buf = [0u8; 65536];
15    loop {
16        let n = file.read(&mut buf)?;
17        if n == 0 {
18            break;
19        }
20        hasher.update(&buf[..n]);
21    }
22    Ok(format!("sha256:{}", hex::encode(hasher.finalize())))
23}