diff options
| author | Thorsten Behrens <sbehrens@gmx.li> | 2010-12-28 16:26:52 -0500 |
|---|---|---|
| committer | Thorsten Behrens <sbehrens@gmx.li> | 2010-12-28 16:26:52 -0500 |
| commit | 295ce314d9f5c750d9569ac75a4f78331fae9992 (patch) | |
| tree | 943c5f2e5237b3cf0665dd8fed278e4efa518770 /lib/Crypto/Hash | |
| parent | 1873473e13161587b870adadc70c1d625f8bc5cf (diff) | |
| download | pycrypto-295ce314d9f5c750d9569ac75a4f78331fae9992.tar.gz | |
Changes to allow pycrpyto to work on Python 3.x as well as 2.1 through 2.7
Diffstat (limited to 'lib/Crypto/Hash')
| -rw-r--r-- | lib/Crypto/Hash/HMAC.py | 11 | ||||
| -rw-r--r-- | lib/Crypto/Hash/MD5.py | 8 | ||||
| -rw-r--r-- | lib/Crypto/Hash/SHA.py | 4 |
3 files changed, 13 insertions, 10 deletions
diff --git a/lib/Crypto/Hash/HMAC.py b/lib/Crypto/Hash/HMAC.py index 4daff2f..333d563 100644 --- a/lib/Crypto/Hash/HMAC.py +++ b/lib/Crypto/Hash/HMAC.py @@ -43,9 +43,8 @@ __revision__ = "$Id$" __all__ = ['new', 'digest_size'] -import string - from Crypto.Util.strxor import strxor_c +from Crypto.Util.py3compat import * # The size of the digests returned by HMAC depends on the underlying # hashing module used. @@ -83,7 +82,7 @@ class HMAC: if len(key) > blocksize: key = digestmod.new(key).digest() - key = key + chr(0) * (blocksize - len(key)) + key = key + bchr(0) * (blocksize - len(key)) self.outer.update(strxor_c(key, opad)) self.inner.update(strxor_c(key, ipad)) if (msg): @@ -102,7 +101,7 @@ class HMAC: An update to this copy won't affect the original object. """ - other = HMAC("") + other = HMAC(b("")) other.digestmod = self.digestmod other.inner = self.inner.copy() other.outer = self.outer.copy() @@ -122,8 +121,8 @@ class HMAC: def hexdigest(self): """Like digest(), but returns a string of hexadecimal digits instead. """ - return "".join([string.zfill(hex(ord(x))[2:], 2) - for x in tuple(self.digest())]) + return "".join(["%02x" % bord(x) + for x in tuple(self.digest())]) def new(key, msg = None, digestmod = None): """Create a new hashing object and return it. diff --git a/lib/Crypto/Hash/MD5.py b/lib/Crypto/Hash/MD5.py index 6838f9b..abc0b1d 100644 --- a/lib/Crypto/Hash/MD5.py +++ b/lib/Crypto/Hash/MD5.py @@ -24,11 +24,14 @@ __revision__ = "$Id$" __all__ = ['new', 'digest_size'] +from Crypto.Util.py3compat import * + try: # The md5 module is deprecated in Python 2.6, so use hashlib when possible. import hashlib - def new(data=""): - return hashlib.md5(data) + + def new(data=b("")): + return hashlib.md5(data) digest_size = new().digest_size except ImportError: @@ -39,4 +42,3 @@ except ImportError: digest_size = digestsize del digestsize del md5 - diff --git a/lib/Crypto/Hash/SHA.py b/lib/Crypto/Hash/SHA.py index 13f69e1..ce78d12 100644 --- a/lib/Crypto/Hash/SHA.py +++ b/lib/Crypto/Hash/SHA.py @@ -24,10 +24,12 @@ __revision__ = "$Id$" __all__ = ['new', 'digest_size'] +from Crypto.Util.py3compat import * + try: # The md5 module is deprecated in Python 2.6, so use hashlib when possible. import hashlib - def new(data=""): + def new(data=b("")): return hashlib.sha1(data) digest_size = new().digest_size |
