Skip to content

Password Hashing (Werkzeug)

Never store plaintext passwords.

If your database leaks and passwords are stored as plaintext, every user account is compromised.

diagram storing a password you can check but never read back mermaid
A hash is one-way: there is no arrow back from the stored string to the password. Verification re-runs the same function on the attempt and compares. The salt is what makes two identical passwords store differently, so a stolen database cannot be scanned for repeats.
  • Hashing: one-way (you can’t recover original password)
  • Encryption: reversible (not what you want for passwords)

Passwords should be stored as:

  • salted, slow hashes

Flask uses Werkzeug, which provides:

  • generate_password_hash()
  • check_password_hash()

Example:

python
from werkzeug.security import generate_password_hash, check_password_hash
 
hashed = generate_password_hash("my_password")
 
assert check_password_hash(hashed, "my_password") is True
assert check_password_hash(hashed, "wrong") is False

Typically your user model has:

  • password_hash column

And you never store the raw password.

  • Comparing raw passwords in code
  • Using fast hashes like MD5/SHA1 for passwords (too fast)
  • Logging passwords in debug logs

Werkzeug’s defaults are safe for most projects.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading