Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4bea0e3

Browse files
committed
Avoiding md5/sha1 deprecated warning in Python >=2.6
1 parent 8f26f30 commit 4bea0e3

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

lib/core/convert.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@
2222
Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2323
"""
2424

25-
import md5
26-
import sha
25+
try:
26+
import hashlib
27+
except:
28+
import md5
29+
import sha
30+
31+
import sys
2732
import struct
2833
import urllib
2934

@@ -45,7 +50,10 @@ def hexencode(string):
4550
return string.encode("hex")
4651

4752
def md5hash(string):
48-
return md5.new(string).hexdigest()
53+
if sys.modules.has_key('hashlib'):
54+
return hashlib.md5(string).hexdigest()
55+
else:
56+
return md5.new(string).hexdigest()
4957

5058
def orddecode(string):
5159
packedString = struct.pack("!"+"I" * len(string), *string)
@@ -55,7 +63,10 @@ def ordencode(string):
5563
return tuple([ord(char) for char in string])
5664

5765
def sha1hash(string):
58-
return sha.new(string).hexdigest()
66+
if sys.modules.has_key('hashlib'):
67+
return hashlib.sha1(string).hexdigest()
68+
else:
69+
return sha.new(string).hexdigest()
5970

6071
def urldecode(string):
6172
result = None

0 commit comments

Comments
 (0)