Password Hashing (bcrypt/argon2)
Why must passwords be hashed with a slow KDF (bcrypt/scrypt/argon2) instead of plain SHA-256?
Answers use simple, clear English.
Audio N/AQuick interview answer
Fast hashes let attackers try billions of guesses offline after a leak. Slow, memory-hard KDFs with unique salts make brute force expensive. Never store reversible encryption for passwords.
Detailed answer
Fast hashes let attackers try billions of guesses offline after a leak. Slow, memory-hard KDFs with unique salts make brute force expensive. Never store reversible encryption for passwords. Prefer argon2id or bcrypt with appropriate cost factors; add pepper only with clear key management. SHA-256 is fine for integrity checksums, not password storage.
Full explanation
SHA-256 is fine for integrity checksums, not password storage.
Real example & use case
Auth service stores argon2id hashes so a DB dump does not instantly yield plaintext passwords.
Pros & cons
Pros: raises attacker cost. Cons: CPU cost on login — tune parameters; DoS via expensive hashes needs rate limits.
Code example
import bcrypt
def hash_password(pw: str) -> bytes:
return bcrypt.hashpw(pw.encode(), bcrypt.gensalt(rounds=12))
def verify(pw: str, hashed: bytes) -> bool:
return bcrypt.checkpw(pw.encode(), hashed)Practice code · python (view only · no execution)
import bcrypt
def hash_password(pw: str) -> bytes:
return bcrypt.hashpw(pw.encode(), bcrypt.gensalt(rounds=12))
def verify(pw: str, hashed: bytes) -> bool:
return bcrypt.checkpw(pw.encode(), hashed)