#sha-256 #hmac #sha-2 #hkdf

no-std hmac-sha256

A small, self-contained SHA256, HMAC-SHA256, and HKDF-SHA256 implementation

23 releases (15 stable)

1.1.13 Jan 22, 2026
1.1.12 May 19, 2025
1.1.8 Dec 9, 2024
1.1.7 Jun 14, 2023
0.1.1 Apr 10, 2019

#60 in Cryptography

Download history 163033/week @ 2025-10-19 186251/week @ 2025-10-26 180115/week @ 2025-11-02 170544/week @ 2025-11-09 165334/week @ 2025-11-16 100138/week @ 2025-11-23 75419/week @ 2025-11-30 70319/week @ 2025-12-07 65711/week @ 2025-12-14 37168/week @ 2025-12-21 33054/week @ 2025-12-28 76978/week @ 2026-01-04 82012/week @ 2026-01-11 88040/week @ 2026-01-18 98399/week @ 2026-01-25 133302/week @ 2026-02-01

409,842 downloads per month
Used in 440 crates (66 directly)

ISC license

36KB
595 lines

rust-hmac-sha256

A small, self-contained SHA256, HMAC-SHA256, and HKDF-SHA256 implementation in Rust with no_std support.

Features

  • Pure Rust implementation
  • No external dependencies (unless optional features are enabled)
  • no_std compatible
  • Both one-shot and streaming APIs
  • Constant-time verification to prevent timing attacks
  • HKDF key derivation (extraction and expansion)

Optional Features

  • traits: Enable support for the Digest trait from the digest crate (both version 0.9.0 and 0.10.7)
  • opt_size: Enable size optimizations. Based on benchmarks, the .text section size is reduced by 75%, at the cost of approximately 16% performance.

Usage Examples

SHA-256

// One-shot hashing
let hash = hmac_sha256::Hash::hash(b"hello world");

// Incremental hashing
let mut hasher = hmac_sha256::Hash::new();
hasher.update(b"hello ");
hasher.update(b"world");
let hash = hasher.finalize();

// Constant-time verification
let expected = hmac_sha256::Hash::hash(b"hello world");
let mut hasher = hmac_sha256::Hash::new();
hasher.update(b"hello world");
assert!(hasher.verify(&expected));

HMAC-SHA256

// One-shot HMAC
let mac = hmac_sha256::HMAC::mac(b"message", b"key");

// Incremental HMAC
let mut hmac = hmac_sha256::HMAC::new(b"key");
hmac.update(b"message part 1");
hmac.update(b"message part 2");
let mac = hmac.finalize();

// Constant-time verification
let expected = hmac_sha256::HMAC::mac(b"message", b"key");
let mut hmac = hmac_sha256::HMAC::new(b"key");
hmac.update(b"message");
assert!(hmac.verify(&expected));

HKDF-SHA256

// Extract a pseudorandom key from input keying material
let prk = hmac_sha256::HKDF::extract(b"salt", b"input key material");

// Expand the pseudorandom key to the desired output length
let mut output = [0u8; 64];
hmac_sha256::HKDF::expand(&mut output, prk, b"application info");

Dependencies

~175KB