Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4)

Side by Side Diff: Lib/hashlib.py

Issue 16113: Add SHA-3 (Keccak) support
Patch Set: Created 3 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Doc/library/hashlib.rst ('k') | Lib/test/test_hashlib.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #. Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org) 1 #. Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
2 # Licensed to PSF under a Contributor Agreement. 2 # Licensed to PSF under a Contributor Agreement.
3 # 3 #
4 4
5 __doc__ = """hashlib module - A common interface to many hash functions. 5 __doc__ = """hashlib module - A common interface to many hash functions.
6 6
7 new(name, data=b'') - returns a new hash object implementing the 7 new(name, data=b'') - returns a new hash object implementing the
8 given hash function; initializing the hash 8 given hash function; initializing the hash
9 using the given binary data. 9 using the given binary data.
10 10
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 More condensed: 48 More condensed:
49 49
50 >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest() 50 >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
51 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2' 51 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
52 52
53 """ 53 """
54 54
55 # This tuple and __get_builtin_constructor() must be modified if a new 55 # This tuple and __get_builtin_constructor() must be modified if a new
56 # always available algorithm is added. 56 # always available algorithm is added.
57 __always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512') 57 __always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')
58
59 # Supported on most platforms
60 __often_supported = ('sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
61 'shake_128', 'shake_256')
58 62
59 algorithms_guaranteed = set(__always_supported) 63 algorithms_guaranteed = set(__always_supported)
60 algorithms_available = set(__always_supported) 64 algorithms_available = set(__always_supported)
61 65
62 __all__ = __always_supported + ('new', 'algorithms_guaranteed', 66 __all__ = __always_supported + ('new', 'algorithms_guaranteed',
63 'algorithms_available', 'pbkdf2_hmac') 67 'algorithms_available', 'pbkdf2_hmac')
64 68
65 69
66 __builtin_constructor_cache = {} 70 __builtin_constructor_cache = {}
67 71
(...skipping 10 matching lines...) Expand all
78 import _md5 82 import _md5
79 cache['MD5'] = cache['md5'] = _md5.md5 83 cache['MD5'] = cache['md5'] = _md5.md5
80 elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'): 84 elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):
81 import _sha256 85 import _sha256
82 cache['SHA224'] = cache['sha224'] = _sha256.sha224 86 cache['SHA224'] = cache['sha224'] = _sha256.sha224
83 cache['SHA256'] = cache['sha256'] = _sha256.sha256 87 cache['SHA256'] = cache['sha256'] = _sha256.sha256
84 elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'): 88 elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):
85 import _sha512 89 import _sha512
86 cache['SHA384'] = cache['sha384'] = _sha512.sha384 90 cache['SHA384'] = cache['sha384'] = _sha512.sha384
87 cache['SHA512'] = cache['sha512'] = _sha512.sha512 91 cache['SHA512'] = cache['sha512'] = _sha512.sha512
92 elif name in {'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
93 'shake_128', 'shake_256'}:
94 import _sha3
95 cache['sha3_224'] = _sha3.sha3_224
96 cache['sha3_256'] = _sha3.sha3_256
97 cache['sha3_384'] = _sha3.sha3_384
98 cache['sha3_512'] = _sha3.sha3_512
99 cache['shake_128'] = _sha3.shake_128
100 cache['shake_256'] = _sha3.shake_256
88 except ImportError: 101 except ImportError:
89 pass # no extension module, this hash is unsupported. 102 pass # no extension module, this hash is unsupported.
90 103
91 constructor = cache.get(name) 104 constructor = cache.get(name)
92 if constructor is not None: 105 if constructor is not None:
93 return constructor 106 return constructor
94 107
95 raise ValueError('unsupported hash type ' + name) 108 raise ValueError('unsupported hash type ' + name)
96 109
97 110
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 218
206 for __func_name in __always_supported: 219 for __func_name in __always_supported:
207 # try them all, some may not work due to the OpenSSL 220 # try them all, some may not work due to the OpenSSL
208 # version not supporting that algorithm. 221 # version not supporting that algorithm.
209 try: 222 try:
210 globals()[__func_name] = __get_hash(__func_name) 223 globals()[__func_name] = __get_hash(__func_name)
211 except ValueError: 224 except ValueError:
212 import logging 225 import logging
213 logging.exception('code for hash %s was not found.', __func_name) 226 logging.exception('code for hash %s was not found.', __func_name)
214 227
228 for __func_name in __often_supported:
229 try:
230 globals()[__func_name] = __get_hash(__func_name)
231 except ValueError:
232 pass
233 else:
234 algorithms_available.add(__func_name)
235
236
215 # Cleanup locals() 237 # Cleanup locals()
216 del __always_supported, __func_name, __get_hash 238 del __always_supported, __often_supported, __func_name, __get_hash
217 del __py_new, __hash_new, __get_openssl_constructor 239 del __py_new, __hash_new, __get_openssl_constructor
OLDNEW
« no previous file with comments | « Doc/library/hashlib.rst ('k') | Lib/test/test_hashlib.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+