A small, self-contained implementation of SHA512, HMAC-SHA512, SHA384, and HMAC-SHA384 in Rust.
- Minimal dependencies
no_std
compatible for embedded systems- Small code size with optional size optimizations
- Optional support for the
Digest
trait from thedigest
crate - Constant-time verification for HMAC results to prevent timing attacks
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)traits
: Enables support for theDigest
trait from thedigest
cratetraits09
: Support fordigest
crate v0.9.xtraits010
: Support fordigest
crate v0.10.x
Add this to your Cargo.toml
:
[dependencies]
hmac-sha512 = "1.1.6"
use hmac_sha512::Hash;
// Compute SHA512 hash
let hash = Hash::hash(b"message");
use hmac_sha512::HMAC;
// Compute HMAC-SHA512
let mac = HMAC::mac(b"message", b"key");
// Verify HMAC-SHA512
let expected = [0u8; 64]; // Replace with actual expected MAC
let is_valid = HMAC::verify(b"message", b"key", &expected);
use hmac_sha512::sha384::Hash;
// Compute SHA384 hash
let hash = Hash::hash(b"message");
use hmac_sha512::sha384::HMAC;
// Compute HMAC-SHA384
let mac = HMAC::mac(b"message", b"key");
// Verify HMAC-SHA384
let expected = [0u8; 48]; // Replace with actual expected MAC
let is_valid = HMAC::verify(b"message", b"key", &expected);
use hmac_sha512::Hash;
use digest::Digest; // Requires the digest crate
let mut hasher = Hash::new();
hasher.update(b"message");
let result = hasher.finalize();
# Build with default features
cargo build
# Build with release optimizations
cargo build --release
# Build with specific features
cargo build --features="traits"
cargo build --features="opt_size"
cargo build --no-default-features # Excludes SHA384 support
# Run all tests
cargo test
This project is licensed under the ISC License.