I have this error:
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
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("\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()
It is probably looking for a character encoding from
wordlistfile
.Or, if you’re working on a line-by-line basis:
EDIT
Per the comment below and this answer.
My answer above assumes that the desired output is a
str
from thewordlist
file. If you are comfortable in working inbytes
, then you’re better off usingopen(wordlist, "rb")
. But it is important to remember that yourhashfile
should NOT userb
if you are comparing it to the output ofhexdigest
.hashlib.md5(value).hashdigest()
outputs astr
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:
Should probably be
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: