Flask Academy · 课时

哈希密码,绝不存储明文

使用 Werkzeug 对密码进行哈希和验证

第 1 / 4 课13 个步骤

哈希密码,绝不存储明文 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flask Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flask Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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. 🎉

免费开始

用 AI 导师学习 Python — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
30
课程
120

常见问题解答

「哈希密码,绝不存储明文」课时是免费的吗?

是的 — 「哈希密码,绝不存储明文」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。

「哈希密码,绝不存储明文」这节课中我会学到什么?

使用 Werkzeug 对密码进行哈希和验证 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Flask Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「哈希密码,绝不存储明文」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Flask Academy 课中编写并运行代码吗?

能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 哈希密码,绝不存储明文
  2. 用户加载器与 UserMixin
  3. login_user、logout_user 与会话
  4. 使用 login_required 保护视图
← 返回 Flask Academy