hashlib.py 4.69 KB
Newer Older
1 2
# $Id$
#
3
#  Copyright (C) 2005-2007   Gregory P. Smith (greg@krypto.org)
4 5 6 7 8
#  Licensed to PSF under a Contributor Agreement.
#

__doc__ = """hashlib module - A common interface to many hash functions.

9 10 11
new(name, data=b'') - returns a new hash object implementing the
                      given hash function; initializing the hash
                      using the given binary data.
12

13 14
Named constructor functions are also available, these are faster
than using new(name):
15 16 17 18 19 20

md5(), sha1(), sha224(), sha256(), sha384(), and sha512()

More algorithms may be available on your platform but the above are
guaranteed to exist.

Christian Heimes's avatar
Christian Heimes committed
21 22 23
NOTE: If you want the adler32 or crc32 hash functions they are available in
the zlib module.

24
Choose your hash function wisely.  Some have known collision weaknesses.
25
sha384 and sha512 will be slow on 32 bit platforms.
26 27

Hash objects have these methods:
28
 - update(arg): Update the hash object with the bytes in arg. Repeated calls
29 30
                are equivalent to a single call with the concatenation of all
                the arguments.
31 32 33 34
 - digest():    Return the digest of the bytes passed to the update() method
                so far.
 - hexdigest(): Like digest() except the digest is returned as a unicode
                object of double length, containing only hexadecimal digits.
35 36 37 38 39 40 41 42 43
 - copy():      Return a copy (clone) of the hash object. This can be used to
                efficiently compute the digests of strings that share a common
                initial substring.

For example, to obtain the digest of the string 'Nobody inspects the
spammish repetition':

    >>> import hashlib
    >>> m = hashlib.md5()
44 45
    >>> m.update(b"Nobody inspects")
    >>> m.update(b" the spammish repetition")
46
    >>> m.digest()
47
    b'\\xbbd\\x9c\\x83\\xdd\\x1e\\xa5\\xc9\\xd9\\xde\\xc9\\xa1\\x8d\\xf0\\xff\\xe9'
48 49 50

More condensed:

51
    >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
52 53
    'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'

54 55
"""

56 57 58 59
# This tuple and __get_builtin_constructor() must be modified if a new
# always available algorithm is added.
__always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')

60 61 62
algorithms = __always_supported

__all__ = __always_supported + ('new', 'algorithms')
63

64 65 66

def __get_builtin_constructor(name):
    if name in ('SHA1', 'sha1'):
67 68
        import _sha1
        return _sha1.sha1
69 70
    elif name in ('MD5', 'md5'):
        import _md5
71
        return _md5.md5
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):
        import _sha256
        bs = name[3:]
        if bs == '256':
            return _sha256.sha256
        elif bs == '224':
            return _sha256.sha224
    elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):
        import _sha512
        bs = name[3:]
        if bs == '512':
            return _sha512.sha512
        elif bs == '384':
            return _sha512.sha384

87 88 89 90 91 92 93 94 95 96 97 98 99
    raise ValueError('unsupported hash type %s' % name)


def __get_openssl_constructor(name):
    try:
        f = getattr(_hashlib, 'openssl_' + name)
        # Allow the C module to raise ValueError.  The function will be
        # defined but the hash not actually available thanks to OpenSSL.
        f()
        # Use the C function directly (very fast)
        return f
    except (AttributeError, ValueError):
        return __get_builtin_constructor(name)
100 101


102
def __py_new(name, data=b''):
103
    """new(name, data=b'') - Return a new hashing object using the named algorithm;
104
    optionally initialized with data (which must be bytes).
105
    """
106
    return __get_builtin_constructor(name)(data)
107 108


109 110 111
def __hash_new(name, data=b''):
    """new(name, data=b'') - Return a new hashing object using the named algorithm;
    optionally initialized with data (which must be bytes).
112 113
    """
    try:
114
        return _hashlib.new(name, data)
115 116 117 118 119
    except ValueError:
        # If the _hashlib module (OpenSSL) doesn't support the named
        # hash, try using our builtin implementations.
        # This allows for SHA224/256 and SHA384/512 support even though
        # the OpenSSL library prior to 0.9.8 doesn't provide them.
120
        return __get_builtin_constructor(name)(data)
121 122 123 124 125


try:
    import _hashlib
    new = __hash_new
126
    __get_hash = __get_openssl_constructor
127 128
except ImportError:
    new = __py_new
129 130 131 132 133 134 135 136 137 138
    __get_hash = __get_builtin_constructor

for __func_name in __always_supported:
    # try them all, some may not work due to the OpenSSL
    # version not supporting that algorithm.
    try:
        globals()[__func_name] = __get_hash(__func_name)
    except ValueError:
        import logging
        logging.exception('code for hash %s was not found.', __func_name)
139

140 141 142
# Cleanup locals()
del __always_supported, __func_name, __get_hash
del __py_new, __hash_new, __get_openssl_constructor