如何更正 TypeError: Unicode 对象必须在散列之前编码? [英] How to correct TypeError: Unicode-objects must be encoded before hashing?

查看:25
本文介绍了如何更正 TypeError: Unicode 对象必须在散列之前编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个错误:

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

当我尝试在 Python 3.2.2 中执行此代码时:

when I try to execute this code in 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("
", "")

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

推荐答案

它可能正在寻找 wordlistfile 中的字符编码.

It is probably looking for a character encoding from wordlistfile.

wordlistfile = open(wordlist,"r",encoding='utf-8')

或者,如果您正在逐行工作:

Or, if you're working on a line-by-line basis:

line.encode('utf-8')


编辑

根据下面的评论和这个答案.

我上面的回答假设所需的输出是来自 wordlist 文件的 str.如果您习惯于使用 bytes,那么最好使用 open(wordlist, "rb").但重要的是要记住,如果你将 hashfilehexdigest 的输出进行比较,你的 hashfile 应该 NOT 使用 rb>.hashlib.md5(value).hashdigest() 输出一个 str 并且不能直接与字节对象进行比较:'abc' != b'abc'.(这个话题还有很多,但我没有时间 ATM).

My answer above assumes that the desired output is a str from the wordlist file. If you are comfortable in working in bytes, then you're better off using open(wordlist, "rb"). But it is important to remember that your hashfile should NOT use rb if you are comparing it to the output of hexdigest. hashlib.md5(value).hashdigest() outputs a str and that cannot be directly compared with a bytes object: 'abc' != b'abc'. (There's a lot more to this topic, but I don't have the time ATM).

还需要注意的是这一行:

It should also be noted that this line:

line.replace("
", "")

应该是

line.strip()

这对字节和字符串都适用.但是,如果您决定简单地转换为 bytes,那么您可以将该行更改为:

That will work for both bytes and str's. But if you decide to simply convert to bytes, then you can change the line to:

line.replace(b"
", b"")

这篇关于如何更正 TypeError: Unicode 对象必须在散列之前编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆