use crate::{sha256::Sha256, sha512::Sha512};
mod private {
pub trait Sealed {}
impl Sealed for crate::sha256::Sha256 {}
impl Sealed for crate::sha512::Sha512 {}
}
pub trait Digest: Clone + private::Sealed {
const OUTPUT_SIZE: usize;
const BLOCK_SIZE: usize;
type Output: AsRef<[u8]> + AsMut<[u8]> + Copy;
fn new() -> Self;
fn update(&mut self, data: &[u8]);
fn finalize(self) -> Self::Output;
fn digest(data: &[u8]) -> Self::Output {
let mut hasher = Self::new();
hasher.update(data);
hasher.finalize()
}
}
impl Digest for Sha256 {
const OUTPUT_SIZE: usize = crate::sha256::OUTPUT_SIZE;
const BLOCK_SIZE: usize = crate::sha256::BLOCK_SIZE;
type Output = [u8; crate::sha256::OUTPUT_SIZE];
fn new() -> Self {
Sha256::new()
}
fn update(&mut self, data: &[u8]) {
Sha256::update(self, data)
}
fn finalize(self) -> Self::Output {
Sha256::finalize(self)
}
}
impl Digest for Sha512 {
const OUTPUT_SIZE: usize = crate::sha512::OUTPUT_SIZE;
const BLOCK_SIZE: usize = crate::sha512::BLOCK_SIZE;
type Output = [u8; crate::sha512::OUTPUT_SIZE];
fn new() -> Self {
Sha512::new()
}
fn update(&mut self, data: &[u8]) {
Sha512::update(self, data)
}
fn finalize(self) -> Self::Output {
Sha512::finalize(self)
}
}