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

Side by Side Diff: Lib/hashlib.py

Issue 26798: add BLAKE2 to hashlib
Patch Set: Created 3 years, 4 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'', **kwargs) - 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
11 Named constructor functions are also available, these are faster 11 Named constructor functions are also available, these are faster
12 than using new(name): 12 than using new(name):
13 13
14 md5(), sha1(), sha224(), sha256(), sha384(), and sha512() 14 md5(), sha1(), sha224(), sha256(), sha384(), and sha512()
15 15
16 On platforms with support for 64bit integer types (some 32bit platforms,
17 all 64bit platforms), blake2b and blake2s are supported.
18
16 More algorithms may be available on your platform but the above are guaranteed 19 More algorithms may be available on your platform but the above are guaranteed
17 to exist. See the algorithms_guaranteed and algorithms_available attributes 20 to exist. See the algorithms_guaranteed and algorithms_available attributes
18 to find out what algorithm names can be passed to new(). 21 to find out what algorithm names can be passed to new().
19 22
20 NOTE: If you want the adler32 or crc32 hash functions they are available in 23 NOTE: If you want the adler32 or crc32 hash functions they are available in
21 the zlib module. 24 the zlib module.
22 25
23 Choose your hash function wisely. Some have known collision weaknesses. 26 Choose your hash function wisely. Some have known collision weaknesses.
24 sha384 and sha512 will be slow on 32 bit platforms. 27 sha384 and sha512 will be slow on 32 bit platforms.
25 28
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 ('blake2b', 'blake2s'):
92 import _blake2
93 cache['blake2b'] = _blake2.blake2b
94 cache['blake2s'] = _blake2.blake2s
88 except ImportError: 95 except ImportError:
89 pass # no extension module, this hash is unsupported. 96 pass # no extension module, this hash is unsupported.
90 97
91 constructor = cache.get(name) 98 constructor = cache.get(name)
92 if constructor is not None: 99 if constructor is not None:
93 return constructor 100 return constructor
94 101
95 raise ValueError('unsupported hash type ' + name) 102 raise ValueError('unsupported hash type ' + name)
96 103
97 104
98 def __get_openssl_constructor(name): 105 def __get_openssl_constructor(name):
99 try: 106 try:
100 f = getattr(_hashlib, 'openssl_' + name) 107 f = getattr(_hashlib, 'openssl_' + name)
101 # Allow the C module to raise ValueError. The function will be 108 # Allow the C module to raise ValueError. The function will be
102 # defined but the hash not actually available thanks to OpenSSL. 109 # defined but the hash not actually available thanks to OpenSSL.
103 f() 110 f()
104 # Use the C function directly (very fast) 111 # Use the C function directly (very fast)
105 return f 112 return f
106 except (AttributeError, ValueError): 113 except (AttributeError, ValueError):
107 return __get_builtin_constructor(name) 114 return __get_builtin_constructor(name)
108 115
109 116
110 def __py_new(name, data=b''): 117 def __py_new(name, data=b'', **kwargs):
118 """new(name, data=b'', **kwargs) - Return a new hashing object using the
119 named algorithm; optionally initialized with data (which must be bytes).
120 """
121 return __get_builtin_constructor(name)(data, **kwargs)
122
123
124 def __hash_new(name, data=b'', **kwargs):
111 """new(name, data=b'') - Return a new hashing object using the named algorit hm; 125 """new(name, data=b'') - Return a new hashing object using the named algorit hm;
112 optionally initialized with data (which must be bytes). 126 optionally initialized with data (which must be bytes).
113 """ 127 """
114 return __get_builtin_constructor(name)(data) 128 if kwargs:
115 129 # For now only the builtin constructor takes keyword args
116 130 # XXX name='blake2b' w/o kwargs may pick OpenSSL 1.1.0's BLAKE2
117 def __hash_new(name, data=b''): 131 # implementation. AFAIK it's slower and doesn't use SSE.
118 """new(name, data=b'') - Return a new hashing object using the named algorit hm; 132 return __get_builtin_constructor(name)(data, **kwargs)
119 optionally initialized with data (which must be bytes).
120 """
121 try: 133 try:
122 return _hashlib.new(name, data) 134 return _hashlib.new(name, data)
123 except ValueError: 135 except ValueError:
124 # If the _hashlib module (OpenSSL) doesn't support the named 136 # If the _hashlib module (OpenSSL) doesn't support the named
125 # hash, try using our builtin implementations. 137 # hash, try using our builtin implementations.
126 # This allows for SHA224/256 and SHA384/512 support even though 138 # This allows for SHA224/256 and SHA384/512 support even though
127 # the OpenSSL library prior to 0.9.8 doesn't provide them. 139 # the OpenSSL library prior to 0.9.8 doesn't provide them.
128 return __get_builtin_constructor(name)(data) 140 return __get_builtin_constructor(name)(data)
129 141
130 142
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 216
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)
226
227 for __func_name in ('blake2b', 'blake2s'):
228 # BLAKE2 is a builtin hash algorithm but it is not available on some
229 # platforms because it requires a 64bit int type.
230 try:
231 globals()[__func_name] = __get_hash(__func_name)
232 except ValueError:
233 pass
234 else:
235 algorithms_available.add(__func_name)
214 236
215 # Cleanup locals() 237 # Cleanup locals()
216 del __always_supported, __func_name, __get_hash 238 del __always_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+