Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert String to MD5

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
like image 831
spenman Avatar asked Nov 06 '12 21:11

spenman


People also ask

How do you generate the MD5 hash of a string?

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.

Is MD5 a string?

The MD5 function returns a 32-character string that is a text representation of the hexadecimal value of a 128-bit checksum.

What is MD5 hash generator?

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.

Can you reverse MD5?

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.


1 Answers

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 hashlib

def computeMD5hash(my_string):
    m = hashlib.md5()
    m.update(my_string.encode('utf-8'))
    return m.hexdigest()
like image 64
Day Avatar answered Oct 07 '22 14:10

Day