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 you're 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 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"")
TypeError: Unicode-objects must be encoded before hashing in Hashlib Function
This worked
h = hashlib.sha1(b"%s|%s|%s" % (str(freq1).encode('utf-8'), str(freq2).encode('utf-8'), str(t_delta).encode('utf-8')))
hashlib: Unicode-objects must be encoded before hashing
The answer is in the error message: use encode
on your text string before hashing.
h = hashlib.sha1(("%s|%s|%s" % (str(freq1), str(freq2), str(t_delta))).encode('utf-8'))
The reason this is necessary is because hashlib.sha1()
requires a bytes
object due to the way it works internally. Normal Python strings (since version 3.0) are made of Unicode codepoints, which don't fit into a byte. They need an encoding which defines how the translation between codepoints and bytes occurs. UTF-8 is the most popular encoding, because it can handle every Unicode codepoint yet remain backwards compatible with older encodings like ASCII.
TypeError: Unicode-objects must be encoded before hashing
import hashlib
message = 'whatever'.encode()
code = hashlib.sha256(message).hexdigest()
print(code)
Python TypeError: Unicode-objects must be encoded before hashing
Only bytes
strings can be hashed. str
is a Unicode string. Encode to bytes, decode to Unicode.
In your .update()
, all four items are being converted to str
. So once you concatenate them together, .encode()
the whole thing to bytes
:
import hashlib as hasher
class Block:
def __init__(self, index, timestamp, data, previous_hash):
self.index = index
self.timestamp = timestamp
self.data = data
self.previous_hash = previous_hash #these four items used to calculate crypHash of each block
self.hash = self.hash_block() #helps ensure integrity throughout blockchain
def hash_block(self):
sha = hasher.sha256()
to_hash = str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash)
sha.update(to_hash.encode())
return sha.hexdigest()
Related Topics
How to Move to One Folder Back in Python
How Can My Model Primary Key Start With a Specific Number
Is There a Memory Efficient and Fast Way to Load Big Json Files
Convert a Standard Python Key Value Dictionary List to Pyspark Data Frame
Sklearn Error Valueerror: Input Contains Nan, Infinity or a Value Too Large for Dtype('Float64')
How to Set Proxy Authentication (User & Password) Using Python + Selenium
How to Use a Pre-Trained Neural Network With Grayscale Images
Replacing Special Characters in a List in Python
How to Test If a List Contains Another List as a Contiguous Subsequence
Pickle - Cpickle.Unpicklingerror: Invalid Load Key, '?'
Deleting Dataframe Row in Pandas If a Combination of Column Values Equals a Tuple in a List
How to Get Rid of the B-Prefix in a String in Python
Python Pip Install Error [Ssl: Certificate_Verify_Failed]
How to Use and Print the Pandas Dataframe Name