Nic
Nic

Reputation: 1657

MD5 hashing: Unicode-objects must be encoded before hashing

I have some code to hash a string in Python 3

import hashlib
hobj = hashlib.md5()

And when I use the command:

hobj.update('test')

I get the error:

TypeError: Unicode-objects must be encoded before hashing

Upvotes: 1

Views: 5498

Answers (1)

user5547025
user5547025

Reputation:

Encode the string to UTF-8 (for example):

hobj.update('test'.encode("UTF-8"))
print(hobj.hexdigest())

Output:

098f6bcd4621d373cade4e832627b4f6

Upvotes: 5

Related Questions