Christian Heimes | 3626a50 | 2013-10-19 14:12:02 +0200 | [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 | |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 7 | new(name, data=b'', **kwargs) - 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 | |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 14 | md5(), sha1(), sha224(), sha256(), sha384(), sha512(), blake2b(), blake2s(), |
| 15 | sha3_224, sha3_256, sha3_384, sha3_512, shake_128, and shake_256. |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 16 | |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 17 | More algorithms may be available on your platform but the above are guaranteed |
| 18 | to exist. See the algorithms_guaranteed and algorithms_available attributes |
| 19 | to find out what algorithm names can be passed to new(). |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 20 | |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 21 | NOTE: If you want the adler32 or crc32 hash functions they are available in |
| 22 | the zlib module. |
| 23 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 24 | Choose your hash function wisely. Some have known collision weaknesses. |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 25 | sha384 and sha512 will be slow on 32 bit platforms. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 26 | |
| 27 | Hash objects have these methods: |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 28 | - update(data): Update the hash object with the bytes in data. Repeated calls |
| 29 | are equivalent to a single call with the concatenation of all |
| 30 | the arguments. |
| 31 | - digest(): Return the digest of the bytes passed to the update() method |
| 32 | so far as a bytes object. |
| 33 | - hexdigest(): Like digest() except the digest is returned as a string |
| 34 | of double length, containing only hexadecimal digits. |
| 35 | - copy(): Return a copy (clone) of the hash object. This can be used to |
| 36 | efficiently compute the digests of datas that share a common |
| 37 | initial substring. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 38 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 39 | For example, to obtain the digest of the byte string 'Nobody inspects the |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 40 | spammish repetition': |
| 41 | |
| 42 | >>> import hashlib |
| 43 | >>> m = hashlib.md5() |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 44 | >>> m.update(b"Nobody inspects") |
| 45 | >>> m.update(b" the spammish repetition") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 46 | >>> m.digest() |
Gregory P. Smith | 6359450 | 2008-08-31 16:35:01 +0000 | [diff] [blame] | 47 | 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] | 48 | |
| 49 | More condensed: |
| 50 | |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 51 | >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 52 | 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2' |
| 53 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 54 | """ |
| 55 | |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 56 | # This tuple and __get_builtin_constructor() must be modified if a new |
| 57 | # always available algorithm is added. |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 58 | __always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512', |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 59 | 'blake2b', 'blake2s', |
| 60 | 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512', |
| 61 | 'shake_128', 'shake_256') |
| 62 | |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 63 | |
Raymond Hettinger | bf1d2bc | 2011-01-24 04:52:27 +0000 | [diff] [blame] | 64 | algorithms_guaranteed = set(__always_supported) |
| 65 | algorithms_available = set(__always_supported) |
Gregory P. Smith | 86508cc | 2010-03-01 02:05:26 +0000 | [diff] [blame] | 66 | |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 67 | __all__ = __always_supported + ('new', 'algorithms_guaranteed', |
Christian Heimes | 3626a50 | 2013-10-19 14:12:02 +0200 | [diff] [blame] | 68 | 'algorithms_available', 'pbkdf2_hmac') |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 69 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 70 | |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 71 | __builtin_constructor_cache = {} |
| 72 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 73 | def __get_builtin_constructor(name): |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 74 | cache = __builtin_constructor_cache |
| 75 | constructor = cache.get(name) |
| 76 | if constructor is not None: |
| 77 | return constructor |
Gregory P. Smith | 12c9d02 | 2011-05-14 15:15:49 -0700 | [diff] [blame] | 78 | try: |
| 79 | if name in ('SHA1', 'sha1'): |
| 80 | import _sha1 |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 81 | cache['SHA1'] = cache['sha1'] = _sha1.sha1 |
Gregory P. Smith | 12c9d02 | 2011-05-14 15:15:49 -0700 | [diff] [blame] | 82 | elif name in ('MD5', 'md5'): |
| 83 | import _md5 |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 84 | cache['MD5'] = cache['md5'] = _md5.md5 |
Gregory P. Smith | 12c9d02 | 2011-05-14 15:15:49 -0700 | [diff] [blame] | 85 | elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'): |
| 86 | import _sha256 |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 87 | cache['SHA224'] = cache['sha224'] = _sha256.sha224 |
| 88 | cache['SHA256'] = cache['sha256'] = _sha256.sha256 |
Gregory P. Smith | 12c9d02 | 2011-05-14 15:15:49 -0700 | [diff] [blame] | 89 | elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'): |
| 90 | import _sha512 |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 91 | cache['SHA384'] = cache['sha384'] = _sha512.sha384 |
| 92 | cache['SHA512'] = cache['sha512'] = _sha512.sha512 |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 93 | elif name in ('blake2b', 'blake2s'): |
| 94 | import _blake2 |
| 95 | cache['blake2b'] = _blake2.blake2b |
| 96 | cache['blake2s'] = _blake2.blake2s |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 97 | elif name in {'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512', |
| 98 | 'shake_128', 'shake_256'}: |
| 99 | import _sha3 |
| 100 | cache['sha3_224'] = _sha3.sha3_224 |
| 101 | cache['sha3_256'] = _sha3.sha3_256 |
| 102 | cache['sha3_384'] = _sha3.sha3_384 |
| 103 | cache['sha3_512'] = _sha3.sha3_512 |
| 104 | cache['shake_128'] = _sha3.shake_128 |
| 105 | cache['shake_256'] = _sha3.shake_256 |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 106 | except ImportError: |
Gregory P. Smith | a3221f8 | 2011-05-14 15:35:19 -0700 | [diff] [blame] | 107 | pass # no extension module, this hash is unsupported. |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 108 | |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 109 | constructor = cache.get(name) |
| 110 | if constructor is not None: |
| 111 | return constructor |
| 112 | |
Gregory P. Smith | 76c28f7 | 2012-07-21 21:19:53 -0700 | [diff] [blame] | 113 | raise ValueError('unsupported hash type ' + name) |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 114 | |
| 115 | |
| 116 | def __get_openssl_constructor(name): |
Christian Heimes | 32a2cee | 2016-09-07 02:35:13 +0200 | [diff] [blame] | 117 | if name in {'blake2b', 'blake2s'}: |
| 118 | # Prefer our blake2 implementation. |
| 119 | return __get_builtin_constructor(name) |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 120 | try: |
| 121 | f = getattr(_hashlib, 'openssl_' + name) |
| 122 | # Allow the C module to raise ValueError. The function will be |
| 123 | # defined but the hash not actually available thanks to OpenSSL. |
| 124 | f() |
| 125 | # Use the C function directly (very fast) |
| 126 | return f |
| 127 | except (AttributeError, ValueError): |
| 128 | return __get_builtin_constructor(name) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 129 | |
| 130 | |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 131 | def __py_new(name, data=b'', **kwargs): |
| 132 | """new(name, data=b'', **kwargs) - Return a new hashing object using the |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 133 | named algorithm; optionally initialized with data (which must be |
| 134 | a bytes-like object). |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 135 | """ |
| 136 | return __get_builtin_constructor(name)(data, **kwargs) |
| 137 | |
| 138 | |
| 139 | def __hash_new(name, data=b'', **kwargs): |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 140 | """new(name, data=b'') - Return a new hashing object using the named algorithm; |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 141 | optionally initialized with data (which must be a bytes-like object). |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 142 | """ |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 143 | if name in {'blake2b', 'blake2s'}: |
| 144 | # Prefer our blake2 implementation. |
| 145 | # OpenSSL 1.1.0 comes with a limited implementation of blake2b/s. |
| 146 | # It does neither support keyed blake2 nor advanced features like |
| 147 | # salt, personal, tree hashing or SSE. |
| 148 | return __get_builtin_constructor(name)(data, **kwargs) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 149 | try: |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 150 | return _hashlib.new(name, data) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 151 | except ValueError: |
| 152 | # If the _hashlib module (OpenSSL) doesn't support the named |
| 153 | # hash, try using our builtin implementations. |
| 154 | # This allows for SHA224/256 and SHA384/512 support even though |
| 155 | # 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] | 156 | return __get_builtin_constructor(name)(data) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 157 | |
| 158 | |
| 159 | try: |
| 160 | import _hashlib |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 161 | new = __hash_new |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 162 | __get_hash = __get_openssl_constructor |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 163 | algorithms_available = algorithms_available.union( |
| 164 | _hashlib.openssl_md_meth_names) |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 165 | except ImportError: |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 166 | new = __py_new |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 167 | __get_hash = __get_builtin_constructor |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 168 | |
Christian Heimes | e92ef13 | 2013-10-13 00:52:43 +0200 | [diff] [blame] | 169 | try: |
Christian Heimes | 3626a50 | 2013-10-19 14:12:02 +0200 | [diff] [blame] | 170 | # OpenSSL's PKCS5_PBKDF2_HMAC requires OpenSSL 1.0+ with HMAC and SHA |
Christian Heimes | e92ef13 | 2013-10-13 00:52:43 +0200 | [diff] [blame] | 171 | from _hashlib import pbkdf2_hmac |
| 172 | except ImportError: |
Christian Heimes | 3626a50 | 2013-10-19 14:12:02 +0200 | [diff] [blame] | 173 | _trans_5C = bytes((x ^ 0x5C) for x in range(256)) |
| 174 | _trans_36 = bytes((x ^ 0x36) for x in range(256)) |
| 175 | |
| 176 | def pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None): |
| 177 | """Password based key derivation function 2 (PKCS #5 v2.0) |
| 178 | |
| 179 | This Python implementations based on the hmac module about as fast |
| 180 | as OpenSSL's PKCS5_PBKDF2_HMAC for short passwords and much faster |
| 181 | for long passwords. |
| 182 | """ |
| 183 | if not isinstance(hash_name, str): |
| 184 | raise TypeError(hash_name) |
| 185 | |
| 186 | if not isinstance(password, (bytes, bytearray)): |
| 187 | password = bytes(memoryview(password)) |
| 188 | if not isinstance(salt, (bytes, bytearray)): |
| 189 | salt = bytes(memoryview(salt)) |
| 190 | |
| 191 | # Fast inline HMAC implementation |
| 192 | inner = new(hash_name) |
| 193 | outer = new(hash_name) |
| 194 | blocksize = getattr(inner, 'block_size', 64) |
| 195 | if len(password) > blocksize: |
| 196 | password = new(hash_name, password).digest() |
| 197 | password = password + b'\x00' * (blocksize - len(password)) |
| 198 | inner.update(password.translate(_trans_36)) |
| 199 | outer.update(password.translate(_trans_5C)) |
| 200 | |
| 201 | def prf(msg, inner=inner, outer=outer): |
| 202 | # PBKDF2_HMAC uses the password as key. We can re-use the same |
Serhiy Storchaka | 56a6d85 | 2014-12-01 18:28:43 +0200 | [diff] [blame] | 203 | # digest objects and just update copies to skip initialization. |
Christian Heimes | 3626a50 | 2013-10-19 14:12:02 +0200 | [diff] [blame] | 204 | icpy = inner.copy() |
| 205 | ocpy = outer.copy() |
| 206 | icpy.update(msg) |
| 207 | ocpy.update(icpy.digest()) |
| 208 | return ocpy.digest() |
| 209 | |
| 210 | if iterations < 1: |
| 211 | raise ValueError(iterations) |
| 212 | if dklen is None: |
| 213 | dklen = outer.digest_size |
| 214 | if dklen < 1: |
| 215 | raise ValueError(dklen) |
| 216 | |
| 217 | dkey = b'' |
| 218 | loop = 1 |
| 219 | from_bytes = int.from_bytes |
| 220 | while len(dkey) < dklen: |
| 221 | prev = prf(salt + loop.to_bytes(4, 'big')) |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 222 | # endianness doesn't matter here as long to / from use the same |
Christian Heimes | 3626a50 | 2013-10-19 14:12:02 +0200 | [diff] [blame] | 223 | rkey = int.from_bytes(prev, 'big') |
| 224 | for i in range(iterations - 1): |
| 225 | prev = prf(prev) |
| 226 | # rkey = rkey ^ prev |
| 227 | rkey ^= from_bytes(prev, 'big') |
| 228 | loop += 1 |
| 229 | dkey += rkey.to_bytes(inner.digest_size, 'big') |
| 230 | |
| 231 | return dkey[:dklen] |
| 232 | |
Christian Heimes | 39093e9 | 2016-09-06 20:22:28 +0200 | [diff] [blame] | 233 | try: |
| 234 | # OpenSSL's scrypt requires OpenSSL 1.1+ |
| 235 | from _hashlib import scrypt |
| 236 | except ImportError: |
| 237 | pass |
| 238 | |
Christian Heimes | e92ef13 | 2013-10-13 00:52:43 +0200 | [diff] [blame] | 239 | |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 240 | for __func_name in __always_supported: |
| 241 | # try them all, some may not work due to the OpenSSL |
| 242 | # version not supporting that algorithm. |
| 243 | try: |
| 244 | globals()[__func_name] = __get_hash(__func_name) |
| 245 | except ValueError: |
| 246 | import logging |
| 247 | logging.exception('code for hash %s was not found.', __func_name) |
| 248 | |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 249 | |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 250 | # Cleanup locals() |
| 251 | del __always_supported, __func_name, __get_hash |
| 252 | del __py_new, __hash_new, __get_openssl_constructor |