我试图破解密码,但无法成功。这是密码。
from hashlib import sha1 as sha_constructor
import random
def generate_sha1(string, salt=None):
if not isinstance(string, (str, str)):
string = str(string)
if isinstance(string, str):
string = string.encode("utf-8")
if not salt:
salt = sha_constructor(str(random.random())).hexdigest()[:5]
hash = sha_constructor(salt + string).hexdigest()
return salt, hash
a = generate_sha1('12345')
print(a)我得到了这个错误。
TypeError: Unicode-objects must be encoded before hashing我做错什么了?
发布于 2014-07-01 10:15:11
对于Python 2,请尝试
if isinstance(string, unicode):而不是
if isinstance(string, str):而且,isinstance(string, (str, str)):也没有意义。应该是isinstance(string, (str, unicode)):
编辑 For Python3,您需要将参数编码为sha_constructor()
arg = str(random.random()).encode('utf-8')
salt = sha_constructor(arg).hexdigest()[:5]如果使用+运算符,Python将再次创建一个必须编码的(unicode)字符串。
https://stackoverflow.com/questions/24507691
复制相似问题