obeli_sk_utils/
sha256sum.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
use concepts::ContentDigest;
use std::path::Path;

#[tracing::instrument(skip_all)]
pub async fn calculate_sha256_file<P: AsRef<Path>>(
    path: P,
) -> Result<ContentDigest, std::io::Error> {
    use sha2::Digest;
    use tokio::io::AsyncReadExt;
    let mut file = tokio::fs::File::open(path).await?;
    let mut hasher = sha2::Sha256::default();
    let mut buf = [0; 4096];
    loop {
        let n = file.read(&mut buf).await?;
        if n == 0 {
            break;
        }
        hasher.update(&buf[..n]);
    }
    Ok(ContentDigest::new(
        concepts::HashType::Sha256,
        format!("{:x}", hasher.finalize()),
    ))
}