proxmox-backup/pbs-tools/src/sha.rs
Wolfgang Bumiller ba0ccc5991 move some tools used by the client
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2021-07-09 15:17:52 +02:00

30 lines
758 B
Rust

//! SHA helpers.
use std::io::Read;
use anyhow::Error;
/// Calculate the sha256sum from a readable object.
pub fn sha256(file: &mut dyn Read) -> Result<([u8; 32], u64), Error> {
let mut hasher = openssl::sha::Sha256::new();
let mut buffer = proxmox::tools::vec::undefined(256 * 1024);
let mut size: u64 = 0;
loop {
let count = match file.read(&mut buffer) {
Ok(0) => break,
Ok(count) => count,
Err(ref err) if err.kind() == std::io::ErrorKind::Interrupted => {
continue;
}
Err(err) => return Err(err.into()),
};
size += count as u64;
hasher.update(&buffer[..count]);
}
let csum = hasher.finish();
Ok((csum, size))
}