Skip to main content
Cryptographic Hashing, Password KDFs & HMAC
Lesson 7 of 19

Cryptographic Hashing, Password KDFs & HMAC

Explain cryptographic hash properties, distinguish SHA-2/SHA-3/BLAKE2 from broken MD5/SHA-1, store passwords safely with Argon2id and salts, and verify messages with HMAC.

Intermediate 24 min Cryptography

Learning objectives

  • Explain pre-image resistance, second pre-image resistance, collision resistance, and the avalanche effect
  • Distinguish hashing from encryption and encoding, and choose safe integrity algorithms over MD5 and SHA-1
  • Design safe password storage using per-user salts and memory-hard KDFs such as Argon2id, scrypt, or bcrypt
  • Use HMAC with constant-time comparison to authenticate messages and prevent length-extension and timing attacks

Recommended first: Symmetric Encryption: AES, Block Modes & AEAD

#Why this matters

File downloads, Git commits, TLS certificates, and login systems all rely on cryptographic hashing. Confusing hashing with encryption or encoding, or storing passwords with raw SHA-256 or MD5, turns a single read-only database leak into rapid mass account takeover.

#Hash properties and how hashing differs from encryption

A cryptographic hash H(x) maps any input to a fixed-size digest without a key. Secure hashes satisfy four properties:

  1. Pre-image resistance (one-way): Given h, finding x with H(x) = h is infeasible.
  2. Second pre-image resistance: Given x, finding x2 != x with H(x2) = H(x) is infeasible.
  3. Collision resistance: Finding any distinct x and y with H(x) = H(y) is infeasible.
  4. Avalanche effect: Flipping one input bit changes roughly 50% of the output bits.
Property Hash (SHA-256) Encryption (AES) Encoding (Base64)
Reversible? No (one-way) Yes (with key) Yes (no key)
Key needed? No Yes No
Output size Fixed (256 bits) Proportional Proportional
Primary goal Integrity Confidentiality Formatting

Verify the avalanche effect at the bit level (using XOR, not hex character matching) in OpenSSL and Python 3:

Observing SHA-256 digests and bit-level avalanche effect
$ printf 'hello' | openssl dgst -sha256
SHA2-256(stdin)= 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
$ printf 'Hello' | openssl dgst -sha256
SHA2-256(stdin)= 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969
$ python3 -c "import hashlib; a=hashlib.sha256(b'hello').digest(); b=hashlib.sha256(b'Hello').digest(); d=sum(bin(x^y).count('1') for x,y in zip(a,b)); print(f'Differing bits: {d}/256 ({d/256*100:.1f}%)')"
Differing bits: 125/256 (48.8%)

#Choosing a hash algorithm in 2026

Both MD5 (collisions since 2004) and SHA-1 (SHAttered 2017, chosen-prefix 2020) are cryptographically broken and must never be used for security or adversarial integrity checks.

Algorithm Output Hex length 2026 Status Recommended use
MD5 128 bits 32 hex Broken Non-security legacy checksums only
SHA-1 160 bits 40 hex Broken / Deprecated Never use for security
SHA-256 / SHA-512 256 / 512 bits 64 / 128 hex Secure File integrity, TLS, signatures, HMAC
SHA-3 (Keccak) 256 / 512 bits 64 / 128 hex Secure Sponge-based alternative to SHA-2
BLAKE2b / BLAKE2s 256 / 512 bits 64 / 128 hex Secure High-speed integrity, Argon2, WireGuard

In CTF challenges, digest format identifies the algorithm: 32 hex chars = MD5, 40 hex = SHA-1, 64 hex = SHA-256, $2b$12$... = bcrypt, and $argon2id$... = Argon2id.

#Password storage: salts and memory-hard KDFs

Fast hashes like SHA-256 run at billions of guesses per second on GPUs. Without a unique random salt per user, identical passwords produce identical digests, enabling instant rainbow table lookups.

Fast Cryptographic Hash vs Password KDF
Fast Hash (SHA-256, SHA-3, BLAKE2)
  • Billions of hashes per second on GPUs
  • No built-in salt; identical inputs match
  • Built for file integrity, HMAC, signatures
Password KDF (Argon2id, scrypt, bcrypt)
  • Deliberately slow and tunable (time / memory)
  • Generates a unique random salt per user
  • Built to resist GPU cracking and rainbow tables
  1. RegisterGenerate unique random salt for the user
  2. HashRun Argon2id or bcrypt over (password, salt)
  3. StoreSave encoded string with parameters, salt, and digest
  4. VerifyRe-hash login input with stored salt and compare in constant time

OWASP recommends Argon2id first because it combines time cost with memory hardness (e.g. memory_cost=65536 KiB, or 64 MB) to resist GPU and side-channel attacks. scrypt is also memory-hard; bcrypt remains acceptable for existing systems (4 KB memory, 72-byte input limit). Where FIPS modules are required, use PBKDF2-HMAC-SHA256 with at least 600,000 iterations per OWASP guidance.

python
import hmac, hashlib
from argon2 import PasswordHasher

ph = PasswordHasher(time_cost=3, memory_cost=65536, parallelism=2, hash_len=32)
stored_hash = ph.hash("correct-horse-battery-staple")
ph.verify(stored_hash, "correct-horse-battery-staple")

def verify_webhook(key: bytes, message: bytes, received_mac: bytes) -> bool:
    expected = hmac.new(key, message, hashlib.sha256).digest()
    return hmac.compare_digest(expected, received_mac)

#HMAC and message authentication

A plain hash only checks accidental corruption. HMAC (RFC 2104) combines a hash with a secret key K: HMAC(K, M) = H((K XOR opad) || H((K XOR ipad) || M)). Without encrypting M, HMAC verifies both integrity and authenticity, and stops length-extension attacks that break naive SHA-256(secret || message). Always compare MACs with hmac.compare_digest rather than == to prevent timing attacks.

#Defending / Common mistakes

  • Using MD5 or SHA-1 for security. Migrate all integrity checks and signatures to SHA-256, SHA-512, SHA-3, or BLAKE2.
  • Storing passwords with fast hashes or a static salt. Use Argon2id, scrypt, or bcrypt so every user receives a unique random salt automatically.
  • Building a custom MAC as SHA-256(secret + message). Use standard hmac.new(key, message, hashlib.sha256) to avoid length-extension attacks.
  • Comparing MACs with ==. Always use constant-time hmac.compare_digest.

#Summary

  • Cryptographic hashes are keyless, one-way, fixed-size functions with pre-image, second pre-image, collision resistance, and the avalanche effect.
  • MD5 and SHA-1 are broken; use SHA-256, SHA-512, SHA-3, or BLAKE2 for integrity.
  • Store passwords with salted, memory-hard KDFs like Argon2id (or scrypt/bcrypt), never fast hashes.
  • Use HMAC with hmac.compare_digest for keyed integrity and authenticity without encryption.

Lecture presentations

Slides on this topic: view them in the browser or download the PDF.

Hash Functions

MD5 · SHA-1 · SHA-2 · SHA-3 and collisions

Slides: 25 PDF · 1.2 MB

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge