Changeset 391 for python/trunk/Lib/hashlib.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/hashlib.py
r2 r391 1 # $Id : hashlib.py 66093 2008-08-31 16:34:18Z gregory.p.smith$1 # $Id$ 2 2 # 3 3 # Copyright (C) 2005 Gregory P. Smith (greg@krypto.org) … … 55 55 """ 56 56 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 61 algorithms = __always_supported 62 63 __all__ = __always_supported + ('new', 'algorithms') 64 57 65 58 66 def __get_builtin_constructor(name): 59 if name in ('SHA1', 'sha1'): 60 import _sha 61 return _sha.new 62 elif name in ('MD5', 'md5'): 63 import _md5 64 return _md5.new 65 elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'): 66 import _sha256 67 bs = name[3:] 68 if bs == '256': 69 return _sha256.sha256 70 elif bs == '224': 71 return _sha256.sha224 72 elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'): 73 import _sha512 74 bs = name[3:] 75 if bs == '512': 76 return _sha512.sha512 77 elif bs == '384': 78 return _sha512.sha384 67 try: 68 if name in ('SHA1', 'sha1'): 69 import _sha 70 return _sha.new 71 elif name in ('MD5', 'md5'): 72 import _md5 73 return _md5.new 74 elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'): 75 import _sha256 76 bs = name[3:] 77 if bs == '256': 78 return _sha256.sha256 79 elif bs == '224': 80 return _sha256.sha224 81 elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'): 82 import _sha512 83 bs = name[3:] 84 if bs == '512': 85 return _sha512.sha512 86 elif bs == '384': 87 return _sha512.sha384 88 except ImportError: 89 pass # no extension module, this hash is unsupported. 79 90 80 raise ValueError, "unsupported hash type" 91 raise ValueError('unsupported hash type ' + name) 92 93 94 def __get_openssl_constructor(name): 95 try: 96 f = getattr(_hashlib, 'openssl_' + name) 97 # Allow the C module to raise ValueError. The function will be 98 # defined but the hash not actually available thanks to OpenSSL. 99 f() 100 # Use the C function directly (very fast) 101 return f 102 except (AttributeError, ValueError): 103 return __get_builtin_constructor(name) 81 104 82 105 … … 104 127 try: 105 128 import _hashlib 106 # use the wrapper of the C implementation107 129 new = __hash_new 130 __get_hash = __get_openssl_constructor 131 except ImportError: 132 new = __py_new 133 __get_hash = __get_builtin_constructor 108 134 109 for opensslFuncName in filter(lambda n: n.startswith('openssl_'), dir(_hashlib)): 110 funcName = opensslFuncName[len('openssl_'):] 111 try: 112 # try them all, some may not work due to the OpenSSL 113 # version not supporting that algorithm. 114 f = getattr(_hashlib, opensslFuncName) 115 f() 116 # Use the C function directly (very fast) 117 exec funcName + ' = f' 118 except ValueError: 119 try: 120 # Use the builtin implementation directly (fast) 121 exec funcName + ' = __get_builtin_constructor(funcName)' 122 except ValueError: 123 # this one has no builtin implementation, don't define it 124 pass 125 # clean up our locals 126 del f 127 del opensslFuncName 128 del funcName 135 for __func_name in __always_supported: 136 # try them all, some may not work due to the OpenSSL 137 # version not supporting that algorithm. 138 try: 139 globals()[__func_name] = __get_hash(__func_name) 140 except ValueError: 141 import logging 142 logging.exception('code for hash %s was not found.', __func_name) 129 143 130 except ImportError: 131 # We don't have the _hashlib OpenSSL module? 132 # use the built in legacy interfaces via a wrapper function 133 new = __py_new 134 135 # lookup the C function to use directly for the named constructors 136 md5 = __get_builtin_constructor('md5') 137 sha1 = __get_builtin_constructor('sha1') 138 sha224 = __get_builtin_constructor('sha224') 139 sha256 = __get_builtin_constructor('sha256') 140 sha384 = __get_builtin_constructor('sha384') 141 sha512 = __get_builtin_constructor('sha512') 144 # Cleanup locals() 145 del __always_supported, __func_name, __get_hash 146 del __py_new, __hash_new, __get_openssl_constructor
Note:
See TracChangeset
for help on using the changeset viewer.