Unable to import "hashlib"

user330885 picture user330885 · Jul 2, 2011 · Viewed 8.7k times · Source

I'm trying to encrypt a string in sha1 and I get an error from the server:

"No Module Named hashlib"

By using the following code:


import hashlib
encrypted = hashlib.sha1(string)
encrypted = encrypted.digest()

I'll appreciate any help,

Thanks, Guy Dor

Answer

André Laszlo picture André Laszlo · Jul 2, 2011

You've probably got a python version < 2.5. Use the sha module instead.

Here's the differences:

>>> import sha
>>> s = sha.new()
>>> s.update('hello')
>>> s.digest()
'\xaa\xf4\xc6\x1d\xdc\xc5\xe8\xa2\xda\xbe\xde\x0f;H,\xd9\xae\xa9CM'

vs

>>> import hashlib
>>> hashlib.sha1('hello').digest()
'\xaa\xf4\xc6\x1d\xdc\xc5\xe8\xa2\xda\xbe\xde\x0f;H,\xd9\xae\xa9CM'