pub struct StreamingHashHmac<H, F>{ /* private fields */ }Expand description
Generic HMAC over any StreamingHash implementation.
The type parameter H is the underlying hash; F is the factory that
creates fresh instances of H. Both H and F must be Send to allow
the MAC to cross thread boundaries.
§Construction
Use StreamingHashHmac::new to provide a key, block size, and hash
factory. The resulting value implements one-shot StreamingHashHmac::mac_oneshot
and incremental StreamingHashHmac::streaming_session.
§Example
use oxicrypto_hash::Sha256Streaming;
use oxicrypto_mac::hmac_streaming_hash::StreamingHashHmac;
let key = b"secret-key-for-hmac";
let msg = b"hello, world";
let mut tag = [0u8; 32];
let mut hmac = StreamingHashHmac::new(key, 64, || Sha256Streaming::new())?;
hmac.mac_oneshot(msg, &mut tag)?;Implementations§
Source§impl<H, F> StreamingHashHmac<H, F>
impl<H, F> StreamingHashHmac<H, F>
Sourcepub fn new(
key: &[u8],
block_size: usize,
output_len: usize,
factory: F,
) -> Result<Self, CryptoError>
pub fn new( key: &[u8], block_size: usize, output_len: usize, factory: F, ) -> Result<Self, CryptoError>
Construct an HMAC instance with the given key, hash block_size, and
output_len of the underlying H.
- If
key.len() > block_sizethe key is pre-hashed using a fresh hasher fromfactory. - The padded key is zero-extended to exactly
block_sizebytes.
§Errors
Returns CryptoError::BadInput when block_size or output_len is
zero, or when key pre-hashing would write into a zero-length buffer.
Sourcepub fn mac_oneshot(&self, msg: &[u8], out: &mut [u8]) -> Result<(), CryptoError>
pub fn mac_oneshot(&self, msg: &[u8], out: &mut [u8]) -> Result<(), CryptoError>
Compute a one-shot HMAC tag over msg, writing into out.
out.len() must be at least self.output_len().
§Errors
CryptoError::BufferTooSmallifout.len() < output_len.
Sourcepub fn output_len(&self) -> usize
pub fn output_len(&self) -> usize
The hash output length in bytes.
Sourcepub fn block_size(&self) -> usize
pub fn block_size(&self) -> usize
The hash block size in bytes.
Sourcepub fn verify(&self, msg: &[u8], expected: &[u8]) -> Result<(), CryptoError>
pub fn verify(&self, msg: &[u8], expected: &[u8]) -> Result<(), CryptoError>
Constant-time verification: compute the HMAC and compare to expected.
Returns Ok(()) if they match, CryptoError::InvalidTag otherwise.
Sourcepub fn streaming_session(&self) -> StreamingHashHmacSession<H, F>where
F: Clone,
pub fn streaming_session(&self) -> StreamingHashHmacSession<H, F>where
F: Clone,
Create an incremental streaming HMAC session.
Returns a StreamingHashHmacSession that accepts data via
update() and produces the final tag via finalize().