use core::fmt; use core::marker::PhantomData; use common::array::ArraySize; use common::hazmat::SerializableState; use common::{BlockSizeUser, KeyInit, KeySizeUser, OutputSizeUser, Reset}; use crate::{ CollisionResistance, CustomizedInit, ExtendableOutput, ExtendableOutputReset, FixedOutput, FixedOutputReset, HashMarker, Update, }; /// Wrapper around [`ExtendableOutput`] types adding [`OutputSizeUser`] with the given size of `S`. pub struct XofFixedWrapper { hash: T, size: PhantomData, } impl Clone for XofFixedWrapper { fn clone(&self) -> Self { Self { hash: self.hash.clone(), size: PhantomData, } } } impl fmt::Debug for XofFixedWrapper { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("XofFixedWrapper") .field("hash", &self.hash) .field("_size", &self.size) .finish() } } impl Default for XofFixedWrapper { fn default() -> Self { Self { hash: Default::default(), size: PhantomData, } } } impl HashMarker for XofFixedWrapper {} #[cfg(feature = "mac")] impl crate::MacMarker for XofFixedWrapper { } impl CollisionResistance for XofFixedWrapper { type CollisionResistance = T::CollisionResistance; } // this blanket impl is needed for HMAC impl BlockSizeUser for XofFixedWrapper { type BlockSize = T::BlockSize; } impl KeySizeUser for XofFixedWrapper { type KeySize = T::KeySize; } impl KeyInit for XofFixedWrapper { fn new(key: &common::Key) -> Self { Self { hash: T::new(key), size: PhantomData, } } } impl Reset for XofFixedWrapper { fn reset(&mut self) { self.hash.reset(); } } impl Update for XofFixedWrapper { fn update(&mut self, data: &[u8]) { self.hash.update(data); } } impl OutputSizeUser for XofFixedWrapper { type OutputSize = S; } impl FixedOutput for XofFixedWrapper { fn finalize_into(self, out: &mut common::Output) { self.hash.finalize_xof_into(out); } } impl FixedOutputReset for XofFixedWrapper { fn finalize_into_reset(&mut self, out: &mut common::Output) { self.hash.finalize_xof_reset_into(out); } } impl ExtendableOutput for XofFixedWrapper { type Reader = T::Reader; fn finalize_xof(self) -> Self::Reader { self.hash.finalize_xof() } } impl ExtendableOutputReset for XofFixedWrapper { fn finalize_xof_reset(&mut self) -> Self::Reader { self.hash.finalize_xof_reset() } } #[cfg(feature = "zeroize")] impl zeroize::ZeroizeOnDrop for XofFixedWrapper { } impl CustomizedInit for XofFixedWrapper { fn new_customized(customization: &[u8]) -> Self { Self { hash: T::new_customized(customization), size: PhantomData, } } } impl SerializableState for XofFixedWrapper { type SerializedStateSize = T::SerializedStateSize; fn serialize(&self) -> common::hazmat::SerializedState { self.hash.serialize() } fn deserialize( serialized_state: &common::hazmat::SerializedState, ) -> Result { T::deserialize(serialized_state).map(|hash| Self { hash, size: PhantomData, }) } }