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()
The Python "TypeError: Strings must be encoded before hashing" occurs when we pass a string to a hashing algorithm. To solve the error, use the encode() method to encode the string to a bytes object, e.g. my_str. encode('utf-8') .
hexdigest() Like digest() except the digest is returned as a string object of double length, containing only hexadecimal digits. This may be used to exchange the value safely in email or other non-binary environments.
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')
Per the comment below and this answer.
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("\n", "")
Should probably be
line.strip()
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"\n", b"")
You must have to define encoding format
like utf-8
, Try this easy way,
This example generates a random number using the SHA256 algorithm:
>>> import hashlib >>> hashlib.sha256(str(random.getrandbits(256)).encode('utf-8')).hexdigest() 'cd183a211ed2434eac4f31b317c573c50e6c24e3a28b82ddcb0bf8bedf387a9f'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With