root/galaxy-central/lib/galaxy/util/hash_util.py

リビジョン 2, 0.9 KB (コミッタ: hatakeyama, 14 年 前)

import galaxy-central

行番号 
1"""
2Utility functions for bi-directional Python version compatibility.  Python 2.5
3introduced hashlib which replaced sha in Python 2.4 and previous versions.
4"""
5
6import 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 (!)
10try:
11    import hashlib
12    sha1 = hashlib.sha1
13    sha = sha1
14    md5 = hashlib.md5
15except ImportError, e:
16    from sha import new as sha1
17    import sha
18    from md5 import new as md5
19import hmac
20
21log = logging.getLogger( __name__ )
22
23def 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
33def hmac_new( key, value ):
34    return hmac.new( key, value, sha ).hexdigest()
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。