rs_hasher_ctx 0.1.3

rs_hasher_ctx is an internal crate of the RustyShield library. It provides the HasherContext trait used across various cryptographic hash function implementations within RustyShield. The trait overloads the `Hasher::finish()` function, unifying the approach to obtaining hash results. While primarily intended for use within RustyShield, rs_hasher_ctx can aid in minimizing dependency entries in external crates leveraging RustyShield's hashing capabilities.
Documentation
use crate::{ByteArrayWrapper, HasherContext};
use core::hash::Hasher;
use rs_internal_hasher::{DigestThroughPad, HashAlgorithm};

#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct GenericHasher<H: Default + HashAlgorithm, const OUTPUT_LEN: usize> {
    pub padding: H::Padding,
    pub state: H,
}

impl<H: Default + HashAlgorithm, const OUTPUT_LEN: usize> Hasher for GenericHasher<H, OUTPUT_LEN>
where
    ByteArrayWrapper<OUTPUT_LEN>: From<H>,
{
    fn finish(&self) -> u64 {
        let mut hasher = self.clone();
        HasherContext::<OUTPUT_LEN>::finish(&mut hasher).state_to_u64()
    }

    fn write(&mut self, bytes: &[u8]) {
        self.padding.write(&mut self.state, bytes)
    }
}

impl<H: Default + HashAlgorithm, const OUTPUT_LEN: usize> HasherContext<OUTPUT_LEN> for GenericHasher<H, OUTPUT_LEN>
where
    ByteArrayWrapper<OUTPUT_LEN>: From<H>,
{
    type Output = H;

    fn finish(&mut self) -> Self::Output {
        self.padding.finish(&mut self.state);
        self.state.clone()
    }
}