Failed to import hashlib

I try to encrypt a string in sha1 and I get an error message from the server:

"No Module Named hashlib"

Using the following code:


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

I would be grateful for any help,

Thank you Guy Dor

+5
source share
5 answers

You probably got python version <2.5. Use instead sha.

Here are 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'

against

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

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

import md5

m = md5.new()
...
+1
source

hashlib - / python 2.5 , , python 2.4

0

python, Jython, :

import _hashlib
h =  _hashlib()
md5Res = h.openssl_md5("helloYou").hexdigest()
print(md5Res)
0

The easiest way to find such errors associated with missing modules is to check their path. I can fully run the python facebook adi api through the console, but when I try this code through C #, I got some path related errors.

Below the statement, the path to the hashlib.py file is specified before import operations.


import sys

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

He solved my problem.

0
source

All Articles