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

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

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

11 12
Named constructor functions are also available, these are faster
than using new(name):
13 14 15

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

16 17 18
More algorithms may be available on your platform but the above are guaranteed
to exist.  See the algorithms_guaranteed and algorithms_available attributes
to find out what algorithm names can be passed to new().
19

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

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

Hash objects have these methods:
27
 - update(arg): Update the hash object with the bytes in arg. Repeated calls
28 29
                are equivalent to a single call with the concatenation of all
                the arguments.
30 31 32 33
 - 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.
34 35 36 37 38 39 40 41 42
 - 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()
43 44
    >>> m.update(b"Nobody inspects")
    >>> m.update(b" the spammish repetition")
45
    >>> m.digest()
46
    b'\\xbbd\\x9c\\x83\\xdd\\x1e\\xa5\\xc9\\xd9\\xde\\xc9\\xa1\\x8d\\xf0\\xff\\xe9'
47 48 49

More condensed:

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

53 54
"""

55 56 57 58
# 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')

59 60
algorithms_guaranteed = set(__always_supported)
algorithms_available = set(__always_supported)
61

62 63
__all__ = __always_supported + ('new', 'algorithms_guaranteed',
                                'algorithms_available')
64

65 66

def __get_builtin_constructor(name):
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    try:
        if name in ('SHA1', 'sha1'):
            import _sha1
            return _sha1.sha1
        elif name in ('MD5', 'md5'):
            import _md5
            return _md5.md5
        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
    except ImportError:
Gregory P. Smith's avatar
Gregory P. Smith committed
89
        pass  # no extension module, this hash is unsupported.
90

91 92 93 94 95 96 97 98 99 100 101 102 103
    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)
104 105


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


113 114 115
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).
116 117
    """
    try:
118
        return _hashlib.new(name, data)
119 120 121 122 123
    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.
124
        return __get_builtin_constructor(name)(data)
125 126 127 128 129


try:
    import _hashlib
    new = __hash_new
130
    __get_hash = __get_openssl_constructor
131 132
    algorithms_available = algorithms_available.union(
            _hashlib.openssl_md_meth_names)
133 134
except ImportError:
    new = __py_new
135 136 137 138 139 140 141 142 143 144
    __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)
145

146 147 148
# Cleanup locals()
del __always_supported, __func_name, __get_hash
del __py_new, __hash_new, __get_openssl_constructor