encryptionhashsha1hashlibpython-2.4

Unable to import "hashlib"


I tried this code:

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

But I got an error that says "No Module Named hashlib". What is wrong, and how do I fix it?


Solution

  • 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'