Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1 | # $Id$ |
| 2 | # |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 3 | # Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 4 | # Licensed to PSF under a Contributor Agreement. |
| 5 | # |
| 6 | |
| 7 | __doc__ = """hashlib module - A common interface to many hash functions. |
| 8 | |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 9 | new(name, data=b'') - returns a new hash object implementing the |
| 10 | given hash function; initializing the hash |
| 11 | using the given binary data. |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 12 | |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 13 | Named constructor functions are also available, these are faster |
| 14 | than using new(name): |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 15 | |
| 16 | md5(), sha1(), sha224(), sha256(), sha384(), and sha512() |
| 17 | |
| 18 | More algorithms may be available on your platform but the above are |
| 19 | guaranteed to exist. |
| 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: |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 28 | - 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] | 29 | are equivalent to a single call with the concatenation of all |
| 30 | the arguments. |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 31 | - digest(): Return the digest of the bytes passed to the update() method |
| 32 | so far. |
| 33 | - hexdigest(): Like digest() except the digest is returned as a unicode |
| 34 | object of double length, containing only hexadecimal digits. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 35 | - copy(): Return a copy (clone) of the hash object. This can be used to |
| 36 | efficiently compute the digests of strings that share a common |
| 37 | initial substring. |
| 38 | |
| 39 | For example, to obtain the digest of the string 'Nobody inspects the |
| 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. |
| 58 | __always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512') |
| 59 | |
| 60 | __all__ = __always_supported + ('new',) |
| 61 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 62 | |
| 63 | def __get_builtin_constructor(name): |
| 64 | if name in ('SHA1', 'sha1'): |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 65 | import _sha1 |
| 66 | return _sha1.sha1 |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 67 | elif name in ('MD5', 'md5'): |
| 68 | import _md5 |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 69 | return _md5.md5 |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 70 | elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'): |
| 71 | import _sha256 |
| 72 | bs = name[3:] |
| 73 | if bs == '256': |
| 74 | return _sha256.sha256 |
| 75 | elif bs == '224': |
| 76 | return _sha256.sha224 |
| 77 | elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'): |
| 78 | import _sha512 |
| 79 | bs = name[3:] |
| 80 | if bs == '512': |
| 81 | return _sha512.sha512 |
| 82 | elif bs == '384': |
| 83 | return _sha512.sha384 |
| 84 | |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 85 | raise ValueError('unsupported hash type %s' % name) |
| 86 | |
| 87 | |
| 88 | def __get_openssl_constructor(name): |
| 89 | try: |
| 90 | f = getattr(_hashlib, 'openssl_' + name) |
| 91 | # Allow the C module to raise ValueError. The function will be |
| 92 | # defined but the hash not actually available thanks to OpenSSL. |
| 93 | f() |
| 94 | # Use the C function directly (very fast) |
| 95 | return f |
| 96 | except (AttributeError, ValueError): |
| 97 | return __get_builtin_constructor(name) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 98 | |
| 99 | |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 100 | def __py_new(name, data=b''): |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 101 | """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] | 102 | optionally initialized with data (which must be bytes). |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 103 | """ |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 104 | return __get_builtin_constructor(name)(data) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 105 | |
| 106 | |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 107 | def __hash_new(name, data=b''): |
| 108 | """new(name, data=b'') - Return a new hashing object using the named algorithm; |
| 109 | optionally initialized with data (which must be bytes). |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 110 | """ |
| 111 | try: |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 112 | return _hashlib.new(name, data) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 113 | except ValueError: |
| 114 | # If the _hashlib module (OpenSSL) doesn't support the named |
| 115 | # hash, try using our builtin implementations. |
| 116 | # This allows for SHA224/256 and SHA384/512 support even though |
| 117 | # 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] | 118 | return __get_builtin_constructor(name)(data) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 119 | |
| 120 | |
| 121 | try: |
| 122 | import _hashlib |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 123 | new = __hash_new |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 124 | __get_hash = __get_openssl_constructor |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 125 | except ImportError: |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 126 | new = __py_new |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 127 | __get_hash = __get_builtin_constructor |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 128 | |
Gregory P. Smith | d8fe8bf | 2009-08-16 22:08:56 +0000 | [diff] [blame] | 129 | for __func_name in __always_supported: |
| 130 | # try them all, some may not work due to the OpenSSL |
| 131 | # version not supporting that algorithm. |
| 132 | try: |
| 133 | globals()[__func_name] = __get_hash(__func_name) |
| 134 | except ValueError: |
| 135 | import logging |
| 136 | logging.exception('code for hash %s was not found.', __func_name) |
| 137 | |
| 138 | # Cleanup locals() |
| 139 | del __always_supported, __func_name, __get_hash |
| 140 | del __py_new, __hash_new, __get_openssl_constructor |