【问题标题】:Verifying a PBKDF2 password hash in python-pbkdf2在 python-pbkdf2 中验证 PBKDF2 密码哈希
【发布时间】:2016-01-13 00:44:55
【问题描述】:

我正在使用下面的 sn-p 来加密用户密码,然后再保存到数据库中。

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

【问题讨论】:

  • 你说的“并不总是一样”是什么意思?验证是否仅在某些时候有效,还是始终对某些密码有效,而对其他密码无效?

标签: python encryption pbkdf2


【解决方案1】:

好的,做了更多的研究,发现要实现这一点,我首先必须加密密码并保存在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 2012-07-28
    • 2020-02-29
    • 2014-10-20
    • 2011-05-24
    • 2017-07-22
    • 2016-04-29
    相关资源
    最近更新 更多