import bcrypt
hashedstring = bcrypt.gensalt()
password = bcrypt.hashpw(password,hashedstring)
次回ハッシュ文字列を取得するときに正常にログインできるように、データベース テーブル フィールドに毎回ハッシュ文字列を保存する必要がありますか?
または、事前に生成された静的なハッシュ文字列をコードで使用する必要がありますか?
パスワードのハッシュに使用するソルトは、結果のハッシュに保存されます。これは、ハッシュから復元できるため、データベースに保存する必要がないことを意味します。
プロジェクトページによると、これは次のように行うことができます:
# Store a hash.
import bcrypt
hashed = bcrypt.hashpw(password, bcrypt.gensalt())
store_in_db(user, hashed) #Where user is the user to load the hash for, and store_in_db does what it says on the tin.
# Check against an existing hash
import bcrypt
hashed = load_from_db(user) # (get the password of the user from database) Where user is the user to load the hash for, and load_from_db does what it says on the tin.
if bcrypt.hashpw(password, hashed) == hashed: # Where password is a plaintext password attempt.
print "It matches"
else:
print "It does not match"
はい、各値に異なるソルトを使用する必要があります。これは BCrypt の設計が奨励しています。
簡単な答え: パスワードごとに新しいソルトを使用します。(編集:bcryptを使用すると、ソルトを個別に保存する必要はありません)
攻撃者が Web サイトからパスワード データベースを取得したとします。すべてのパスワードが一般的なソルトを使用してハッシュされている場合、攻撃者は一般的なパスワードを使用している人物を簡単に見つけることができます。
hashedpwd = somehash('swordfish' + salt)
次に、「swordfish」をパスワードとして使用しているすべての人を見つけるために、データベース クエリだけが必要です。非常に一般的なパスワードを持つユーザーのかなりの割合が常に存在します。
一方、すべてのパスワードに独自のソルトがあり、データベースに 100 万のパスワードがある場合、攻撃者は 1 つのパスワードをチェックするために 100 万のハッシュを計算する必要があるため、はるかに安全です。