use sha2::Digest as _;
use crate::hash::{Digest, HASH_LEN, Hasher};
#[derive(Clone, Default)]
pub struct Sha256Hasher(sha2::Sha256);
impl Sha256Hasher {
#[inline]
pub fn new() -> Self {
Self(sha2::Sha256::new())
}
}
impl core::fmt::Debug for Sha256Hasher {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Sha256Hasher").finish_non_exhaustive()
}
}
impl Hasher for Sha256Hasher {
#[inline]
fn reset(&mut self) {
self.0 = sha2::Sha256::new();
}
#[inline]
fn update(&mut self, bytes: &[u8]) {
sha2::Digest::update(&mut self.0, bytes);
}
#[inline]
fn finalize(&mut self, out: &mut Digest) {
let result = self.0.finalize_reset();
let mut bytes = [0u8; HASH_LEN];
bytes.copy_from_slice(&result);
*out = Digest::from_bytes(bytes);
}
}