如何纠正TypeError:必须在散列之前编码Unicode对象?

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)

  • `open(wordlist,"r",encoding ='utf-8')`为什么使用open特定编码,编码指定解码编解码器,没有这个选项,它使用平台相关编码. (3认同)

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)

  • 此行使密码无法使用。password_salt = os.urandom(32).hex() 它应该是一个固定的已知值,但只能对服务器保密。请纠正我或将其调整为您的代码。 (2认同)
  • 我同意@Yash,你要么为每个哈希使用一个盐(不是最好的),或者如果为每个哈希生成一个随机盐,则必须将其与哈希一起存储以便稍后再次使用以进行比较 (2认同)

Cat*_*lus 14

错误已经说明了你必须做的事情.MD5对字节进行操作,因此您必须将Unicode字符串编码为bytes,例如使用line.encode('utf-8').


Mik*_*ash 12

编码这条线为我修复了它。

m.update(line.encode('utf-8'))
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算法的编码数据的编码方式与底层文件完全相同.


jfs*_*jfs 6

您可以以二进制模式打开文件:

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)

  • 我非常惊讶我必须向下滚动这么远才能找到第一个理智的答案。除非有某种原因认为“wordlist”文件的编码错误(因此必须从错误的编码中解码,然后使用正确的编码进行哈希编码),这是迄今为止最好的解决方案,避免了无意义的解码和重新编码支持只处理“字节”(OP代码中的错误来源)。 (3认同)

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)


SBi*_*han 6

如果是单行字符串。用 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)