convert String to MD5 convert String to MD5 python-3.x python-3.x

convert String to MD5


As the error suggests, your string must be unicode and you have to encode it. Looking at the call you make (from your stack trace):

computeMD5hash("The quick brown fox jumps over the lazy dog")

it looks like you must be running Python 3 where strings are unicode objects. To encode to a byte representation which can then be processed by the hashlib, change this

m.update((string))

to this (if utf-8 is an appropriate encoding for you to use - it depends how you will be using this):

m.update(string.encode('utf-8'))

If this is all news to you, you should probably read the excellent Python 3 Unicode HOWTO.


Also, while I'm here, your code has some other issues

  • some unecessary bits - no need for the from hashlib import line or the temporary md5string.
  • it's bad form to import modules from within a function, so import hashlib should be moved to module scope.
  • the function is returning the digest() which is raw binary, and from your stack trace it looks like you're expecting the hexdigest() instead which is the same thing represented as a hexadecimal string.

To fix and tidy it all up, try this:

import hashlibdef computeMD5hash(my_string):    m = hashlib.md5()    m.update(my_string.encode('utf-8'))    return m.hexdigest()


Rather than trying to hash the string, you should hash an encoded byte sequence. Instead of

>>> import hashlib>>> hashlib.md5("fred")Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: Unicode-objects must be encoded before hashing

you should encode it, e.g.:

>>> "fred".encode("utf")b'fred'>>> hashlib.md5("fred".encode("utf")).hexdigest()'570a90bfbf8c7eab5dc5d4e26832d5b1'

In Python 2 you could get away without doing this, and it led to no end of unnoticed bugs. Fortunately Python 3 has much saner unicode support, and distinguishes between bytes and strings.