use alloc::boxed::Box;
pub use crate::msgs::enums::HashAlgorithm;
pub trait Hash: Send + Sync {
fn start(&self) -> Box<dyn Context>;
fn hash(&self, data: &[u8]) -> Output;
fn output_len(&self) -> usize;
fn algorithm(&self) -> HashAlgorithm;
fn fips(&self) -> bool {
false
}
}
pub struct Output {
buf: [u8; Self::MAX_LEN],
used: usize,
}
impl Output {
pub fn new(bytes: &[u8]) -> Self {
let mut output = Self {
buf: [0u8; Self::MAX_LEN],
used: bytes.len(),
};
debug_assert!(bytes.len() <= Self::MAX_LEN);
output.buf[..bytes.len()].copy_from_slice(bytes);
output
}
pub const MAX_LEN: usize = 64;
}
impl AsRef<[u8]> for Output {
fn as_ref(&self) -> &[u8] {
&self.buf[..self.used]
}
}
pub trait Context: Send + Sync {
fn fork_finish(&self) -> Output;
fn fork(&self) -> Box<dyn Context>;
fn finish(self: Box<Self>) -> Output;
fn update(&mut self, data: &[u8]);
}