PageRenderTime 98ms CodeModel.GetById 36ms app.highlight 7ms RepoModel.GetById 23ms app.codeStats 0ms

/Lib/hashlib.py

http://unladen-swallow.googlecode.com/
Python | 141 lines | 135 code | 1 blank | 5 comment | 0 complexity | f56dbc5ee6774e857a7ef07accdbd19b MD5 | raw file
  1# $Id: hashlib.py 66093 2008-08-31 16:34:18Z gregory.p.smith $
  2#
  3#  Copyright (C) 2005   Gregory P. Smith (greg@krypto.org)
  4#  Licensed to PSF under a Contributor Agreement.
  5#
  6
  7__doc__ = """hashlib module - A common interface to many hash functions.
  8
  9new(name, string='') - returns a new hash object implementing the
 10                       given hash function; initializing the hash
 11                       using the given string data.
 12
 13Named constructor functions are also available, these are much faster
 14than using new():
 15
 16md5(), sha1(), sha224(), sha256(), sha384(), and sha512()
 17
 18More algorithms may be available on your platform but the above are
 19guaranteed to exist.
 20
 21NOTE: If you want the adler32 or crc32 hash functions they are available in
 22the zlib module.
 23
 24Choose your hash function wisely.  Some have known collision weaknesses.
 25sha384 and sha512 will be slow on 32 bit platforms.
 26
 27Hash objects have these methods:
 28 - update(arg): Update the hash object with the string arg. Repeated calls
 29                are equivalent to a single call with the concatenation of all
 30                the arguments.
 31 - digest():    Return the digest of the strings passed to the update() method
 32                so far. This may contain non-ASCII characters, including
 33                NUL bytes.
 34 - hexdigest(): Like digest() except the digest is returned as a string of
 35                double length, containing only hexadecimal digits.
 36 - copy():      Return a copy (clone) of the hash object. This can be used to
 37                efficiently compute the digests of strings that share a common
 38                initial substring.
 39
 40For example, to obtain the digest of the string 'Nobody inspects the
 41spammish repetition':
 42
 43    >>> import hashlib
 44    >>> m = hashlib.md5()
 45    >>> m.update("Nobody inspects")
 46    >>> m.update(" the spammish repetition")
 47    >>> m.digest()
 48    '\\xbbd\\x9c\\x83\\xdd\\x1e\\xa5\\xc9\\xd9\\xde\\xc9\\xa1\\x8d\\xf0\\xff\\xe9'
 49
 50More condensed:
 51
 52    >>> hashlib.sha224("Nobody inspects the spammish repetition").hexdigest()
 53    'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
 54
 55"""
 56
 57
 58def __get_builtin_constructor(name):
 59    if name in ('SHA1', 'sha1'):
 60        import _sha
 61        return _sha.new
 62    elif name in ('MD5', 'md5'):
 63        import _md5
 64        return _md5.new
 65    elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):
 66        import _sha256
 67        bs = name[3:]
 68        if bs == '256':
 69            return _sha256.sha256
 70        elif bs == '224':
 71            return _sha256.sha224
 72    elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):
 73        import _sha512
 74        bs = name[3:]
 75        if bs == '512':
 76            return _sha512.sha512
 77        elif bs == '384':
 78            return _sha512.sha384
 79
 80    raise ValueError, "unsupported hash type"
 81
 82
 83def __py_new(name, string=''):
 84    """new(name, string='') - Return a new hashing object using the named algorithm;
 85    optionally initialized with a string.
 86    """
 87    return __get_builtin_constructor(name)(string)
 88
 89
 90def __hash_new(name, string=''):
 91    """new(name, string='') - Return a new hashing object using the named algorithm;
 92    optionally initialized with a string.
 93    """
 94    try:
 95        return _hashlib.new(name, string)
 96    except ValueError:
 97        # If the _hashlib module (OpenSSL) doesn't support the named
 98        # hash, try using our builtin implementations.
 99        # This allows for SHA224/256 and SHA384/512 support even though
100        # the OpenSSL library prior to 0.9.8 doesn't provide them.
101        return __get_builtin_constructor(name)(string)
102
103
104try:
105    import _hashlib
106    # use the wrapper of the C implementation
107    new = __hash_new
108
109    for opensslFuncName in filter(lambda n: n.startswith('openssl_'), dir(_hashlib)):
110        funcName = opensslFuncName[len('openssl_'):]
111        try:
112            # try them all, some may not work due to the OpenSSL
113            # version not supporting that algorithm.
114            f = getattr(_hashlib, opensslFuncName)
115            f()
116            # Use the C function directly (very fast)
117            exec funcName + ' = f'
118        except ValueError:
119            try:
120                # Use the builtin implementation directly (fast)
121                exec funcName + ' = __get_builtin_constructor(funcName)'
122            except ValueError:
123                # this one has no builtin implementation, don't define it
124                pass
125    # clean up our locals
126    del f
127    del opensslFuncName
128    del funcName
129
130except ImportError:
131    # We don't have the _hashlib OpenSSL module?
132    # use the built in legacy interfaces via a wrapper function
133    new = __py_new
134
135    # lookup the C function to use directly for the named constructors
136    md5 = __get_builtin_constructor('md5')
137    sha1 = __get_builtin_constructor('sha1')
138    sha224 = __get_builtin_constructor('sha224')
139    sha256 = __get_builtin_constructor('sha256')
140    sha384 = __get_builtin_constructor('sha384')
141    sha512 = __get_builtin_constructor('sha512')