Unicode Objects Must Be Encoded Before Hashing Error
Questions dealing with similar issues: SO 1, SO 2, SO 3. I've tried their answers, encoding pretty much any string to utf-8, but hmac still tells me to encode my unicoe chars. The
Solution 1:
"Parameter msg can be of any type supported by hashlib.".
"Note: Feeding string objects into is not supported, as hashes work on bytes, not on characters.".
Thus, your message must be of type bytes. Use .encode() on the message wich will give you the bytes object.
Note: This is only necessary for python 3!
To encode the digest to base64 use the base64 library.
importbase64signature_b64= base64.b64encode(signature.digest())
Solution 2:
For Python 3.9, I was getting the same error,
self._inner.update(msg) TypeError: Unicode-objects must be encoded before hashing
The following worked for me,
signature = hmac.new(key.encode('utf-8'), msg.encode('utf-8'), hashlib.sha256)
auth_token = str(signature.hexdigest())
Post a Comment for "Unicode Objects Must Be Encoded Before Hashing Error"