Ok I am trying to write a basic converter of a string to md5
hash code but when I run my program I keep getting an error that say's:
Traceback (most recent call last):
File "C:\Users\Shane\Documents\Amer CISC\lab4.py", line 30, in <module>
assertEqual (computeMD5hash("The quick brown fox jumps over the lazy dog"),("9e107d9d372bb6826bd81d3542a419d6"))
File "C:\Users\Shane\Documents\Amer CISC\lab4.py", line 27, in computeMD5hash
m.update(string)
TypeError: Unicode-objects must be encoded before hashing
My code looks like this:
def computeMD5hash(string):
import hashlib
from hashlib import md5
m = hashlib.md5()
m.update((string))
md5string=m.digest()
return md5string
Call MessageDigest. getInstance("MD5") to get a MD5 instance of MessageDigest you can use. The compute the hash by doing one of: Feed the entire input as a byte[] and calculate the hash in one operation with md.
The MD5 function returns a 32-character string that is a text representation of the hexadecimal value of a 128-bit checksum.
The MD5 hashing algorithm uses a complex mathematical formula to create a hash. It converts data into blocks of specific sizes and manipulates that data a number of times. While this is happening, the algorithm adds a unique value into the calculation and converts the result into a small signature or hash.
No, it is not possible to reverse a hash function such as MD5: given the output hash value it is impossible to find the input message unless enough information about the input message is known.
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
from hashlib import
line or the temporary md5string
.import hashlib
should be moved to module scope.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 hashlib
def computeMD5hash(my_string):
m = hashlib.md5()
m.update(my_string.encode('utf-8'))
return m.hexdigest()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With