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

Delta Between Two Patch Sets: Lib/hashlib.py

Issue 26798: add BLAKE2 to hashlib
Left Patch Set: Created 3 years, 3 months ago
Right Patch Set: Created 2 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:
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'', **kwargs) - 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(), sha512(), blake2b(), and blake2s()
15
16 On platforms with support for 64bit integer types (some 32bit platforms,
17 all 64bit platforms), blake2b and blake2s are supported.
18 15
19 More algorithms may be available on your platform but the above are guaranteed 16 More algorithms may be available on your platform but the above are guaranteed
20 to exist. See the algorithms_guaranteed and algorithms_available attributes 17 to exist. See the algorithms_guaranteed and algorithms_available attributes
21 to find out what algorithm names can be passed to new(). 18 to find out what algorithm names can be passed to new().
22 19
23 NOTE: If you want the adler32 or crc32 hash functions they are available in 20 NOTE: If you want the adler32 or crc32 hash functions they are available in
24 the zlib module. 21 the zlib module.
25 22
26 Choose your hash function wisely. Some have known collision weaknesses. 23 Choose your hash function wisely. Some have known collision weaknesses.
27 sha384 and sha512 will be slow on 32 bit platforms. 24 sha384 and sha512 will be slow on 32 bit platforms.
(...skipping 22 matching lines...) Expand all
50 47
51 More condensed: 48 More condensed:
52 49
53 >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest() 50 >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
54 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2' 51 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
55 52
56 """ 53 """
57 54
58 # 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
59 # always available algorithm is added. 56 # always available algorithm is added.
60 __always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512') 57 __always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512',
58 'blake2b', 'blake2s')
61 59
62 algorithms_guaranteed = set(__always_supported) 60 algorithms_guaranteed = set(__always_supported)
63 algorithms_available = set(__always_supported) 61 algorithms_available = set(__always_supported)
64 62
65 __all__ = __always_supported + ('new', 'algorithms_guaranteed', 63 __all__ = __always_supported + ('new', 'algorithms_guaranteed',
66 'algorithms_available', 'pbkdf2_hmac') 64 'algorithms_available', 'pbkdf2_hmac')
67 65
68 66
69 __builtin_constructor_cache = {} 67 __builtin_constructor_cache = {}
70 68
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 """new(name, data=b'', **kwargs) - Return a new hashing object using the 116 """new(name, data=b'', **kwargs) - Return a new hashing object using the
119 named algorithm; optionally initialized with data (which must be bytes). 117 named algorithm; optionally initialized with data (which must be bytes).
120 """ 118 """
121 return __get_builtin_constructor(name)(data, **kwargs) 119 return __get_builtin_constructor(name)(data, **kwargs)
122 120
123 121
124 def __hash_new(name, data=b'', **kwargs): 122 def __hash_new(name, data=b'', **kwargs):
125 """new(name, data=b'') - Return a new hashing object using the named algorit hm; 123 """new(name, data=b'') - Return a new hashing object using the named algorit hm;
126 optionally initialized with data (which must be bytes). 124 optionally initialized with data (which must be bytes).
127 """ 125 """
128 if kwargs: 126 if name in {'blake2b', 'blake2s'}:
129 # For now only the builtin constructor takes keyword args 127 # Prefer our blake2 implementation.
130 # XXX name='blake2b' w/o kwargs may pick OpenSSL 1.1.0's BLAKE2 128 # OpenSSL 1.1.0 comes with a limited implementation of blake2b/s.
131 # implementation. AFAIK it's slower and doesn't use SSE. 129 # It does neither support keyed blake2 nor advanced features like
130 # salt, personal, tree hashing or SSE.
132 return __get_builtin_constructor(name)(data, **kwargs) 131 return __get_builtin_constructor(name)(data, **kwargs)
133 try: 132 try:
134 return _hashlib.new(name, data) 133 return _hashlib.new(name, data)
135 except ValueError: 134 except ValueError:
136 # If the _hashlib module (OpenSSL) doesn't support the named 135 # If the _hashlib module (OpenSSL) doesn't support the named
137 # hash, try using our builtin implementations. 136 # hash, try using our builtin implementations.
138 # This allows for SHA224/256 and SHA384/512 support even though 137 # This allows for SHA224/256 and SHA384/512 support even though
139 # the OpenSSL library prior to 0.9.8 doesn't provide them. 138 # the OpenSSL library prior to 0.9.8 doesn't provide them.
140 return __get_builtin_constructor(name)(data) 139 return __get_builtin_constructor(name)(data)
141 140
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 216
218 for __func_name in __always_supported: 217 for __func_name in __always_supported:
219 # try them all, some may not work due to the OpenSSL 218 # try them all, some may not work due to the OpenSSL
220 # version not supporting that algorithm. 219 # version not supporting that algorithm.
221 try: 220 try:
222 globals()[__func_name] = __get_hash(__func_name) 221 globals()[__func_name] = __get_hash(__func_name)
223 except ValueError: 222 except ValueError:
224 import logging 223 import logging
225 logging.exception('code for hash %s was not found.', __func_name) 224 logging.exception('code for hash %s was not found.', __func_name)
226 225
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)
236 226
237 # Cleanup locals() 227 # Cleanup locals()
238 del __always_supported, __func_name, __get_hash 228 del __always_supported, __func_name, __get_hash
239 del __py_new, __hash_new, __get_openssl_constructor 229 del __py_new, __hash_new, __get_openssl_constructor
LEFTRIGHT

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