use sha1::Digest; use std::hash::Hasher; /// Hesher returning hash from the algorithm implementing Digest pub struct DigestHasher { digest: D, } impl DigestHasher where D: Digest, { /// Obtain hash. pub fn finalize(self) -> String { hex::encode(self.digest.finalize().0) } } impl From for DigestHasher where D: Digest, { /// Creating a hash from an algorithm implementing Digest. fn from(digest: D) -> Self { DigestHasher { digest } } } impl Hasher for DigestHasher { /// Stopper to prevent calling the standard Hasher result. fn finish(&self) -> u64 { unimplemented!("Do not call finish()"); } fn write(&mut self, bytes: &[u8]) { self.digest.update(bytes); } }