| 1 | """ |
|---|
| 2 | Utility functions for bi-directional Python version compatibility. Python 2.5 |
|---|
| 3 | introduced hashlib which replaced sha in Python 2.4 and previous versions. |
|---|
| 4 | """ |
|---|
| 5 | |
|---|
| 6 | import sys, logging |
|---|
| 7 | |
|---|
| 8 | # Use hashlib module if for Python 2.5+, fall back on old sha and md5 modules |
|---|
| 9 | # sha1 requires explicit calls to new if also being passed to hmac (!) |
|---|
| 10 | try: |
|---|
| 11 | import hashlib |
|---|
| 12 | sha1 = hashlib.sha1 |
|---|
| 13 | sha = sha1 |
|---|
| 14 | md5 = hashlib.md5 |
|---|
| 15 | except ImportError, e: |
|---|
| 16 | from sha import new as sha1 |
|---|
| 17 | import sha |
|---|
| 18 | from md5 import new as md5 |
|---|
| 19 | import hmac |
|---|
| 20 | |
|---|
| 21 | log = logging.getLogger( __name__ ) |
|---|
| 22 | |
|---|
| 23 | def new_secure_hash( text_type=None ): |
|---|
| 24 | """ |
|---|
| 25 | Returns either a sha1 hash object (if called with no arguments), or a |
|---|
| 26 | hexdigest of the sha1 hash of the argument `text_type`. |
|---|
| 27 | """ |
|---|
| 28 | if text_type: |
|---|
| 29 | return sha1( text_type ).hexdigest() |
|---|
| 30 | else: |
|---|
| 31 | return sha1() |
|---|
| 32 | |
|---|
| 33 | def hmac_new( key, value ): |
|---|
| 34 | return hmac.new( key, value, sha ).hexdigest() |
|---|