Joh*_*mBF 256 python unicode syntax-error hashlib python-3.x
我有这个错误:
Traceback (most recent call last):
File "python_md5_cracker.py", line 27, in <module>
m.update(line)
TypeError: Unicode-objects must be encoded before hashing
Run Code Online (Sandbox Code Playgroud)
当我尝试在Python 3.2.2中执行此代码时:
import hashlib, sys
m = hashlib.md5()
hash = ""
hash_file = input("What is the file name in which the hash resides? ")
wordlist = input("What is your wordlist? (Enter the file name) ")
try:
hashdocument = open(hash_file, "r")
except IOError:
print("Invalid file.")
raw_input()
sys.exit()
else:
hash = hashdocument.readline()
hash = hash.replace("\n", "")
try:
wordlistfile = open(wordlist, "r")
except IOError:
print("Invalid file.")
raw_input()
sys.exit()
else:
pass
for line in wordlistfile:
# Flush the buffer (this caused a massive problem when placed
# at the beginning of the script, because the buffer kept getting
# overwritten, thus comparing incorrect hashes)
m = hashlib.md5()
line = line.replace("\n", "")
m.update(line)
word_hash = m.hexdigest()
if word_hash == hash:
print("Collision! The word corresponding to the given hash is", line)
input()
sys.exit()
print("The hash given does not correspond to any supplied word in the wordlist.")
input()
sys.exit()
Run Code Online (Sandbox Code Playgroud)
cwa*_*ole 259
它可能正在寻找一个字符编码wordlistfile.
wordlistfile = open(wordlist,"r",encoding='utf-8')
Run Code Online (Sandbox Code Playgroud)
或者,如果您正在逐行工作:
line.encode('utf-8')
Run Code Online (Sandbox Code Playgroud)
Jay*_*tel 114
您必须定义encoding format一样utf-8,试试这个简单的方法,
此示例使用SHA256算法生成随机数:
>>> import hashlib
>>> hashlib.sha256(str(random.getrandbits(256)).encode('utf-8')).hexdigest()
'cd183a211ed2434eac4f31b317c573c50e6c24e3a28b82ddcb0bf8bedf387a9f'
Run Code Online (Sandbox Code Playgroud)
Khắ*_* Từ 16
存储密码(PY3):
import hashlib, os
password_salt = os.urandom(32).hex()
password = '12345'
hash = hashlib.sha512()
hash.update(('%s%s' % (password_salt, password)).encode('utf-8'))
password_hash = hash.hexdigest()
Run Code Online (Sandbox Code Playgroud)
tzo*_*zot 11
请先看看那个答案.
现在,该错误信息是明确的:你只能使用字节,而不是Python字符串(曾经被认为是unicode在Python <3),所以你必须与你的首选编码编码字符串:utf-32,utf-16,utf-8或限制8-甚至一个位编码(有些人可能称之为代码页).
当您从文件中读取时,您的wordlist文件中的字节将由Python 3自动解码为Unicode.我建议你这样做:
m.update(line.encode(wordlistfile.encoding))
Run Code Online (Sandbox Code Playgroud)
因此,推送到md5算法的编码数据的编码方式与底层文件完全相同.
您可以以二进制模式打开文件:
import hashlib
with open(hash_file) as file:
control_hash = file.readline().rstrip("\n")
wordlistfile = open(wordlist, "rb")
# ...
for line in wordlistfile:
if hashlib.md5(line.rstrip(b'\n\r')).hexdigest() == control_hash:
# collision
Run Code Online (Sandbox Code Playgroud)
Sab*_*i 6
import hashlib
string_to_hash = '123'
hash_object = hashlib.sha256(str(string_to_hash).encode('utf-8'))
print('Hash', hash_object.hexdigest())
Run Code Online (Sandbox Code Playgroud)
如果是单行字符串。用 b 或 B 包裹它。例如:
variable = b"This is a variable"
Run Code Online (Sandbox Code Playgroud)
或者
variable2 = B"This is also a variable"
Run Code Online (Sandbox Code Playgroud)