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:
m = hashlib.md5() #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)
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:
Please take a look first at that answer.
Now, the error message is clear: you can only use bytes, not Python strings (what used to be
unicode
in Python < 3), so you have to encode the strings with your preferred encoding:utf-32
,utf-16
,utf-8
or even one of the restricted 8-bit encodings (what some might call codepages).The bytes in your wordlist file are being automatically decoded to Unicode by Python 3 as you read from the file. I suggest you do:
so that the encoded data pushed to the md5 algorithm are encoded exactly like the underlying file.
The error already says what you have to do. MD5 operates on bytes, so you have to encode Unicode string into
bytes
, e.g. withline.encode('utf-8')
.You must have to define
encoding format
likeutf-8
, Try this easy way,This example generates a random number using the SHA256 algorithm:
To store the password (PY3):
You could open the file in binary mode: