Crate hmac_sha256

Source
Expand description

A small, self-contained SHA256, HMAC-SHA256, and HKDF-SHA256 implementation (C) Frank Denis <fdenis [at] fastly [dot] com>

This library provides a lightweight implementation of SHA-256, HMAC-SHA256, and HKDF-SHA256 cryptographic functions with no external dependencies (unless the traits feature is enabled).

§Features

  • traits: Enables support for the Digest trait from the digest crate
  • opt_size: Enables size optimizations (reduces .text section size by ~75% with ~16% performance cost)

§Examples

// Calculate a SHA-256 hash
let hash = hmac_sha256::Hash::hash(b"hello world");

// Verify a hash in one shot
let expected = hmac_sha256::Hash::hash(b"hello world");
assert!(hmac_sha256::Hash::verify(b"hello world", &expected));

// Create an HMAC-SHA256
let mac = hmac_sha256::HMAC::mac(b"message", b"key");

// Verify an HMAC-SHA256 in one shot
let expected = hmac_sha256::HMAC::mac(b"message", b"key");
assert!(hmac_sha256::HMAC::verify(b"message", b"key", &expected));

// Use HKDF-SHA256 for key derivation
let prk = hmac_sha256::HKDF::extract(b"salt", b"input key material");
let mut output = [0u8; 32];
hmac_sha256::HKDF::expand(&mut output, prk, b"context info");

Structs§

HKDF
HMAC-based Key Derivation Function (HKDF) implementation using SHA-256.
HMAC
HMAC-SHA256 implementation.
Hash
SHA-256 hash implementation.