Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1 | # $Id$ |
| 2 | # |
Gregory P. Smith | f805785 | 2007-09-09 20:25:00 +0000 | [diff] [blame] | 3 | # Copyright (C) 2005 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 | |
| 9 | new(name, string='') - returns a new hash object implementing the |
| 10 | given hash function; initializing the hash |
| 11 | using the given string data. |
| 12 | |
| 13 | Named constructor functions are also available, these are much faster |
| 14 | than using new(): |
| 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 | |
Gregory P. Smith | bde4007 | 2008-03-19 01:38:35 +0000 | [diff] [blame] | 21 | NOTE: If you want the adler32 or crc32 hash functions they are available in |
| 22 | the zlib module. |
| 23 | |
Georg Brandl | 7a4e804 | 2006-10-29 18:01:08 +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. |
Georg Brandl | 7a4e804 | 2006-10-29 18:01:08 +0000 | [diff] [blame] | 26 | |
| 27 | Hash objects have these methods: |
| 28 | - update(arg): Update the hash object with the string arg. Repeated calls |
| 29 | are equivalent to a single call with the concatenation of all |
| 30 | the arguments. |
| 31 | - digest(): Return the digest of the strings passed to the update() method |
| 32 | so far. This may contain non-ASCII characters, including |
| 33 | NUL bytes. |
| 34 | - hexdigest(): Like digest() except the digest is returned as a string of |
| 35 | double length, containing only hexadecimal digits. |
| 36 | - copy(): Return a copy (clone) of the hash object. This can be used to |
| 37 | efficiently compute the digests of strings that share a common |
| 38 | initial substring. |
| 39 | |
| 40 | For example, to obtain the digest of the string 'Nobody inspects the |
| 41 | spammish repetition': |
| 42 | |
| 43 | >>> import hashlib |
| 44 | >>> m = hashlib.md5() |
| 45 | >>> m.update("Nobody inspects") |
| 46 | >>> m.update(" the spammish repetition") |
| 47 | >>> m.digest() |
Gregory P. Smith | f07e5a9 | 2008-08-31 16:34:18 +0000 | [diff] [blame] | 48 | '\\xbbd\\x9c\\x83\\xdd\\x1e\\xa5\\xc9\\xd9\\xde\\xc9\\xa1\\x8d\\xf0\\xff\\xe9' |
Georg Brandl | 7a4e804 | 2006-10-29 18:01:08 +0000 | [diff] [blame] | 49 | |
| 50 | More condensed: |
| 51 | |
| 52 | >>> hashlib.sha224("Nobody inspects the spammish repetition").hexdigest() |
| 53 | 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2' |
| 54 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 55 | """ |
| 56 | |
Gregory P. Smith | 99954c9 | 2009-08-16 21:54:45 +0000 | [diff] [blame] | 57 | # This tuple and __get_builtin_constructor() must be modified if a new |
| 58 | # always available algorithm is added. |
| 59 | __always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512') |
| 60 | |
Gregory P. Smith | e6390a1 | 2010-03-01 02:01:47 +0000 | [diff] [blame] | 61 | algorithms = __always_supported |
| 62 | |
| 63 | __all__ = __always_supported + ('new', 'algorithms') |
Gregory P. Smith | 99954c9 | 2009-08-16 21:54:45 +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'): |
| 68 | import _sha |
| 69 | return _sha.new |
| 70 | elif name in ('MD5', 'md5'): |
| 71 | import _md5 |
| 72 | return _md5.new |
| 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 | 99954c9 | 2009-08-16 21:54:45 +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 | |
| 103 | def __py_new(name, string=''): |
| 104 | """new(name, string='') - Return a new hashing object using the named algorithm; |
| 105 | optionally initialized with a string. |
| 106 | """ |
| 107 | return __get_builtin_constructor(name)(string) |
| 108 | |
| 109 | |
| 110 | def __hash_new(name, string=''): |
| 111 | """new(name, string='') - Return a new hashing object using the named algorithm; |
| 112 | optionally initialized with a string. |
| 113 | """ |
| 114 | try: |
| 115 | return _hashlib.new(name, string) |
| 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. |
| 121 | return __get_builtin_constructor(name)(string) |
| 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 | 99954c9 | 2009-08-16 21:54:45 +0000 | [diff] [blame] | 127 | __get_hash = __get_openssl_constructor |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 128 | except ImportError: |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 129 | new = __py_new |
Gregory P. Smith | 99954c9 | 2009-08-16 21:54:45 +0000 | [diff] [blame] | 130 | __get_hash = __get_builtin_constructor |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 131 | |
Gregory P. Smith | 99954c9 | 2009-08-16 21:54:45 +0000 | [diff] [blame] | 132 | for __func_name in __always_supported: |
| 133 | # try them all, some may not work due to the OpenSSL |
| 134 | # version not supporting that algorithm. |
| 135 | try: |
| 136 | globals()[__func_name] = __get_hash(__func_name) |
| 137 | except ValueError: |
| 138 | import logging |
| 139 | logging.exception('code for hash %s was not found.', __func_name) |
| 140 | |
| 141 | # Cleanup locals() |
| 142 | del __always_supported, __func_name, __get_hash |
| 143 | del __py_new, __hash_new, __get_openssl_constructor |