From 91c891b32fbd11753b0f9a22b61d59bd483676d7 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Thu, 9 Aug 2012 23:34:37 -0500 Subject: [PATCH] simplified more CRYPT logic and removed hmac_hash, functionality included in simple_hash, thanks David --- VERSION | 2 +- gluon/html.py | 7 +++---- gluon/utils.py | 20 ++++++++------------ 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/VERSION b/VERSION index 2c6da68c..80ac79d0 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.00.0 (2012-08-09 23:09:46) dev +Version 2.00.0 (2012-08-09 23:34:34) dev diff --git a/gluon/html.py b/gluon/html.py index d09ab194..f935e0dd 100644 --- a/gluon/html.py +++ b/gluon/html.py @@ -25,7 +25,7 @@ from HTMLParser import HTMLParser from htmlentitydefs import name2codepoint from storage import Storage -from utils import web2py_uuid, hmac_hash, compare +from utils import web2py_uuid, simple_hash, compare from highlight import highlight regex_crlf = re.compile('\r|\n') @@ -327,8 +327,7 @@ def URL( # re-assembling the same way during hash authentication message = h_args + '?' + urllib.urlencode(sorted(h_vars)) - sig = hmac_hash(message, (hmac_key or '')+(salt or ''), - digest_alg='sha1') + sig = simple_hash(message, hmac_key or '',salt or '',digest_alg='sha1') # add the signature into vars list_vars.append(('_signature', sig)) @@ -447,7 +446,7 @@ def verifyURL(request, hmac_key=None, hash_vars=True, salt=None, user_signature= message = h_args + '?' + urllib.urlencode(sorted(h_vars)) # hash with the hmac_key provided - sig = hmac_hash(message, str(hmac_key)+(salt or ''), digest_alg='sha1') + sig = simple_hash(message, str(hmac_key), salt or '', digest_alg='sha1') # put _signature back in get_vars just in case a second call to URL.verify is performed # (otherwise it'll immediately return false) diff --git a/gluon/utils.py b/gluon/utils.py index 52e04068..364a118c 100644 --- a/gluon/utils.py +++ b/gluon/utils.py @@ -42,10 +42,15 @@ def simple_hash(text, key='', salt = '', digest_alg = 'md5'): """ if not digest_alg: raise RuntimeError, "simple_hash with digest_alg=None" - elif not isinstance(digest_alg,str): + elif not isinstance(digest_alg,str): # manual approach h = digest_alg(text+key+salt) - elif key: # backward compatile - return hmac_hash(text, key+salt, digest_alg) + elif digest_alg.startswith('pbkdf2'): # latest and coolest! + iterations, keylen, alg = digest_alg[7:-1].split(',') + return pbkdf2_hex(text, salt, int(iterations), + int(keylen),get_digest(alg)) + elif key: # use hmac + digest_alg = get_digest(digest_alg) + h = hmac.new(key+salt,value,digest_alg) else: # compatible with third party systems h = hashlib.new(digest_alg) h.update(text+salt) @@ -82,15 +87,6 @@ DIGEST_ALG_BY_SIZE = { 512/4: 'sha512', } -def hmac_hash(value, salt, digest_alg='md5'): - if isinstance(digest_alg,str) and digest_alg.startswith('pbkdf2'): - iterations, keylen, alg = digest_alg[7:-1].split(',') - return pbkdf2_hex(value, salt, int(iterations), - int(keylen),get_digest(alg)) - digest_alg = get_digest(digest_alg) - d = hmac.new(salt,value,digest_alg) - return d.hexdigest() - ### compute constant ctokens def initialize_urandom():