Sub topic 3
Hash Functions and What Makes a Good One
This chapter assumes basic Python, hexadecimal and binary notation, bitwise operations, modulo, and dictionary or map data structures.
Hash Flooding Attacks and Seeded Hashes
A hash table maps each key to a bucket. If many attacker-controlled keys produce the same bucket, lookups become slow because the table must compare many keys. This is a hash flooding attack. An operation that normally takes near-constant time can approach linear time.
A seeded hash uses a secret, randomly chosen value for each process. The attacker cannot easily predict which inputs collide.
import hashlib
import os
seed = os.urandom(16)
def bucket(key: bytes, count: int) -> int:
digest = hashlib.blake2b(key, key=seed, digest_size=8).digest()
return int.from_bytes(digest, "big") % count
print(bucket(b"user-supplied-key", 1024))What Cryptographic Hashes Add
A cryptographic hash is designed to make several attacks impractical:
- Preimage resistance: given a hash, finding an input that produces it is infeasible.
- Second-preimage resistance: given one input, finding a different input with the same hash is infeasible.
- Collision resistance: finding any two inputs with the same hash is infeasible.
These properties require more than good bucket distribution.
When a Hash Breaks: MD5 and SHA-1
MD5 and SHA-1 have practical collision attacks. Do not use them for signatures, certificates, or security-sensitive integrity checks. A collision does not mean every use of an old hash is immediately exploitable, but it removes an important security guarantee.
Using SHA-256 to Check Integrity
SHA-256 is suitable for detecting accidental changes and for many public integrity checks.
import hashlib
data = b"release-1.0.tar.gz contents"
digest = hashlib.sha256(data).hexdigest()
print(digest)A published digest does not authenticate a file if an attacker can replace both the file and its digest. Use a signature or HMAC when the source must be trusted.
Authenticating Messages with HMAC
HMAC combines a cryptographic hash with a secret key. It produces a tag that proves the message came from someone holding that key.
import hashlib
import hmac
key = b"shared secret"
message = b"transfer=100"
tag = hmac.new(key, message, hashlib.sha256).digest()
print(hmac.compare_digest(
tag,
hmac.new(key, message, hashlib.sha256).digest()
))Storing Passwords: Salts and Slow Hashes
Use a password-hashing function such as Argon2id, scrypt, or bcrypt. A random salt makes equal passwords produce different stored values. A deliberately slow, memory-intensive function makes each guessing attempt expensive. Do not use SHA-256 alone for passwords.
Choosing a Hash for the Job
| Job | Suitable choice |
|---|---|
| Hash-table indexing | Fast hash, preferably seeded |
| Public file fingerprint | SHA-256 |
| Message authentication | HMAC-SHA-256 |
| Password storage | Argon2id, scrypt, or bcrypt |
Exercise
An attacker can submit arbitrary keys to your hash table. What protection should you consider first?
Show answer
Use a hash with a secret per-process seed so the attacker cannot precompute bucket collisions.