use core::{fmt, mem};
use digest::{
core_api::BlockSizeUser,
generic_array::{typenum::*, GenericArray},
FixedOutput, FixedOutputReset, OutputSizeUser, Reset, Update,
};
use ring::digest::Context;
macro_rules! impl_digest {
(
$(#[doc = $doc:tt])*
$name:ident, $hasher:ident, $block_len:ty, $output_size:ty
) => {
$(#[doc = $doc])*
#[repr(transparent)]
#[derive(Clone)]
pub struct $name(Context);
impl $name {
fn take(&mut self) -> Context {
mem::replace(&mut self.0, Context::new(&ring::digest::$hasher))
}
}
impl Default for $name {
fn default() -> Self {
$name(Context::new(&ring::digest::$hasher))
}
}
impl Update for $name {
fn update(&mut self, data: &[u8]) {
self.0.update(data.as_ref())
}
}
impl BlockSizeUser for $name {
type BlockSize = $block_len;
}
impl OutputSizeUser for $name {
type OutputSize = $output_size;
}
impl FixedOutput for $name {
fn finalize_into(self, out: &mut GenericArray<u8, Self::OutputSize>) {
*out = GenericArray::clone_from_slice(self.0.finish().as_ref());
}
}
impl FixedOutputReset for $name {
fn finalize_into_reset(&mut self, out: &mut GenericArray<u8, Self::OutputSize>) {
*out = GenericArray::clone_from_slice(self.take().finish().as_ref());
}
}
impl Reset for $name {
fn reset(&mut self) {
self.take();
}
}
impl fmt::Debug for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct(stringify!($name)).finish_non_exhaustive()
}
}
};
}
impl_digest!(
Sha1,
SHA1_FOR_LEGACY_USE_ONLY,
U64,
U20
);
impl_digest!(
Sha256,
SHA256,
U64,
U32
);
impl_digest!(
Sha384,
SHA384,
U128,
U48
);
impl_digest!(
Sha512,
SHA512,
U128,
U64
);
impl_digest!(
Sha512Trunc256,
SHA512_256,
U128,
U32
);