details: http://www.bx.psu.edu/hg/galaxy/rev/084973d2b331 changeset: 3311:084973d2b331 user: James Taylor <james@jamestaylor.org> date: Mon Feb 01 16:23:20 2010 -0500 description: Fix md5 hashing helper for Python 2.4 diffstat: lib/galaxy/util/hash_util.py | 43 +++++++++++++++------------ lib/galaxy/web/framework/helpers/__init__.py | 4 +- 2 files changed, 26 insertions(+), 21 deletions(-) diffs (78 lines): diff -r 5ea7bb062c22 -r 084973d2b331 lib/galaxy/util/hash_util.py --- a/lib/galaxy/util/hash_util.py Mon Feb 01 15:43:07 2010 -0500 +++ b/lib/galaxy/util/hash_util.py Mon Feb 01 16:23:20 2010 -0500 @@ -1,28 +1,33 @@ +""" +Utility functions for bi-directional Python version compatibility. Python 2.5 +introduced hashlib which replaced sha in Python 2.4 and previous versions. +""" + import sys, logging -using_24 = sys.version_info[:2] < ( 2, 5 ) -if using_24: - import sha -else: + +# Use hashlib module if for Python 2.5+, fall back on old sha and md5 modules +try: import hashlib + sha1 = hashlib.sha1 + md5 = hashlib.md5 +except ImportError, e: + import sha, md5 + sha1 = sha.new + md5 = md5.new + import hmac log = logging.getLogger( __name__ ) -""" -Utility functions for bi-directional Python version compatibility. Python 2.5 -introduced hashlib which replaced sha in Python 2.4 and previous versions. -""" def new_secure_hash( text_type=None ): - if using_24: - if text_type: - return sha.new( text_type ).hexdigest() - return sha.new() + """ + Returns either a sha1 hash object (if called with no arguments), or a + hexdigest of the sha1 hash of the argument `text_type`. + """ + if text_type: + return sha1( text_type ).hexdigest() else: - if text_type: - return hashlib.sha1( text_type ).hexdigest() - return hashlib.sha1() + return sha1() + def hmac_new( key, value ): - if using_24: - return hmac.new( key, value, sha ).hexdigest() - else: - return hmac.new( key, value, hashlib.sha1 ).hexdigest() + return hmac.new( key, value, sha1 ).hexdigest() diff -r 5ea7bb062c22 -r 084973d2b331 lib/galaxy/web/framework/helpers/__init__.py --- a/lib/galaxy/web/framework/helpers/__init__.py Mon Feb 01 15:43:07 2010 -0500 +++ b/lib/galaxy/web/framework/helpers/__init__.py Mon Feb 01 16:23:20 2010 -0500 @@ -4,8 +4,8 @@ from webhelpers import * from galaxy.util.json import to_json_string +from galaxy.util import hash_util from datetime import datetime, timedelta -import hashlib # If the date is more than one week ago, then display the actual date instead of in words def time_ago( x ): @@ -50,6 +50,6 @@ """ Return hex encoded md5 hash of string s """ - m = hashlib.md5() + m = hash_util.md5() m.update( s ) return m.hexdigest() \ No newline at end of file