0Pricing
Flask Academy · Lesson

Hash Passwords, Never Store Plaintext

Use Werkzeug to hash and verify passwords.

Hash Passwords, Never Store Plaintext is a free Flask Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Plaintext Is a Disaster

If you store passwords as plaintext, one database leak hands attackers every account at once. The first rule of auth is simple: never save the raw password. 🔒

Hashing, Not Encrypting

You protect passwords with hashing, a one-way transform. Unlike encryption, a hash cannot be reversed back into the original password, even by you.

Werkzeug Has It Built In

Flask ships with Werkzeug, which gives you two helpers for password security. You import them straight from its security module, no extra install needed.

from werkzeug.security import generate_password_hash, check_password_hash

Hash on Sign-Up

When a user registers, run generate_password_hash on their password and store only the result. The plaintext never touches your database.

hashed = generate_password_hash("hunter2")
user.password_hash = hashed

Salt Comes Free

generate_password_hash adds a random salt for you. That is why two users with the same password get totally different stored hashes.

Verify on Login

At login you cannot un-hash anything. Instead you call check_password_hash with the stored hash and the typed password to get a True or False.

ok = check_password_hash(user.password_hash, "hunter2")

Argument Order Matters

Remember the order: the stored hash comes first, the user-supplied password second. Swapping them silently breaks every login attempt.

check_password_hash(stored_hash, typed_password)

Pick a Strong Method

By default Werkzeug uses a strong, slow algorithm on purpose. Slowness is a feature here, because it makes brute-force guessing far more expensive.

generate_password_hash(pw, method="pbkdf2:sha256")

Store the Hash, Not More

Your user table needs a single password_hash column. You never need a separate salt column, since the salt is baked into the hash string itself.

password_hash = db.Column(db.String(255))

Helper Methods on User

A clean trick is to put a set_password method on your User model so hashing lives in one place and your routes stay tidy.

def set_password(self, pw):
    self.password_hash = generate_password_hash(pw)

Never Log the Password

Even during debugging, do not print or log the raw password. A stray log line can leak credentials just as badly as a database breach can.

Quick Check

You need to confirm a login. Which call should you use?

Recap

You learned to hash with generate_password_hash, store only the result, and verify with check_password_hash. Plaintext passwords are gone for good. 🎉

Frequently asked questions

Is the “Hash Passwords, Never Store Plaintext” lesson free?

Yes — the full text of “Hash Passwords, Never Store Plaintext” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “Hash Passwords, Never Store Plaintext”?

Use Werkzeug to hash and verify passwords. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Hash Passwords, Never Store Plaintext” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Hash Passwords, Never Store Plaintext
  2. User Loader and the UserMixin
  3. login_user, logout_user, and Sessions
  4. Protect Views with login_required
← Back to Flask Academy