我散列密码如下:验证一个哈希密码
mysalt = os.urandom(12).encode('hex')
passhash = mysalt + pw
hash = str(pbkdf2_sha256.encrypt(passhash))
我如何可以验证此密码与用户入口?如下:
hash3 = str(pbkdf2_sha256.encrypt(passhash))
...返回一个完全不同的值。
在此先感谢。
我散列密码如下:验证一个哈希密码
mysalt = os.urandom(12).encode('hex')
passhash = mysalt + pw
hash = str(pbkdf2_sha256.encrypt(passhash))
我如何可以验证此密码与用户入口?如下:
hash3 = str(pbkdf2_sha256.encrypt(passhash))
...返回一个完全不同的值。
在此先感谢。
确保您在存储密码时使用相同的盐,因为您检查密码时:passhash
不同盐的值会导致不同的哈希值。
您可以为每个密码(更方便)使用相同的盐,为每个用户(更安全)使用和存储不同的盐,或两者的组合。
编辑:你没有指定你使用哪个库来获得pbkdf2_sha256
。这可能是因为图书馆本身可以处理与盐有关的任务。例如,PassLib会为您生成一个盐并将其包含在结果中。它提供了一个verify
方法来根据该结果检查用户的输入。
建议不要比较结果字符串 - 通常编码会妨碍您的工作。尝试比较实际字节。
的解决方案是:
import os
from passlib.hash import pbkdf2_sha256
mysalt=os.urandom(12).encode('hex')
hash = pbkdf2_sha256.encrypt("password")
print hash
print pbkdf2_sha256.verify("password", hash)
pwd='sefrhiloliutzrthgrfsdyv<sef234244567!"234wsdycvhn'
mypass =mysalt+pwd
print mypass
hash2 =pbkdf2_sha256.encrypt(mypass, rounds =200000)
hash1= pbkdf2_sha256.encrypt(pwd, rounds=80000, salt_size=100)
print"hash2: ", hash2
print pbkdf2_sha256.verify(mypass,hash2)
这是很难从代码明智的,如果你不添加一些格式。 – arkascha
我希望你使用相同的盐,而不是尝试用新的盐。 –