Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 1 | # Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 2 | # Licensed to PSF under a Contributor Agreement. |
| 3 | # |
| 4 | |
| 5 | __doc__ = """hashlib module - A common interface to many hash functions. |
| 6 | |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 7 | new(name, data=b'') - returns a new hash object implementing the |
| 8 | given hash function; initializing the hash |
| 9 | using the given binary data. |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 10 | |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 11 | Named constructor functions are also available, these are faster |
| 12 | than using new(name): |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 13 | |
| 14 | md5(), sha1(), sha224(), sha256(), sha384(), and sha512() |
| 15 | |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 16 | More algorithms may be available on your platform but the above are guaranteed |
| 17 | to exist. See the algorithms_guaranteed and algorithms_available attributes |
| 18 | to find out what algorithm names can be passed to new(). |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 19 | |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 20 | NOTE: If you want the adler32 or crc32 hash functions they are available in |
| 21 | the zlib module. |
| 22 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 23 | Choose your hash function wisely. Some have known collision weaknesses. |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 24 | sha384 and sha512 will be slow on 32 bit platforms. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 25 | |
| 26 | Hash objects have these methods: |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 27 | - update(arg): Update the hash object with the bytes in arg. Repeated calls |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 28 | are equivalent to a single call with the concatenation of all |
| 29 | the arguments. |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 30 | - digest(): Return the digest of the bytes passed to the update() method |
| 31 | so far. |
| 32 | - hexdigest(): Like digest() except the digest is returned as a unicode |
| 33 | object of double length, containing only hexadecimal digits. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 34 | - copy(): Return a copy (clone) of the hash object. This can be used to |
| 35 | efficiently compute the digests of strings that share a common |
| 36 | initial substring. |
| 37 | |
| 38 | For example, to obtain the digest of the string 'Nobody inspects the |
| 39 | spammish repetition': |
| 40 | |
| 41 | >>> import hashlib |
| 42 | >>> m = hashlib.md5() |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 43 | >>> m.update(b"Nobody inspects") |
| 44 | >>> m.update(b" the spammish repetition") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 45 | >>> m.digest() |
Gregory P. Smith | 6359450 | 2008-08-31 16:35:01 +0000 | [diff] [blame] | 46 | b'\\xbbd\\x9c\\x83\\xdd\\x1e\\xa5\\xc9\\xd9\\xde\\xc9\\xa1\\x8d\\xf0\\xff\\xe9' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 47 | |
| 48 | More condensed: |
| 49 | |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 50 | >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 51 | 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2' |
| 52 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 53 | """ |
| 54 | |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 55 | # This tuple and __get_builtin_constructor() must be modified if a new |
| 56 | # always available algorithm is added. |
| 57 | __always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512') |
| 58 | |
Raymond Hettinger | bf1d2bc | 2011-01-24 04:52:27 +0000 | [diff] [blame] | 59 | algorithms_guaranteed = set(__always_supported) |
| 60 | algorithms_available = set(__always_supported) |
Gregory P. Smith | 86508cc | 2010-03-01 02:05:26 +0000 | [diff] [blame] | 61 | |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 62 | __all__ = __always_supported + ('new', 'algorithms_guaranteed', |
| 63 | 'algorithms_available') |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 64 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 65 | |
| 66 | def __get_builtin_constructor(name): |
| 67 | if name in ('SHA1', 'sha1'): |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 68 | import _sha1 |
| 69 | return _sha1.sha1 |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 70 | elif name in ('MD5', 'md5'): |
| 71 | import _md5 |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 72 | return _md5.md5 |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 73 | elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'): |
| 74 | import _sha256 |
| 75 | bs = name[3:] |
| 76 | if bs == '256': |
| 77 | return _sha256.sha256 |
| 78 | elif bs == '224': |
| 79 | return _sha256.sha224 |
| 80 | elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'): |
| 81 | import _sha512 |
| 82 | bs = name[3:] |
| 83 | if bs == '512': |
| 84 | return _sha512.sha512 |
| 85 | elif bs == '384': |
| 86 | return _sha512.sha384 |
| 87 | |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 88 | raise ValueError('unsupported hash type %s' % name) |
| 89 | |
| 90 | |
| 91 | def __get_openssl_constructor(name): |
| 92 | try: |
| 93 | f = getattr(_hashlib, 'openssl_' + name) |
| 94 | # Allow the C module to raise ValueError. The function will be |
| 95 | # defined but the hash not actually available thanks to OpenSSL. |
| 96 | f() |
| 97 | # Use the C function directly (very fast) |
| 98 | return f |
| 99 | except (AttributeError, ValueError): |
| 100 | return __get_builtin_constructor(name) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 101 | |
| 102 | |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 103 | def __py_new(name, data=b''): |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 104 | """new(name, data=b'') - Return a new hashing object using the named algorithm; |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 105 | optionally initialized with data (which must be bytes). |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 106 | """ |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 107 | return __get_builtin_constructor(name)(data) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 108 | |
| 109 | |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 110 | def __hash_new(name, data=b''): |
| 111 | """new(name, data=b'') - Return a new hashing object using the named algorithm; |
| 112 | optionally initialized with data (which must be bytes). |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 113 | """ |
| 114 | try: |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 115 | return _hashlib.new(name, data) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 116 | except ValueError: |
| 117 | # If the _hashlib module (OpenSSL) doesn't support the named |
| 118 | # hash, try using our builtin implementations. |
| 119 | # This allows for SHA224/256 and SHA384/512 support even though |
| 120 | # the OpenSSL library prior to 0.9.8 doesn't provide them. |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 121 | return __get_builtin_constructor(name)(data) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 122 | |
| 123 | |
| 124 | try: |
| 125 | import _hashlib |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 126 | new = __hash_new |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 127 | __get_hash = __get_openssl_constructor |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 128 | algorithms_available = algorithms_available.union( |
| 129 | _hashlib.openssl_md_meth_names) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 130 | except ImportError: |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 131 | new = __py_new |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 132 | __get_hash = __get_builtin_constructor |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 133 | |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 134 | for __func_name in __always_supported: |
| 135 | # try them all, some may not work due to the OpenSSL |
| 136 | # version not supporting that algorithm. |
| 137 | try: |
| 138 | globals()[__func_name] = __get_hash(__func_name) |
| 139 | except ValueError: |
| 140 | import logging |
| 141 | logging.exception('code for hash %s was not found.', __func_name) |
| 142 | |
| 143 | # Cleanup locals() |
| 144 | del __always_supported, __func_name, __get_hash |
| 145 | del __py_new, __hash_new, __get_openssl_constructor |