I am using the following code which works fine on python2.7. This code returns me error 'Unicode objects must be encoded before hashing' on python 3.7. Can someone please tell me the equal of this in python3.7 version.
base64.encodestring(hashlib.sha256(any_string).digest()).strip()
A lot of downstream code depends on this so I cannot change this algo. I want the same output in python3.7.
Any pointers would be appreciated.
base64.encodestring(hashlib.sha256(any_string.encode('UTF-8')).digest()).strip()
In Python 3+ unicode objects (strings) and bytes are handled differently than in Python 2. The sha256 function seems to require bytes and not unicode which is why the error is appearing. Adding .encode('UTF-8') to the string will give the correct format for the sha256 function. I have tested this in both python 2.7 and 3.7 and both work correctly and give the same output.
base64.encodestring returns error Unicode objects must be encoded before hashing