[英]Verifying a PBKDF2 password hash in python-pbkdf2
我在保存到數據庫之前使用下面的代碼片段來加密用戶密碼。
from pbkdf2 import crypt
pwhash = crypt(password_from_user)
示例: $p5k2$$Y0qfZ64u$A/pYO.3Mt9HstUtEEhWH/RXBg16EXDMr
然后,我將其保存在數據庫中。 在本地,我可以執行檢查做這樣的事情:
from pbkdf2 import crypt
pwhash = crypt("secret")
alleged_pw = raw_input("Enter password: ")
if pwhash == crypt(alleged_pw, pwhash):
print "Password good"
else:
print "Invalid password"
但是如何對db上的內容執行檢查,因為加密字符串並不總是相同。 我正在使用python-pbkdf2 。
Okey,做了更多研究並想出要實現這一點,我首先要加密密碼並保存在db.as中:
pwhash = crypt("secret",iterations=1000)
這可以產生像$p5k2$3e8$her4h.6b$.p.OE5Gy4Nfgue4D5OKiEVWdvbxBovxm
這樣的字符串
並且要驗證用戶何時想要使用相同的密碼登錄,我使用以下功能:
def isValidPassword(userPassword,hashKeyInDB):
result = crypt(userPassword,hashKeyInDB,iterations = 1000)
return reesult == hashKeyInDB #hashKeyInDB in this case is $p5k2$3e8$her4h.6b$.p.OE5Gy4Nfgue4D5OKiEVWdvbxBovxm
如果密碼相同,則此方法返回True
否則返回False
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.