その後、ユーザー入力パスワードuser_pass
があるとします。ハッシュも同様にハッシュし、ハッシュと格納されたハッシュを比較し、一致すると元のパスワードも一致します。
bcryptはハッシュされたパスワードの一部としてsalt値を自動的に保存するので、将来の入力もハッシュするときに使用できることに注意してください。周り
初回:
import bcrypt
password = u'foobar'
salt = bcrypt.gensalt()
password_hashed = bcrypt.hashpw(password, salt)
# store 'password_hashed' in a database of your choosing
後の時代:
#Initial generation
hashed = bcrypt.hashpw(password, bcrypt.gensalt())
#Store hashed in your db
#Load hashed from the db and check the provided password
if bcrypt.hashpw(password, hashed) == hashed:
print "It matches"
else:
print "It does not match"
http://www.mindrot.org/projects/py-bcrypt/:
import bcrypt
password = something_that_gets_input()
stored_hash = something_that_gets_this_from_the_db()
if bcrypt.hashpw(password, stored_hash) == stored_hash:
# password matches
偉大な答えですが、 'hash'はPython 2と3の予約済みキーワード(組み込み関数)であり、' hash = ... 'を設定すると、あなたが入っているスコープ内の組み込み関数がオーバーライドされます。 'hashed'や' pw_hash'のようなものです。 – alichaudry
私は同意します。この「ハッシュ」は別の名前で置き換えなければなりません:)。 – ivanleoncz
文字列 '' secret'.encode() 'をエンコードすることを忘れないでください。注:Python 3でテストされています。 – Yamaneko