use sha2::Digest;
#[derive(Default)]
pub struct Sha224 {
sha224: sha2::Sha224,
}
impl Sha224 {
pub fn new() -> Self {
Self::default()
}
pub fn hash(data: &[u8]) -> [u8; 28] {
let mut hash = Self::new();
hash.write(data);
hash.finish()
}
pub fn write(&mut self, data: &[u8]) {
self.sha224.update(data);
}
pub fn finish(self) -> [u8; 28] {
self.sha224.finalize().into()
}
}
impl std::io::Write for Sha224 {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.write(buf);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
impl std::hash::Hasher for Sha224 {
fn finish(&self) -> u64 {
panic!(
"not supported because the hash values produced by this hasher \
contain more than just the 64 bits returned by this method"
)
}
fn write(&mut self, bytes: &[u8]) {
self.write(bytes)
}
}