hash_based_signatures/
io.rs

1use anyhow::Context as _;
2use anyhow::Result;
3use ring::digest::{Context, Digest, SHA256};
4use std::fs::File;
5use std::io::Read;
6use std::path::Path;
7
8pub fn hash_file(path: &Path) -> Result<Digest> {
9    let mut reader = File::open(path)
10        .with_context(|| format!("Failed to open file at {:?}. Does it exist?", path))?;
11    let mut context = Context::new(&SHA256);
12    let mut buffer = [0; 1024];
13
14    loop {
15        let count = reader
16            .read(&mut buffer)
17            .with_context(|| format!("Cannot read file at {:?}.", path))?;
18        if count == 0 {
19            break;
20        }
21        context.update(&buffer[..count]);
22    }
23
24    Ok(context.finish())
25}