Documentation

HMAC rs-hmac - Hash-based Message Authentication Code

This HMAC implementation is a specific type of message authentication code (MAC) involving a cryptographic hash function in combination with a secret cryptographic key. It was developed by the National Institute of Standards and Technology (NIST).

The HMAC function implemented in this crate is compatible with any hash function present in the encompassing project.

HMAC is suitable for a range of cryptographic purposes, including verifying the data integrity and the authenticity of a message. It is typically used in data communications and is crucial for many protocols to ensure data hasn't been tampered with during transmission.

Usage

The crate provides a straightforward API. Users can create a new HMAC instance, update it with input data, and finalize to get the resultant MAC.

Example

Here is an example of how to use the HMAC function with SHA3-256 in Rust:

# use std::hash::Hasher;
# use rs_hmac::Hmac;
# use rs_sha3_256::Sha3_256State;
let mut hmac = Hmac::<Sha3_256State, 32>::new(b"my secret and secure key");
hmac.write(b"hello world");
let result = hmac.finish();
assert_eq!(result, 0xF9C0C982D2F30FE5);

Use Cases

HMAC is recommended for a variety of tasks, including:

  • Ensuring data integrity and authenticity in data communications.
  • Authenticating API requests.
  • Secure password storage.

NIST recommends HMAC for ensuring data integrity and authenticity, particularly when it is crucial to verify that data has not been tampered with during communication.