Secure Coding & OWASP Top 10 for Backend · 课时

安全的密码存储与凭据恢复

学习如何正确哈希密码、防御凭据攻击,并构建安全的密码重置和账户恢复流程。

第 4 / 4 课13 个步骤

安全的密码存储与凭据恢复 是 CoddyKit 上的免费 Secure Coding & OWASP Top 10 for Backend 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Secure Coding & OWASP Top 10 for Backend 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Secure Coding & OWASP Top 10 for Backend 课程共包含 4 节课。

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

Why Hashing Matters

Storing passwords in plaintext or with weak encoding means a single database breach exposes every user. Passwords must be stored as salted hashes using a slow, purpose-built algorithm.

This lesson covers correct hashing and secure recovery flows.

Hash, Do Not Encrypt

Passwords should be hashed, not encrypted. Encryption is reversible; if the key leaks, all passwords are exposed. Hashing is one-way: you verify by hashing the input and comparing.

  • Never use reversible encryption for passwords
  • Never use fast hashes like MD5 or SHA-1 alone

Salting

A salt is a unique random value added to each password before hashing. Salts ensure two users with the same password get different hashes, defeating precomputed rainbow tables.

Modern algorithms generate and store the salt for you.

Choosing an Algorithm

Use a deliberately slow, memory-hard algorithm: Argon2 (preferred), bcrypt, or scrypt. Their cost factor makes brute-force attacks expensive even with stolen hashes.

Hashing in Code

Here is a conceptual hashing and verification flow using a bcrypt-style API.

import bcrypt

def hash_password(plain):
    return bcrypt.hashpw(plain.encode(), bcrypt.gensalt(rounds=12))

def verify(plain, stored_hash):
    return bcrypt.checkpw(plain.encode(), stored_hash)

h = hash_password('s3cret')
print(verify('s3cret', h))

Tuning the Cost Factor

The cost factor (rounds) controls how slow hashing is. Set it so a single hash takes around 100-300ms on your hardware: slow enough to deter attackers, fast enough for login. Re-tune it as hardware improves.

Defending Against Credential Stuffing

Attackers reuse leaked credentials across sites. Defend with rate limiting, account lockout with backoff, and checks against known-breached password lists.

  • Throttle failed logins per account and per IP
  • Reject passwords found in breach corpuses
  • Alert users of logins from new devices

Constant-Time Comparison

Comparing secrets must take the same time regardless of how many characters match, or attackers can infer values via timing. Hashing libraries provide constant-time verify functions; use them instead of plain equality.

Secure Password Reset Tokens

Reset flows are a frequent weak point. Generate a high-entropy random token, store only its hash, set a short expiry, and invalidate it after one use.

import secrets, hashlib, time

def create_reset_token():
    raw = secrets.token_urlsafe(32)
    record = {
        'token_hash': hashlib.sha256(raw.encode()).hexdigest(),
        'expires_at': time.time() + 900,
        'used': False,
    }
    return raw, record  # email raw to user, store record

Avoiding Account Enumeration

Reset and login responses should not reveal whether an email exists. Return the same message regardless, and send the reset email only if the account exists. This prevents attackers from harvesting valid accounts.

Rehashing on Login

When you upgrade algorithms or cost factors, rehash a user's password on their next successful login using the new parameters. This migrates your store gradually without forcing resets.

Quick Check

Test your understanding of secure password storage.

Recap

You learned to hash with salt using Argon2 or bcrypt, tune the cost factor, defend against credential stuffing with rate limiting, compare in constant time, and build single-use expiring reset tokens that avoid account enumeration. Proper credential handling protects users even when the database is breached.

免费开始

用 AI 导师学习 Secure Coding & OWASP Top 10 for Backend — 免费

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

课程
12
课程
48

常见问题解答

「安全的密码存储与凭据恢复」课时是免费的吗?

是的 — 「安全的密码存储与凭据恢复」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Secure Coding & OWASP Top 10 for Backend 课程的其余内容,请升级到 CoddyKit PRO。 Secure Coding & OWASP Top 10 for Backend 课程共包含 4 节课。

「安全的密码存储与凭据恢复」这节课中我会学到什么?

学习如何正确哈希密码、防御凭据攻击,并构建安全的密码重置和账户恢复流程。 你通过在浏览器中直接运行的动手代码来练习 Secure Coding & OWASP Top 10 for Backend,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Secure Coding & OWASP Top 10 for Backend 需要有经验吗?

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

「安全的密码存储与凭据恢复」课时需要多长时间?

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

我能在这节 Secure Coding & OWASP Top 10 for Backend 课中编写并运行代码吗?

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

此课程中的所有课时

  1. 多因素身份验证(MFA)
  2. OAuth 2.0 与 OpenID Connect
  3. JWT 安全与最佳实践
  4. 安全的密码存储与凭据恢复
← 返回 Secure Coding & OWASP Top 10 for Backend