use alloc::vec::Vec;
use crate::CryptoError;
pub trait Hash: Send + Sync {
#[must_use]
fn name(&self) -> &'static str;
#[must_use]
fn output_len(&self) -> usize;
#[must_use = "result must be checked"]
fn hash(&self, msg: &[u8], out: &mut [u8]) -> Result<(), CryptoError>;
#[must_use = "result must be checked"]
fn hash_to_vec(&self, msg: &[u8]) -> Result<Vec<u8>, CryptoError> {
let mut out = alloc::vec![0u8; self.output_len()];
self.hash(msg, &mut out)?;
Ok(out)
}
#[must_use = "result must be checked"]
fn hash_to_array<const N: usize>(&self, msg: &[u8]) -> Result<[u8; N], CryptoError>
where
Self: Sized,
{
if N != self.output_len() {
return Err(CryptoError::BadInput);
}
let mut out = [0u8; N];
self.hash(msg, &mut out)?;
Ok(out)
}
}
pub trait StreamingHash: Send {
fn update(&mut self, data: &[u8]);
#[must_use = "result must be checked"]
fn finalize(self, out: &mut [u8]) -> Result<(), CryptoError>;
fn reset(&mut self);
}