5

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

Roman Bodnarchuk
  • 29,461
  • 12
  • 59
  • 75
  • I guess the python version you used is too old, according to http://docs.python.org/library/hashlib.html hashlib is only available in python 2.5+ – Tedil Jul 02 '11 at 15:06

5 Answers5

6

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'
André Laszlo
  • 15,169
  • 3
  • 63
  • 81
1

Also, FWIW and for others ending up here, but for hashlib.md5():

import md5

m = md5.new()
...
0

On some python derivatives such as Jython, use the following:

import _hashlib
h =  _hashlib()
md5Res = h.openssl_md5("helloYou").hexdigest()
print(md5Res)
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
Aries
  • 916
  • 6
  • 3
0

The easiest way to find such errors related to the modules no found is to verify their path. I am completely able to run the python facebook ads api code through console but when I try this code through c# i got several path related errors.

Just below given statement before the import statements to give the path of "hashlib.py file".


import sys

sys.path.append('C:\Python34\Lib')

It has resolved my problem.

jkmehmi
  • 66
  • 1
  • 8
0

hashlib is a new module/library in python 2.5 the server certainly run python 2.4 or earlier

solsTiCe
  • 379
  • 5
  • 15