Search

Lib.rs

› Cryptography
#hash #mac

no-std hmac

Generic implementation of Hash-based Message Authentication Code (HMAC)

Owned by Tony Arcieri, Artyom Pavlov, RustCrypto.

  • Audit
  • Install
  • API reference
  • GitHub repo (rustcrypto)

25 releases

Uses new Rust 2024

0.13.0 Mar 29, 2026
0.13.0-rc.5 Feb 2, 2026
0.13.0-rc.3 Nov 5, 2025
0.13.0-rc.0 May 29, 2025
0.0.1 Oct 21, 2016

#544 in Cryptography

Download history 3683917/week @ 2026-01-18 3779947/week @ 2026-01-25 4102767/week @ 2026-02-01 4227549/week @ 2026-02-08 4159008/week @ 2026-02-15 4551840/week @ 2026-02-22 5639176/week @ 2026-03-01 6076944/week @ 2026-03-08 5603422/week @ 2026-03-15 5333309/week @ 2026-03-22 5638971/week @ 2026-03-29 5850707/week @ 2026-04-05 6228063/week @ 2026-04-12 6357783/week @ 2026-04-19 6774462/week @ 2026-04-26 7132247/week @ 2026-05-03

26,990,466 downloads per month
Used in 22,200 crates (2,442 directly)

MIT/Apache

46KB
359 lines

RustCrypto: HMAC

crate Docs Build Status Apache2/MIT licensed Rust Version Project Chat

Generic implementation of Hash-based Message Authentication Code (HMAC).

To use it you will need a cryptographic hash function implementation which implements the digest crate traits. You can find compatible crates (e.g. sha2) in the RustCrypto/hashes repository.

This crate provides four HMAC implementations: Hmac, HmacReset, SimpleHmac, and SimpleHmacReset.

The first two types are buffered wrappers around block-level block_api::HmacCore and block_api::HmacResetCore types respectively. Internally they uses efficient state representation, but work only with hash functions which expose block-level API and consume blocks eagerly (e.g. they will not work with the BLAKE2 family of hash functions).

On the other hand, SimpleHmac and SimpleHmacReset are a bit less efficient, but work with all hash functions which implement the Digest trait.

Hmac and SimpleHmac do not support resetting MAC state (i.e. they do not implement the Reset and FixedOutputReset traits). Use HmacReset or SimpleHmacReset if you want to reuse MAC state.

Examples

Let us demonstrate how to use HMAC using the SHA-256 hash function implemented in the sha2 crate.

In the following examples Hmac is interchangeable with SimpleHmac.

To get authentication code:

use sha2::Sha256;
use hmac::{Hmac, KeyInit, Mac};
use hex_literal::hex;

// Create alias for HMAC-SHA256
type HmacSha256 = Hmac<Sha256>;

let mut mac = HmacSha256::new_from_slice(b"my secret and secure key")
    .expect("HMAC can take key of any size");
mac.update(b"input message");

// `result` has type `CtOutput` which is a thin wrapper around array of
// bytes for providing constant time equality check
let result = mac.finalize();
// To get underlying array use `into_bytes`, but be careful, since
// incorrect use of the code value may permit timing attacks which defeats
// the security provided by the `CtOutput`
let code_bytes = result.into_bytes();
let expected = hex!("
    97d2a569059bbcd8ead4444ff99071f4
    c01d005bcefe0d3567e1be628e5fdcd9
");
assert_eq!(code_bytes[..], expected[..]);

To verify the message:

use sha2::Sha256;
use hmac::{Hmac, KeyInit, Mac};
use hex_literal::hex;

type HmacSha256 = Hmac<Sha256>;

let mut mac = HmacSha256::new_from_slice(b"my secret and secure key")
    .expect("HMAC can take key of any size");

mac.update(b"input message");

let code_bytes = hex!("
    97d2a569059bbcd8ead4444ff99071f4
    c01d005bcefe0d3567e1be628e5fdcd9
");
// `verify_slice` will return `Ok(())` if code is correct, `Err(MacError)` otherwise
mac.verify_slice(&code_bytes[..]).unwrap();

Block and input sizes

Usually it is assumed that block size is larger than output size. Due to the generic nature of the implementation, we must handle cases when this assumption does not hold. This is done by truncating hash output to the hash block size if needed.

License

Licensed under either of:

  • Apache License, Version 2.0
  • MIT license

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1MB
~21K SLoC

  • digest 0.11.2+mac
  • dev hex-literal 1.0
  • dev md-5 0.11
  • dev sha1 0.11
  • dev sha2 0.11
  • dev streebog 0.11

Other feature

  • zeroize
Related: chacha20, ed25519, pbkdf2, cipher, sha3, pkcs8, digest, rsa, belt-mac, cbc-mac, cmac, daa, pmac, retail-mac, cbc, belt-hash, belt-block, belt-kwp, belt-dwp, belt-ctr
See also: totp-rs, hmac-sha512, hmac-sha256, libcrux-hmac, crypt_guard, totp_rfc6238, yubihsm, hmac-sha1-compact, lockstitch

Lib.rs is an unofficial list of Rust/Cargo crates, created by kornelski. It contains data from multiple sources, including heuristics, and manually curated data. Content of this page is not necessarily endorsed by the authors of the crate. This site is not affiliated with nor endorsed by the Rust Project. If something is missing or incorrect, please file a bug.