bultin constant time checking
- if hmac.compare_digest is there, we should use it instead of our own fallback. - jwt handler has been updated to use utils.compare (reported in #web2py-users) - includes the same mods as https://github.com/web2py/web2py/pull/1146
This commit is contained in:
+9
-1
@@ -64,6 +64,10 @@ else:
|
||||
except (ImportError, ValueError):
|
||||
HAVE_PBKDF2 = False
|
||||
|
||||
HAVE_COMPARE_DIGEST = False
|
||||
if hasattr(hmac, 'compare_digest'):
|
||||
HAVE_COMPARE_DIGEST = True
|
||||
|
||||
logger = logging.getLogger("web2py")
|
||||
|
||||
|
||||
@@ -77,6 +81,8 @@ def AES_new(key, IV=None):
|
||||
|
||||
def compare(a, b):
|
||||
""" Compares two strings and not vulnerable to timing attacks """
|
||||
if HAVE_COMPARE_DIGEST:
|
||||
return hmac.compare_digest(a, b)
|
||||
if len(a) != len(b):
|
||||
return False
|
||||
result = 0
|
||||
@@ -143,6 +149,7 @@ DIGEST_ALG_BY_SIZE = {
|
||||
512 / 4: 'sha512',
|
||||
}
|
||||
|
||||
|
||||
def get_callable_argspec(fn):
|
||||
if inspect.isfunction(fn) or inspect.ismethod(fn):
|
||||
inspectable = fn
|
||||
@@ -154,6 +161,7 @@ def get_callable_argspec(fn):
|
||||
inspectable = fn
|
||||
return inspect.getargspec(inspectable)
|
||||
|
||||
|
||||
def pad(s, n=32, padchar=' '):
|
||||
return s + (32 - len(s) % 32) * padchar
|
||||
|
||||
@@ -172,7 +180,7 @@ def secure_dumps(data, encryption_key, hash_key=None, compression_level=None):
|
||||
|
||||
|
||||
def secure_loads(data, encryption_key, hash_key=None, compression_level=None):
|
||||
if not ':' in data:
|
||||
if ':' not in data:
|
||||
return None
|
||||
if not hash_key:
|
||||
hash_key = sha1(encryption_key).hexdigest()
|
||||
|
||||
Reference in New Issue
Block a user