パスワードはハッシュ化し、平文では保存しない
Werkzeugでパスワードをハッシュ化して検証します
「パスワードはハッシュ化し、平文では保存しない」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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_hashHash 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 = hashedSalt 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. 🎉
よくある質問
「パスワードはハッシュ化し、平文では保存しない」レッスンは無料ですか?
はい。「パスワードはハッシュ化し、平文では保存しない」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「パスワードはハッシュ化し、平文では保存しない」で何を学びますか?
Werkzeugでパスワードをハッシュ化して検証します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「パスワードはハッシュ化し、平文では保存しない」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- パスワードはハッシュ化し、平文では保存しない
- ユーザーローダーとUserMixin
- login_user、logout_user、セッション
- login_requiredでビューを保護する