简体   繁体   English

Python-passlib

[英]Python - passlib

i have a problem parsing a string from a file to passlib.hash.sha512_crypt.encrypt() 我在将文件中的字符串解析为passlib.hash.sha512_crypt.encrypt()时遇到问题

My Code is this: 我的代码是这样的:

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")

in the file i have this: 在文件中我有这个:

password
123456789
987654321

i know that the default rounds for linux password for example is 5000 but in my script, when he try to encrypt the word password with the salt saltsalt he output 我知道例如linux密码的默认值是5000,但是在我的脚本中,当他尝试使用他输出的salt saltalt加密单词password

$6$saltsalt$YslT1fZBE1gwV0EkEo6UdHwwyL8M/EiBeNfZyr7TZcKxAUd0QkMaP8jmfarPGYVaNUy6haNbxsh6RKsm6dzP81

but when i run it from python shell i got 但是当我从python shell运行它时

>>> from passlib.hash import sha512_crypt
>>> sha512_crypt.encrypt("password", salt="saltsalt", rounds=5000)
'$6$saltsalt$qFmFH.bQmmtXzyBY0s9v7Oicd2z4XSIecDzlB5KiA2/jctKu9YterLp8wwnSq.qc.eoxqOmSuNp2xS0ktL3nh/'

Why they doesn't match? 为什么它们不匹配?

The first hash you posted ( "...$YslT1..." ) is actually the hash of "password\\n" , not "password" , because iterating over a file handle ( l ) returns lines which include any linefeed characters. 您发布的第一个哈希( "...$YslT1..." )实际上是"password\\n"的哈希,而不是"password"的哈希,因为在文件句柄( l )上进行迭代会返回包含任何换行字符的行。 A cheap fix would be to do l = l.rstrip() . 一个便宜的解决方案是做l = l.rstrip()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM