#sha-2 #sha-512 #hmac #crypto #cryptographic-hashes

no-std hmac-sha512

A small, self-contained SHA512, HMAC-SHA512, SHA384 and HMAC-SHA384 implementation

17 releases (9 stable)

new 1.1.7 May 19, 2025
1.1.6 Dec 9, 2024
1.1.5 Jun 14, 2023
1.1.4 Nov 29, 2022
0.1.6 Jun 23, 2020

#142 in Cryptography

Download history 243013/week @ 2025-02-01 272830/week @ 2025-02-08 182799/week @ 2025-02-15 157033/week @ 2025-02-22 458336/week @ 2025-03-01 410849/week @ 2025-03-08 592080/week @ 2025-03-15 909004/week @ 2025-03-22 293286/week @ 2025-03-29 311217/week @ 2025-04-05 192874/week @ 2025-04-12 129408/week @ 2025-04-19 129232/week @ 2025-04-26 173525/week @ 2025-05-03 152263/week @ 2025-05-10 116667/week @ 2025-05-17

594,412 downloads per month
Used in 89 crates (19 directly)

ISC license

31KB
736 lines

hmac-sha512

A small, self-contained implementation of SHA512, HMAC-SHA512, SHA384, and HMAC-SHA384 in Rust.

Crates.io Documentation License: ISC

Features

  • Minimal dependencies
  • no_std compatible for embedded systems
  • Small code size with optional size optimizations
  • Optional support for the Digest trait from the digest crate
  • Constant-time verification for HMAC results to prevent timing attacks

Optional Features

  • sha384 (enabled by default): Includes SHA384 and HMAC-SHA384 implementations
  • opt_size: Optimizes for binary size at a slight performance cost (reduces text section size by ~75% with ~16% performance hit)
  • traits: Enables support for the Digest trait from the digest crate
    • traits09: Support for digest crate v0.9.x
    • traits010: Support for digest crate v0.10.x

Usage

Add this to your Cargo.toml:

[dependencies]
hmac-sha512 = "1.1.6"

SHA512 Hashing

use hmac_sha512::Hash;

// Compute SHA512 hash
let hash = Hash::hash(b"message");

HMAC-SHA512

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);

SHA384 Hashing (when enabled)

use hmac_sha512::sha384::Hash;

// Compute SHA384 hash
let hash = Hash::hash(b"message");

HMAC-SHA384 (when enabled)

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);

With Digest Trait (when enabled)

use hmac_sha512::Hash;
use digest::Digest;  // Requires the digest crate

let mut hasher = Hash::new();
hasher.update(b"message");
let result = hasher.finalize();

Building and Testing

# 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

License

This project is licensed under the ISC License.

Dependencies

~145KB