1. brianfrantz
  2. beaker


Philip Jenvey  committed e8ac113

prefer hashlib over the md5/sha modules which are deprecated in Python 2.6

  • Participants
  • Parent commits eb6cff7
  • Branches trunk

Comments (0)

Files changed (3)

File beaker/crypto/pbkdf2.py

View file
 except ImportError:
     # PyCrypto not available.  Use the Python standard library.
     import hmac as HMAC
-    import sha as SHA1
+    try:
+        from hashlib import sha1 as SHA1
+    except ImportError:
+        # NOTE: We have to use the callable with hashlib (hashlib.sha1),
+        # otherwise hmac only accepts the sha module object itself
+        import sha as SHA1
 def strxor(a, b):
     return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b)])
     def __init__(self, passphrase, salt, iterations=1000,
                  digestmodule=SHA1, macmodule=HMAC):
+        # Support both sha/md5 style modules and hashlib functions
+        if not callable(macmodule):
+            macmodule = macmodule.new
+        if not callable(digestmodule):
+            digestmodule = digestmodule.new
         self.__macmodule = macmodule
         self.__digestmodule = digestmodule
         self._setup(passphrase, salt, iterations, self._pseudorandom)
     def _pseudorandom(self, key, msg):
         """Pseudorandom function.  e.g. HMAC-SHA1"""
-        return self.__macmodule.new(key=key, msg=msg,
+        return self.__macmodule(key=key, msg=msg,
     def read(self, bytes):

File beaker/session.py

View file
 import cPickle
 import Cookie
 import hmac
-import md5
 import os
 import random
-import sha
 import time
 from datetime import datetime, timedelta
+    from hashlib import md5, sha1
+except ImportError:
+    from md5 import md5
+    # NOTE: We have to use the callable with hashlib (hashlib.sha1),
+    # otherwise hmac only accepts the sha module object itself
+    import sha as sha1
 # Check for pycryptopp encryption for AES
     def value_decode(self, val):
         val = val.strip('"')
-        sig = hmac.new(self.secret, val[40:], sha).hexdigest()
+        sig = hmac.new(self.secret, val[40:], sha1).hexdigest()
         if sig != val[:40]:
             return None, val
             return val[40:], val
     def value_encode(self, val):
-        sig = hmac.new(self.secret, val, sha).hexdigest()
+        sig = hmac.new(self.secret, val, sha1).hexdigest()
         return str(val), ("%s%s" % (sig, val))
     def _create_id(self):
-        self.id = md5.new(
-            md5.new("%f%s%f%s" % (time.time(), id({}), random.random(), os.getpid()) ).hexdigest(), 
+        self.id = md5(
+            md5("%f%s%f%s" % (time.time(), id({}), random.random(), os.getpid()) ).hexdigest(), 
         self.is_new = True
         if self.use_cookies:
             return cPickle.loads(data)
     def _make_id(self):
-        return md5.new(md5.new(
+        return md5(md5(
             "%f%s%f%d" % (time.time(), id({}), random.random(), os.getpid())

File beaker/util.py

View file
 from datetime import datetime, timedelta
 import os
-import sha
 import string
 import types
 import weakref
     Set = set
 except NameError:
     from sets import Set
+    from hashlib import sha1
+except ImportError:
+    from sha import sha as sha1
 from beaker.converters import asbool
     ident = string.join(identifiers, "_")
     if digest_filenames:
-        ident = sha.new(ident).hexdigest()
+        ident = sha1(ident).hexdigest()
     ident = os.path.basename(ident)