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:
[]
= "1.1"
SHA512 Hashing
use Hash;
// One-shot hashing
let hash = hash;
// Incremental hashing
let mut hasher = new;
hasher.update;
hasher.update;
let hash = hasher.finalize;
HMAC-SHA512
use HMAC;
// One-shot HMAC
let mac = HMACmac;
// Incremental HMAC
let mut hmac = HMACnew;
hmac.update;
hmac.update;
let mac = hmac.finalize;
// Constant-time verification (one-shot)
let expected = HMACmac;
let is_valid = HMACverify;
// Constant-time verification (streaming)
let mut hmac = HMACnew;
hmac.update;
assert!;
HKDF-SHA512
HKDF (HMAC-based Key Derivation Function) as defined in RFC 5869.
use HKDF;
// Extract a pseudorandom key from input keying material
let prk = HKDFextract;
// Expand the pseudorandom key to the desired output length
let mut output = ;
HKDFexpand;
SHA384 Hashing (when enabled)
use Hash;
let hash = hash;
HMAC-SHA384 (when enabled)
use HMAC;
let mac = HMACmac;
let expected = ; // Replace with actual expected MAC
let is_valid = HMACverify;
With Digest Trait
use Hash;
use Digest; // Requires enabling traits feature
let mut hasher = new;
hasher.update;
let result = hasher.finalize;
Building and Testing
# Build with default features
# Build with all features
# Build without SHA384 support
# Run all tests
License
This project is licensed under the ISC License.