python – How to correct TypeError: Unicode-objects must be encoded before hashing?
python – How to correct TypeError: Unicode-objects must be encoded before hashing?
It is probably looking for a character encoding from wordlistfile
.
wordlistfile = open(wordlist,r,encoding=utf-8)
Or, if youre working on a line-by-line basis:
line.encode(utf-8)
EDIT
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 youre 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 != babc
. (Theres a lot more to this topic, but I dont 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 strs. But if you decide to simply convert to bytes
, then you can change the line to:
line.replace(bn, 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
python – How to correct TypeError: Unicode-objects must be encoded before hashing?
import hashlib
string_to_hash = 123
hash_object = hashlib.sha256(str(string_to_hash).encode(utf-8))
print(Hash, hash_object.hexdigest())