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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 21 | Choose your hash function wisely. Some have known collision weaknesses. |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 22 | sha384 and sha512 will be slow on 32 bit platforms. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 23 | |
| 24 | Hash objects have these methods: |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 25 | - 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] | 26 | are equivalent to a single call with the concatenation of all |
| 27 | the arguments. |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 28 | - digest(): Return the digest of the bytes passed to the update() method |
| 29 | so far. |
| 30 | - hexdigest(): Like digest() except the digest is returned as a unicode |
| 31 | object of double length, containing only hexadecimal digits. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 32 | - copy(): Return a copy (clone) of the hash object. This can be used to |
| 33 | efficiently compute the digests of strings that share a common |
| 34 | initial substring. |
| 35 | |
| 36 | For example, to obtain the digest of the string 'Nobody inspects the |
| 37 | spammish repetition': |
| 38 | |
| 39 | >>> import hashlib |
| 40 | >>> m = hashlib.md5() |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 41 | >>> m.update(b"Nobody inspects") |
| 42 | >>> m.update(b" the spammish repetition") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 43 | >>> m.digest() |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 44 | 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] | 45 | |
| 46 | More condensed: |
| 47 | |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 48 | >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 49 | 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2' |
| 50 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 51 | """ |
| 52 | |
| 53 | |
| 54 | def __get_builtin_constructor(name): |
| 55 | if name in ('SHA1', 'sha1'): |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 56 | import _sha1 |
| 57 | return _sha1.sha1 |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 58 | elif name in ('MD5', 'md5'): |
| 59 | import _md5 |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 60 | return _md5.md5 |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 61 | elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'): |
| 62 | import _sha256 |
| 63 | bs = name[3:] |
| 64 | if bs == '256': |
| 65 | return _sha256.sha256 |
| 66 | elif bs == '224': |
| 67 | return _sha256.sha224 |
| 68 | elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'): |
| 69 | import _sha512 |
| 70 | bs = name[3:] |
| 71 | if bs == '512': |
| 72 | return _sha512.sha512 |
| 73 | elif bs == '384': |
| 74 | return _sha512.sha384 |
| 75 | |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 76 | raise ValueError("unsupported hash type") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 77 | |
| 78 | |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 79 | def __py_new(name, data=b''): |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 80 | """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] | 81 | optionally initialized with data (which must be bytes). |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 82 | """ |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 83 | return __get_builtin_constructor(name)(data) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 84 | |
| 85 | |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 86 | def __hash_new(name, data=b''): |
| 87 | """new(name, data=b'') - Return a new hashing object using the named algorithm; |
| 88 | optionally initialized with data (which must be bytes). |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 89 | """ |
| 90 | try: |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 91 | return _hashlib.new(name, data) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 92 | except ValueError: |
| 93 | # If the _hashlib module (OpenSSL) doesn't support the named |
| 94 | # hash, try using our builtin implementations. |
| 95 | # This allows for SHA224/256 and SHA384/512 support even though |
| 96 | # 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] | 97 | return __get_builtin_constructor(name)(data) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 98 | |
| 99 | |
| 100 | try: |
| 101 | import _hashlib |
| 102 | # use the wrapper of the C implementation |
| 103 | new = __hash_new |
| 104 | |
| 105 | for opensslFuncName in filter(lambda n: n.startswith('openssl_'), dir(_hashlib)): |
| 106 | funcName = opensslFuncName[len('openssl_'):] |
| 107 | try: |
| 108 | # try them all, some may not work due to the OpenSSL |
| 109 | # version not supporting that algorithm. |
| 110 | f = getattr(_hashlib, opensslFuncName) |
| 111 | f() |
| 112 | # Use the C function directly (very fast) |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 113 | exec(funcName + ' = f') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 114 | except ValueError: |
| 115 | try: |
| 116 | # Use the builtin implementation directly (fast) |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 117 | exec(funcName + ' = __get_builtin_constructor(funcName)') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 118 | except ValueError: |
| 119 | # this one has no builtin implementation, don't define it |
| 120 | pass |
| 121 | # clean up our locals |
| 122 | del f |
| 123 | del opensslFuncName |
| 124 | del funcName |
| 125 | |
| 126 | except ImportError: |
| 127 | # We don't have the _hashlib OpenSSL module? |
| 128 | # use the built in legacy interfaces via a wrapper function |
| 129 | new = __py_new |
| 130 | |
| 131 | # lookup the C function to use directly for the named constructors |
| 132 | md5 = __get_builtin_constructor('md5') |
| 133 | sha1 = __get_builtin_constructor('sha1') |
| 134 | sha224 = __get_builtin_constructor('sha224') |
| 135 | sha256 = __get_builtin_constructor('sha256') |
| 136 | sha384 = __get_builtin_constructor('sha384') |
| 137 | sha512 = __get_builtin_constructor('sha512') |