0Pricing
Secure Coding & OWASP Top 10 for Backend · レッスン

安全なパスワード保存と認証情報の復旧

パスワードを正しくハッシュ化し、認証情報攻撃に対処し、安全なパスワードリセットとアカウントリカバリフローを構築する方法を学びます。

「安全なパスワード保存と認証情報の復旧」はCoddyKit上の無料Secure Coding & OWASP Top 10 for Backendレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.

よくある質問

「安全なパスワード保存と認証情報の復旧」レッスンは無料ですか?

はい。「安全なパスワード保存と認証情報の復旧」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応の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を演習し、24時間対応の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に戻る