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

Delta Between Two Patch Sets: Lib/hashlib.py

Issue 16113: Add SHA-3 (Keccak) support
Left Patch Set: Created 5 years, 9 months ago
Right Patch Set: Created 3 years, 1 month 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Doc/library/hashlib.rst ('k') | Lib/test/test_hashlib.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 47
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 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
59 'shake_128', 'shake_256')
60
58 61
59 algorithms_guaranteed = set(__always_supported) 62 algorithms_guaranteed = set(__always_supported)
60 algorithms_available = set(__always_supported) 63 algorithms_available = set(__always_supported)
61 64
62 __all__ = __always_supported + ('new', 'algorithms_guaranteed', 65 __all__ = __always_supported + ('new', 'algorithms_guaranteed',
63 'algorithms_available', 'pbkdf2_hmac') 66 'algorithms_available', 'pbkdf2_hmac')
64 67
65 68
66 __builtin_constructor_cache = {} 69 __builtin_constructor_cache = {}
67 70
(...skipping 10 matching lines...) Expand all
78 import _md5 81 import _md5
79 cache['MD5'] = cache['md5'] = _md5.md5 82 cache['MD5'] = cache['md5'] = _md5.md5
80 elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'): 83 elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):
81 import _sha256 84 import _sha256
82 cache['SHA224'] = cache['sha224'] = _sha256.sha224 85 cache['SHA224'] = cache['sha224'] = _sha256.sha224
83 cache['SHA256'] = cache['sha256'] = _sha256.sha256 86 cache['SHA256'] = cache['sha256'] = _sha256.sha256
84 elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'): 87 elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):
85 import _sha512 88 import _sha512
86 cache['SHA384'] = cache['sha384'] = _sha512.sha384 89 cache['SHA384'] = cache['sha384'] = _sha512.sha384
87 cache['SHA512'] = cache['sha512'] = _sha512.sha512 90 cache['SHA512'] = cache['sha512'] = _sha512.sha512
91 elif name in {'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
92 'shake_128', 'shake_256'}:
93 import _sha3
94 cache['sha3_224'] = _sha3.sha3_224
95 cache['sha3_256'] = _sha3.sha3_256
96 cache['sha3_384'] = _sha3.sha3_384
97 cache['sha3_512'] = _sha3.sha3_512
98 cache['shake_128'] = _sha3.shake_128
99 cache['shake_256'] = _sha3.shake_256
88 except ImportError: 100 except ImportError:
89 pass # no extension module, this hash is unsupported. 101 pass # no extension module, this hash is unsupported.
90 102
91 constructor = cache.get(name) 103 constructor = cache.get(name)
92 if constructor is not None: 104 if constructor is not None:
93 return constructor 105 return constructor
94 106
95 raise ValueError('unsupported hash type ' + name) 107 raise ValueError('unsupported hash type ' + name)
96 108
97 109
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 outer = new(hash_name) 177 outer = new(hash_name)
166 blocksize = getattr(inner, 'block_size', 64) 178 blocksize = getattr(inner, 'block_size', 64)
167 if len(password) > blocksize: 179 if len(password) > blocksize:
168 password = new(hash_name, password).digest() 180 password = new(hash_name, password).digest()
169 password = password + b'\x00' * (blocksize - len(password)) 181 password = password + b'\x00' * (blocksize - len(password))
170 inner.update(password.translate(_trans_36)) 182 inner.update(password.translate(_trans_36))
171 outer.update(password.translate(_trans_5C)) 183 outer.update(password.translate(_trans_5C))
172 184
173 def prf(msg, inner=inner, outer=outer): 185 def prf(msg, inner=inner, outer=outer):
174 # PBKDF2_HMAC uses the password as key. We can re-use the same 186 # PBKDF2_HMAC uses the password as key. We can re-use the same
175 # digest objects and and just update copies to skip initialization. 187 # digest objects and just update copies to skip initialization.
176 icpy = inner.copy() 188 icpy = inner.copy()
177 ocpy = outer.copy() 189 ocpy = outer.copy()
178 icpy.update(msg) 190 icpy.update(msg)
179 ocpy.update(icpy.digest()) 191 ocpy.update(icpy.digest())
180 return ocpy.digest() 192 return ocpy.digest()
181 193
182 if iterations < 1: 194 if iterations < 1:
183 raise ValueError(iterations) 195 raise ValueError(iterations)
184 if dklen is None: 196 if dklen is None:
185 dklen = outer.digest_size 197 dklen = outer.digest_size
(...skipping 19 matching lines...) Expand all
205 217
206 for __func_name in __always_supported: 218 for __func_name in __always_supported:
207 # try them all, some may not work due to the OpenSSL 219 # try them all, some may not work due to the OpenSSL
208 # version not supporting that algorithm. 220 # version not supporting that algorithm.
209 try: 221 try:
210 globals()[__func_name] = __get_hash(__func_name) 222 globals()[__func_name] = __get_hash(__func_name)
211 except ValueError: 223 except ValueError:
212 import logging 224 import logging
213 logging.exception('code for hash %s was not found.', __func_name) 225 logging.exception('code for hash %s was not found.', __func_name)
214 226
227
215 # Cleanup locals() 228 # Cleanup locals()
216 del __always_supported, __func_name, __get_hash 229 del __always_supported, __func_name, __get_hash
217 del __py_new, __hash_new, __get_openssl_constructor 230 del __py_new, __hash_new, __get_openssl_constructor
LEFTRIGHT

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