Back
+30 XP
Concept

Secure Key Derivation with PBKDF2 & Salt

Storing passwords with fast hashes like MD5 or raw SHA-256 leaves credentials defenseless against GPU rainbow table attacks. PBKDF2 applies a cryptographically random salt and thousands of HMAC iterations to deliberately slow down computation, making brute-force cracking mathematically infeasible.

python
import hashlib, os

salt = os.urandom(16)
dk = hashlib.pbkdf2_hmac('sha256', password.encode(), salt, 100000)

# Secure Key Derivation with PBKDF2 & Dynamic Salt Modern GPUs can compute over **100 billion SHA-256 hashes per second**. If you store user passwords as `sha256(password)`, an attacker with an RTX 4090 will crack an 8-character password in less than 3 minutes using precomputed lookup tables (rainbow tables). 🛡️💻 **Password-Based Key Derivation Function 2 (PBKDF2)** uses two layers of defense: 1. **Cryptographic Salt (`os.urandom(16)`)**: A unique random salt generated per user ensures that if 10,000 users all have the password `"Password123"`, every single stored hash is completely different. Rainbow tables are rendered useless. 2. **Computational Slowness (Iterations)**: By running $100,000$ or more iterations of HMAC-SHA256, verification takes $\approx 50$ milliseconds on a server (unnoticeable to a human logging in), but an attacker testing billions of passwords now faces centuries of compute time. ```python import hashlib import os salt = os.urandom(16) derived_key = hashlib.pbkdf2_hmac("sha256", password.encode(), salt, 100000, dklen=32) ``` ## Pop Quiz Why must a cryptographic salt be randomly generated per user rather than hardcoded globally? - [ ] Per-user salts guarantee that identical passwords produce completely different hash digests, neutralizing precomputed rainbow tables. - [ ] Global salts cause memory buffer overflows in GPU cracking clusters. - [ ] Random salts compress password strings by 50% using entropy algorithms. - [ ] Because Linux shadow files reject hashes with static salts. ## Real-World Challenge Implement: 1. `derive_password_key(password: str, salt: bytes = None, iterations: int = 100000) -> tuple[bytes, bytes]`: Derives a 32-byte key using PBKDF2-HMAC-SHA256 and returns `(derived_key, salt)`. 2. `verify_password_key(password: str, expected_key: bytes, salt: bytes, iterations: int = 100000) -> bool`: Recomputes the key and verifies using `hmac.compare_digest`. <details> <summary>Solution</summary> ```python import hashlib import hmac import os def derive_password_key(password: str, salt: bytes = None, iterations: int = 100000) -> tuple[bytes, bytes]: if salt is None: salt = os.urandom(16) pw_bytes = password.encode("utf-8") derived = hashlib.pbkdf2_hmac("sha256", pw_bytes, salt, iterations, dklen=32) return derived, salt def verify_password_key(password: str, expected_key: bytes, salt: bytes, iterations: int = 100000) -> bool: recomputed_key, _ = derive_password_key(password, salt=salt, iterations=iterations) return hmac.compare_digest(expected_key, recomputed_key) # Verification user_pw = "CorrectHorseBatteryStaple#2026" key_bytes, user_salt = derive_password_key(user_pw, iterations=50000) assert len(key_bytes) == 32 assert len(user_salt) == 16 # Verify valid credentials assert verify_password_key(user_pw, key_bytes, user_salt, iterations=50000) is True # Verify incorrect password assert verify_password_key("WrongPassword123", key_bytes, user_salt, iterations=50000) is False ``` </details>