pub struct Hmac<H: Default + HashAlgorithm, const OUTPUT_SIZE: usize> { /* private fields */ }Expand description
Hmac<H: Default + HashAlgorithm, const OUTPUT_SIZE: usize> is a generic struct that provides the HMAC (Hash-based
Message Authentication Code) in Rust.
In the context of cryptographic hashing, HMAC is an algorithm that combines a specified Hash function (H) and a
secret cryptographic key to convert input data into a fixed-size sequence of bytes. The HMAC is responsible for
maintaining the internal state of the hashing process and providing methods to add more data and retrieve the
resultant Message Authentication Code (MAC).
The Hmac struct is implemented using any hash function available in this crate, making it a flexible choice for
HMAC operations.
§Examples
The following examples demonstrate using Hmac with both Hash and Hasher, and illustrate the difference between
these approaches:
use rs_sha3_512::Sha3_512State;
let data = b"hello";
let key = b"my secret and secure key";
// Using Hash
let mut hmac_hash = Hmac::<Sha3_512State, 64>::new(key);
data.hash(&mut hmac_hash);
let result_via_hash = hmac_hash.finish();
// Using Hasher
let mut hmac_hasher = Hmac::<Sha3_512State, 64>::new(key);
hmac_hasher.write(data);
let result_via_hasher = hmac_hasher.finish();
// Simulating the Hash inners
let mut hmac_simulate = Hmac::<Sha3_512State, 64>::new(key);
hmac_simulate.write_usize(data.len());
hmac_simulate.write(data);
let simulated_hash_result = hmac_simulate.finish();
assert_ne!(result_via_hash, result_via_hasher);
assert_eq!(result_via_hash, simulated_hash_result);Note that in the context of HMAC, the hash operation applies a different set of operations than the hasher’s
write, which results in different outcomes.
Implementations§
Source§impl<H, const OUTPUT_SIZE: usize> Hmac<H, OUTPUT_SIZE>where
H: BytesLen + Default + HashAlgorithm<Output = ByteArrayWrapper<OUTPUT_SIZE>>,
<H as HashAlgorithm>::Output: From<H>,
ByteArrayWrapper<OUTPUT_SIZE>: From<H>,
impl<H, const OUTPUT_SIZE: usize> Hmac<H, OUTPUT_SIZE>where
H: BytesLen + Default + HashAlgorithm<Output = ByteArrayWrapper<OUTPUT_SIZE>>,
<H as HashAlgorithm>::Output: From<H>,
ByteArrayWrapper<OUTPUT_SIZE>: From<H>,
Sourcepub fn new(key: &[u8]) -> Self
pub fn new(key: &[u8]) -> Self
Creates a new HMAC context with the given key.
This method initializes a new HMAC context using the provided key. If the key is longer than the block size of the hash algorithm, it is hashed, and the hash is used as the key. If the key is shorter than the block size of the hash algorithm, it is padded with zeros.
The key is split into two halves - one for the inner hash and one for the outer hash. The inner hash is padded
with 0x36 and the outer hash is padded with 0x5c.
§Arguments
key- A byte slice that holds the key.
§Returns
A new HMAC context.
§Example
use rs_hasher_ctx::HasherContext;
use rs_hmac::Hmac;
use rs_sha3_384::Sha3_384State;
let key = b"my secret and secure key";
let mut hmac = Hmac::<Sha3_384State, 48>::new(key);
hmac.write(b"data");
let u64result = hmac.finish();
let bytes_result = HasherContext::finish(&mut hmac);
assert_eq!(u64result, 0xCD06EBA676832C12);
assert_eq!(
bytes_result,
[
0xC1, 0x4C, 0x21, 0xF6, 0x5A, 0xCA, 0x45, 0x19, 0x91, 0xF3, 0xAB, 0x87, 0x76, 0x6E, 0x7C, 0xDF, 0xC4, 0x50,
0x6A, 0x18, 0xE7, 0x08, 0xA4, 0x64, 0xFA, 0x0E, 0xCD, 0x4C, 0xC0, 0x97, 0x32, 0xFA, 0x5F, 0x8A, 0x8A, 0x33,
0x26, 0x0F, 0xE2, 0x65, 0x9C, 0xC3, 0xBF, 0x6E, 0xD1, 0x5E, 0x16, 0xEB
]
);Sourcepub fn digest(key: &[u8], msg: &[u8]) -> ByteArrayWrapper<OUTPUT_SIZE>
pub fn digest(key: &[u8], msg: &[u8]) -> ByteArrayWrapper<OUTPUT_SIZE>
Computes the HMAC of a message with a key.
This method calculates the HMAC of the provided message using the key from the HMAC context.
§Arguments
key- A byte slice that holds the key.msg- A byte slice that holds the message.
§Returns
The HMAC of the message as a byte array wrapper.
§Example
use rs_hmac::Hmac;
use rs_sha1::{Sha1Hasher, Sha1State};
let key = b"key";
let msg = b"The quick brown fox jumps over the lazy dog";
let resulting_sha1state = Hmac::<Sha1State, 20>::digest(key, msg);
assert_eq!(format!("{:02x}", resulting_sha1state), "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9");Trait Implementations§
Source§impl<H: Clone + Default + HashAlgorithm, const OUTPUT_SIZE: usize> Clone for Hmac<H, OUTPUT_SIZE>
impl<H: Clone + Default + HashAlgorithm, const OUTPUT_SIZE: usize> Clone for Hmac<H, OUTPUT_SIZE>
Source§impl<H: Debug + Default + HashAlgorithm, const OUTPUT_SIZE: usize> Debug for Hmac<H, OUTPUT_SIZE>
impl<H: Debug + Default + HashAlgorithm, const OUTPUT_SIZE: usize> Debug for Hmac<H, OUTPUT_SIZE>
Source§impl<H: Default + Default + HashAlgorithm, const OUTPUT_SIZE: usize> Default for Hmac<H, OUTPUT_SIZE>
impl<H: Default + Default + HashAlgorithm, const OUTPUT_SIZE: usize> Default for Hmac<H, OUTPUT_SIZE>
Source§impl<H: Hash + Default + HashAlgorithm, const OUTPUT_SIZE: usize> Hash for Hmac<H, OUTPUT_SIZE>
impl<H: Hash + Default + HashAlgorithm, const OUTPUT_SIZE: usize> Hash for Hmac<H, OUTPUT_SIZE>
Source§impl<H, const OUTPUT_SIZE: usize> Hasher for Hmac<H, OUTPUT_SIZE>where
H: Default + HashAlgorithm,
<H as HashAlgorithm>::Output: From<H>,
ByteArrayWrapper<OUTPUT_SIZE>: From<H>,
impl<H, const OUTPUT_SIZE: usize> Hasher for Hmac<H, OUTPUT_SIZE>where
H: Default + HashAlgorithm,
<H as HashAlgorithm>::Output: From<H>,
ByteArrayWrapper<OUTPUT_SIZE>: From<H>,
1.26.0 · Source§fn write_u128(&mut self, i: u128)
fn write_u128(&mut self, i: u128)
u128 into this hasher.1.3.0 · Source§fn write_usize(&mut self, i: usize)
fn write_usize(&mut self, i: usize)
usize into this hasher.1.26.0 · Source§fn write_i128(&mut self, i: i128)
fn write_i128(&mut self, i: i128)
i128 into this hasher.1.3.0 · Source§fn write_isize(&mut self, i: isize)
fn write_isize(&mut self, i: isize)
isize into this hasher.Source§fn write_length_prefix(&mut self, len: usize)
fn write_length_prefix(&mut self, len: usize)
hasher_prefixfree_extras)