我在将字符串从文件解析为 passlib.hash.sha512_crypt.encrypt()
时遇到问题我的代码是这样的:
from passlib.hash import sha512_crypt
h = input("sha512: ")
s = input("salt: ")
d = input("dictionary: ")
dictionary = open(d, "r")
for l in dictionary:
for i in range(1000,7000):
t = sha512_crypt.encrypt( str(l), salt=s, rounds=i)
print (t)
t = t.replace("rounds=" + str(i) + "$", "")
print("[DEBUG] SEARCHING " + str(i) + " USING " + l),
if (t == h):
print("FOUND AT: " + str(i) + "\nCODE IS: " + l)
break
else:
print("NO CODE FOUND")
在文件中我有这个:
password
123456789
987654321
我知道例如 linux 密码的默认轮数是 5000,但是在我的脚本中,当他尝试用他输出的盐 saltsalt 加密单词 password 时
$6$saltsalt$YslT1fZBE1gwV0EkEo6UdHwwyL8M/EiBeNfZyr7TZcKxAUd0QkMaP8jmfarPGYVaNUy6haNbxsh6RKsm6dzP81
但是当我从 python shell 运行它时我得到了
>>> from passlib.hash import sha512_crypt
>>> sha512_crypt.encrypt("password", salt="saltsalt", rounds=5000)
'$6$saltsalt$qFmFH.bQmmtXzyBY0s9v7Oicd2z4XSIecDzlB5KiA2/jctKu9YterLp8wwnSq.qc.eoxqOmSuNp2xS0ktL3nh/'
为什么不匹配?
最佳答案
您发布的第一个散列 ("...$YslT1..."
) 实际上是 "password\n"
的散列,而不是 "password"
,因为遍历文件句柄 (l
) 会返回包含任何换行符的行。一个廉价的解决方法是执行 l = l.rstrip()
。
关于Python-密码库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33857087/