Guide
Security
Developer Reference

Hashing and HMAC: What They Are and When to Use Them

A practical guide to integrity, authentication, and common developer misconceptions.

Last updated: Feb 18, 2026

Part of the complete guide to encryption, hashing, and encoding.

Hashing

A one-way function that produces a fixed-size fingerprint of any input. Used for integrity verification. Cannot be reversed.

Examples: SHA-256, SHA-3, MD5

HMAC

A keyed hash that proves both integrity and authenticity. Only someone with the secret key can produce or verify the correct HMAC.

Examples: HMAC-SHA256, HMAC-SHA512

Encryption

Transforms data so only authorized parties can read it. Reversible with the correct key. Provides confidentiality, not integrity.

Examples: AES, RSA, ChaCha20

Which One Do I Need?

GoalRecommended Approach
Detect if data has changedHash
Authenticate API request with shared secretHMAC
Store passwords securelyPassword hashing (bcrypt, Argon2)
Keep data secret from unauthorized partiesEncryption
Prevent tampering and prove sender identityHMAC (or digital signatures)
Verify file integrity after downloadHash (checksum)

Hashing Explained

A hash function takes input of any size and produces a fixed-size output called a digest or hash. This process is deterministic (same input always produces the same output) and one-way (you cannot recover the original input from the hash).

Even a tiny change in the input produces a completely different hash. This property, called the avalanche effect, makes hashes ideal for detecting modifications. If the hash of a file matches the expected value, you can be confident the file is unchanged.

Collisions occur when two different inputs produce the same hash. Modern cryptographic hash functions like SHA-256 make it computationally infeasible to find collisions intentionally. Older algorithms like MD5 and SHA-1 have known weaknesses and should be avoided for security-sensitive applications.

Common uses:

  • File integrity verification (checksums)
  • Cache keys and content deduplication
  • Content addressing (IPFS, Git commits)
  • Data fingerprinting for comparison

HMAC Explained

HMAC (Hash-based Message Authentication Code) combines a cryptographic hash function with a secret key. Unlike plain hashing, HMAC provides both integrity and authenticity. Only someone who knows the secret key can generate the correct HMAC for a given message.

When you receive a message with an HMAC, you can verify it by computing the HMAC yourself (using the shared secret) and comparing it to the one provided. If they match, you know the message is unmodified and came from someone with the key.

HMAC is widely used for API request signing, webhook verification, and session token validation. Services like AWS, Stripe, and GitHub use HMAC signatures to authenticate requests and webhooks.

For a direct comparison of plain SHA-256 and HMAC-SHA256 — specifically which to use for checksums vs API signing — see SHA-256 vs HMAC-SHA256.

Important: HMAC is not encryption

HMAC does not hide data. The message remains readable. HMAC only proves that the message is authentic and unmodified. If you need confidentiality, use encryption.

Hash vs HMAC vs Encryption

AspectHashHMACEncryption
PurposeIntegrity verificationIntegrity + authenticationConfidentiality
Reversible?NoNoYes (with key)
Requires secret?NoYesYes
Provides confidentiality?NoNoYes
Provides integrity?YesYesNot inherently
Provides authentication?NoYesNo
Typical use casesChecksums, deduplication, content addressingAPI signing, webhooks, message authenticationProtecting data at rest and in transit

Common Real-World Patterns

File Integrity Verification

Software distributors publish hash values (checksums) alongside downloads. After downloading, you compute the hash of the file and compare it to the published value. If they match, the file is authentic and uncorrupted.

SHA-256 is the standard choice. The hash should be obtained from a trusted source separate from the download itself.

Webhook Verification

Services like GitHub, Stripe, and Twilio send webhooks with an HMAC signature header. Your server computes the HMAC of the payload using your shared secret and compares it to the header value. This confirms the webhook is genuine.

Always use constant-time comparison to prevent timing attacks when verifying HMACs.

API Request Signing

Cloud providers like AWS use HMAC-based request signing. The client computes an HMAC over request parameters, headers, and a timestamp using their secret access key. The server verifies the signature to authenticate the request.

Timestamps prevent replay attacks by rejecting requests that are too old. For a full comparison of API authentication options (API keys, JWT, OAuth 2.0, and HMAC signing), see the API Authentication Methods guide.

Encrypted Data with Integrity

Encryption alone provides confidentiality but not integrity. Attackers might modify ciphertext without detection. The solution is authenticated encryption (like AES-GCM) or combining encryption with HMAC.

Modern best practice is to use authenticated encryption modes that handle both confidentiality and integrity in a single operation. See the Authenticated Encryption and Integrity guide for AES-GCM and ChaCha20-Poly1305 details.

Common Misconceptions

X

"Hashing hides data"

Hashing is not encryption. While you cannot reverse a hash, the original data is not "hidden." Anyone can hash the same input and get the same output.

X

"Encoding provides security"

Base64, URL encoding, and hex encoding provide zero security. They are format conversions that anyone can reverse instantly.

X

"Encryption automatically proves authenticity"

Basic encryption modes can be tampered with without detection. Use authenticated encryption or add HMAC for integrity and authenticity.

X

"HMAC is encryption"

HMAC provides authentication and integrity, not confidentiality. The message remains readable. If you need secrecy, use encryption.

X

"SHA-1 and MD5 are fine for security"

Both have known collision vulnerabilities. Use SHA-256 or SHA-3 for security applications. MD5 and SHA-1 are only acceptable for non-security checksums.

Frequently Asked Questions

Try These Tools

Related Guides and Comparisons