22 releases (14 stable)
| 1.1.12 | Feb 13, 2026 |
|---|---|
| 1.1.8 | Jan 22, 2026 |
| 1.1.7 | May 19, 2025 |
| 1.1.6 | Dec 9, 2024 |
| 0.1.6 | Jun 23, 2020 |
#35 in Cryptography
627,719 downloads per month
Used in 173 crates
(24 directly)
46KB
1K
SLoC
hmac-sha512
A small, self-contained SHA512, HMAC-SHA512, HKDF-SHA512, SHA384, and HMAC-SHA384 implementation in Rust.
Features
- Pure Rust implementation
- Minimal dependencies
no_stdcompatible for embedded systems- Both one-shot and streaming APIs for HMAC
- HKDF key derivation (RFC 5869)
- Constant-time verification to prevent timing attacks
- Optional size optimizations
Optional Features
sha384(enabled by default): Includes SHA384 and HMAC-SHA384 implementationsopt_size: Optimizes for binary size at a slight performance cost (reduces text section size by ~75% with ~16% performance hit)traits09: Support forDigesttrait fromdigestcrate v0.9.xtraits010: Support forDigesttrait fromdigestcrate v0.10.xtraits011: Support forDigesttrait fromdigestcrate v0.11.x
Usage
Add this to your Cargo.toml:
[dependencies]
hmac-sha512 = "1.1"
SHA512 Hashing
use hmac_sha512::Hash;
// One-shot hashing
let hash = Hash::hash(b"message");
// Incremental hashing
let mut hasher = Hash::new();
hasher.update(b"hello ");
hasher.update(b"world");
let hash = hasher.finalize();
HMAC-SHA512
use hmac_sha512::HMAC;
// One-shot HMAC
let mac = HMAC::mac(b"message", b"key");
// Incremental HMAC
let mut hmac = HMAC::new(b"key");
hmac.update(b"message part 1");
hmac.update(b"message part 2");
let mac = hmac.finalize();
// Constant-time verification (one-shot)
let expected = HMAC::mac(b"message", b"key");
let is_valid = HMAC::verify(b"message", b"key", &expected);
// Constant-time verification (streaming)
let mut hmac = HMAC::new(b"key");
hmac.update(b"message");
assert!(hmac.finalize_verify(&expected));
HKDF-SHA512
HKDF (HMAC-based Key Derivation Function) as defined in RFC 5869.
use hmac_sha512::HKDF;
// Extract a pseudorandom key from input keying material
let prk = HKDF::extract(b"salt", b"input key material");
// Expand the pseudorandom key to the desired output length
let mut output = [0u8; 128];
HKDF::expand(&mut output, prk, b"application info");
SHA384 Hashing (when enabled)
use hmac_sha512::sha384::Hash;
let hash = Hash::hash(b"message");
HMAC-SHA384 (when enabled)
use hmac_sha512::sha384::HMAC;
let mac = HMAC::mac(b"message", b"key");
let expected = [0u8; 48]; // Replace with actual expected MAC
let is_valid = HMAC::verify(b"message", b"key", &expected);
With Digest Trait
use hmac_sha512::Hash;
use digest::Digest; // Requires enabling traits feature
let mut hasher = Hash::new();
hasher.update(b"message");
let result = hasher.finalize();
Building and Testing
# Build with default features
cargo build
# Build with all features
cargo build --all-features
# Build without SHA384 support
cargo build --no-default-features
# Run all tests
cargo test --all-features
License
This project is licensed under the ISC License.
Dependencies
~180KB