我使用下面的代码片段在保存到数据库之前加密用户密码。
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"
但是我该如何检查数据库中的内容,因为加密的字符串并不总是相同的。我正在使用 python-pbkdf2 .
最佳答案
好的,做了更多的研究,发现要实现这一点,我首先必须加密密码并保存在 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
。
关于python - 在 python-pbkdf2 中验证 PBKDF2 密码哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33133625/