在python-pbkdf2

时间:2015-10-14 19:07:46

标签: python encryption 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

1 个答案:

答案 0 :(得分:1)

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

相关问题