Skip to content
CoreSource checked

HMAC

Authenticates messages by combining a hash function with a shared secret key.

Quick Facts

Compact metadata for implementation and discovery.

Category
Cryptography
Difficulty
Intermediate
Family
Message Authentication
Tier
Core
Status
Verified
Review
Source Checked
Sources
1
Formulas
1
Relationships
7
Code example
Not ready
Visualization
Ready
Aliases
Hash-Based Message Authentication Code

Overview

HMAC wraps a cryptographic hash with inner and outer keyed hashes to verify message integrity and authenticity.

Core Concepts

security primitivemessage authentication codehash functionsecurity assumptionmessage authenticationkey materialmessage transformationmisuse resistance

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(n)

Average

O(n)

Worst

O(n)

Space

O(1)

When to Use

  • Use HMAC when you need to compute a keyed message digest.
  • Use it for aPI request signing with inputs like shared secret key.
  • Use it only through reviewed libraries when the security goal matches the primitive.
  • Use it when the Message Authentication tradeoff is acceptable for the problem size.

When Not to Use

  • Avoid custom implementations for production security or when key, nonce, and padding rules are unclear.
  • Avoid it when the required output is not message authentication code.

What It Does

  • Computes a keyed message digest
  • Lets receivers verify that the message was not changed

Use Cases

  • API request signing
  • Message authentication
  • Token integrity checks

Inputs

  • Shared secret key
  • Message
  • Hash function

Outputs

  • Message authentication code

Key Formulas (1)

HMAC structure

HMAC = H((key xor opad) || H((key xor ipad) || message))
HMAC = H((key xor opad) || H((key xor ipad) || message))

Combines a secret key and hash function to authenticate a message.

Pseudocode

  1. 1.Normalize the key to the hash block size
  2. 2.Hash the inner pad with the message
  3. 3.Hash the outer pad with the inner hash
  4. 4.Send or compare the final code

Canonical Example

Input
message = 'pay 10' with configured keys, nonces, or public parameters
Output
ciphertext, tag, digest, signature, proof, or verification result
Explanation
The primitive applies its defined arithmetic, hashing, or block operation without inventing extra protocol steps.

Implementation Notes

  • Keep the input contract explicit: Shared secret key; Message; Hash function.
  • Choose hash and equality behavior together so equal keys land in the same lookup path.
  • Validate parameters, key sizes, nonces, salts, and error handling against the library's recommended API.
  • Use reviewed libraries and protocol specifications for production security-sensitive code.

Common Pitfalls

  • HMAC provides authentication, not encryption.
  • Using an unstable or poorly distributed hash function can create clustered buckets or wrong comparisons.
  • Confusing encryption, hashing, signatures, and authentication codes as interchangeable tools.
  • Handle empty, invalid, and boundary inputs before trusting message authentication code.

Interactive Mechanism Walkthrough

Graph Frontier & Edge Relaxation

Show a message passing through inner and outer keyed hash layers.

HMAC mechanism walkthrough step 1 of 4suvwxt
Step 1 of 4: Normalize the key to the hash block size
keyed-hash

Notes

HMAC operates in O(n) time and O(1) space within the Message Authentication family. Key governing relation: HMAC = H((key xor opad) || H((key xor ipad) || message)). Often compared with SHA-256 and ChaCha20.

Sources (1)

What each reference supports for this entry.

RFC 2104

Documentation

IETF reference for HMAC keyed-hash message authentication.

Open source

Explore Next

Good next jumps from the relationship graph, ranked for useful discovery.

Used byHKDF

HKDF is built from HMAC extract and expand steps.

HMAC can be instantiated with SHA-256 as its underlying hash.

Related Algorithms

Tags

CryptographyMessage AuthenticationHashingSecurity