use hmac::digest::{ block_buffer::Eager, core_api::{ BlockSizeUser, BufferKindUser, CoreProxy, CoreWrapper, FixedOutputCore, OutputSizeUser, UpdateCore, }, generic_array::typenum::{IsLess, Le, NonZero, U256}, Digest, FixedOutput, HashMarker, KeyInit, Output, Update, }; use hmac::{Hmac, HmacCore, SimpleHmac}; pub trait Sealed { type Core: Clone; fn new_from_slice(key: &[u8]) -> Self; fn new_core(key: &[u8]) -> Self::Core; fn from_core(core: &Self::Core) -> Self; fn update(&mut self, data: &[u8]); fn finalize(self) -> Output; } impl Sealed for Hmac where H: CoreProxy + OutputSizeUser, H::Core: HashMarker + UpdateCore + FixedOutputCore + BufferKindUser + Default + Clone, ::BlockSize: IsLess, Le<::BlockSize, U256>: NonZero, { type Core = HmacCore; #[inline(always)] fn new_from_slice(key: &[u8]) -> Self { KeyInit::new_from_slice(key).expect("HMAC can take a key of any size") } #[inline(always)] fn new_core(key: &[u8]) -> Self::Core { HmacCore::new_from_slice(key).expect("HMAC can take a key of any size") } #[inline(always)] fn from_core(core: &Self::Core) -> Self { CoreWrapper::from_core(core.clone()) } #[inline(always)] fn update(&mut self, data: &[u8]) { Update::update(self, data); } #[inline(always)] fn finalize(self) -> Output { // Output and Output are always equal to each other, // but we can not prove it at type level Output::::clone_from_slice(&self.finalize_fixed()) } } impl Sealed for SimpleHmac { type Core = Self; #[inline(always)] fn new_from_slice(key: &[u8]) -> Self { KeyInit::new_from_slice(key).expect("HMAC can take a key of any size") } #[inline(always)] fn new_core(key: &[u8]) -> Self::Core { KeyInit::new_from_slice(key).expect("HMAC can take a key of any size") } #[inline(always)] fn from_core(core: &Self::Core) -> Self { core.clone() } #[inline(always)] fn update(&mut self, data: &[u8]) { Update::update(self, data); } #[inline(always)] fn finalize(self) -> Output { // Output and Output are always equal to each other, // but we can not prove it at type level Output::::clone_from_slice(&self.finalize_fixed()) } }