Storing passwords: why fast hashes fail
A password store must make guessing expensive, and must assume the database will leak one day.
Remember: Use a slow, salted, memory-hard function such as Argon2id, and verify with the library's constant-time check.
A password store has two jobs: make each guess expensive for an attacker, and assume that the database will eventually leak. A hash that is fast to compute fails the first job completely.
Why SHA-256 is the wrong tool
- Speed is the problem. SHA-256 is designed to be fast, so a modern GPU tests tens of billions of guesses per second.
- No salt: the same password always gives the same hash. Precomputed tables work, and shared passwords are visible across accounts.
- No cost factor: you cannot make it slower as hardware improves.
- A hash is not encryption. Nobody decrypts it: attackers guess candidates and compare.
digest = hashlib.sha256(password.encode()).hexdigest()
ph = PasswordHasher()
ph.verify(user.password_hash, password) # raises on mismatch
Timing and enumeration
If the code returns early when the email does not exist, the response time reveals which accounts exist, even if the error message is identical. Run the hash check in both cases.
A strong hash does not replace MFA, rate limiting, or a check against known breached passwords.
Questions to ask yourself
- What happens to these hashes if the database leaks tomorrow?
- Does the response time differ between an existing and a missing account?
- Is there rate limiting and a second factor?
Practise this
- Login with SHA-256 · Authentication & cryptography