It should be easy to write a small wrapper around any writer using a SHA-1 crate that supports incremental updating, e.g. sha-1:
use std::io::{ Write, Result as IoResult };
use sha1::Sha1;
pub struct Sha1Writer<W> {
writer: W,
hasher: Sha1,
}
impl<W> Sha1Writer<W> {
fn new(writer: W) -> Self {
Sha1Writer { writer, hasher: Sha1::new() }
}
fn into_digest(self) -> impl AsRef<[u8]> {
self.hasher.finalize()
}
}
impl<W: Write> Write for Sha1Writer<W> {
fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
self.hasher.update(buf);
self.writer.write(buf)
}
fn flush(&mut self) -> IoResult<()> {
self.writer.flush()
}
}
You might want to implement all the default methods of io::Write
as well, for the sake of performance.
Furthermore, if you can choose the hashing algorithm, don't choose SHA-1 – it's not considered secure anymore. Try at least SHA-256 or some of the SHA-3 candidates instead, such as BLAKE2 or Keccak.