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

Side by Side Diff: Lib/hashlib.py

Issue 26798: add BLAKE2 to hashlib
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:
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(), sha512(), blake2b(), and blake2s()
15 15
16 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
17 to exist. See the algorithms_guaranteed and algorithms_available attributes 17 to exist. See the algorithms_guaranteed and algorithms_available attributes
18 to find out what algorithm names can be passed to new(). 18 to find out what algorithm names can be passed to new().
19 19
20 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
21 the zlib module. 21 the zlib module.
22 22
23 Choose your hash function wisely. Some have known collision weaknesses. 23 Choose your hash function wisely. Some have known collision weaknesses.
24 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
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 'blake2b', 'blake2s')
58 59
59 algorithms_guaranteed = set(__always_supported) 60 algorithms_guaranteed = set(__always_supported)
60 algorithms_available = set(__always_supported) 61 algorithms_available = set(__always_supported)
61 62
62 __all__ = __always_supported + ('new', 'algorithms_guaranteed', 63 __all__ = __always_supported + ('new', 'algorithms_guaranteed',
63 'algorithms_available', 'pbkdf2_hmac') 64 'algorithms_available', 'pbkdf2_hmac')
64 65
65 66
66 __builtin_constructor_cache = {} 67 __builtin_constructor_cache = {}
67 68
(...skipping 10 matching lines...) Expand all
78 import _md5 79 import _md5
79 cache['MD5'] = cache['md5'] = _md5.md5 80 cache['MD5'] = cache['md5'] = _md5.md5
80 elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'): 81 elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):
81 import _sha256 82 import _sha256
82 cache['SHA224'] = cache['sha224'] = _sha256.sha224 83 cache['SHA224'] = cache['sha224'] = _sha256.sha224
83 cache['SHA256'] = cache['sha256'] = _sha256.sha256 84 cache['SHA256'] = cache['sha256'] = _sha256.sha256
84 elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'): 85 elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):
85 import _sha512 86 import _sha512
86 cache['SHA384'] = cache['sha384'] = _sha512.sha384 87 cache['SHA384'] = cache['sha384'] = _sha512.sha384
87 cache['SHA512'] = cache['sha512'] = _sha512.sha512 88 cache['SHA512'] = cache['sha512'] = _sha512.sha512
89 elif name in ('blake2b', 'blake2s'):
90 import _blake2
91 cache['blake2b'] = _blake2.blake2b
92 cache['blake2s'] = _blake2.blake2s
88 except ImportError: 93 except ImportError:
89 pass # no extension module, this hash is unsupported. 94 pass # no extension module, this hash is unsupported.
90 95
91 constructor = cache.get(name) 96 constructor = cache.get(name)
92 if constructor is not None: 97 if constructor is not None:
93 return constructor 98 return constructor
94 99
95 raise ValueError('unsupported hash type ' + name) 100 raise ValueError('unsupported hash type ' + name)
96 101
97 102
98 def __get_openssl_constructor(name): 103 def __get_openssl_constructor(name):
99 try: 104 try:
100 f = getattr(_hashlib, 'openssl_' + name) 105 f = getattr(_hashlib, 'openssl_' + name)
101 # Allow the C module to raise ValueError. The function will be 106 # Allow the C module to raise ValueError. The function will be
102 # defined but the hash not actually available thanks to OpenSSL. 107 # defined but the hash not actually available thanks to OpenSSL.
103 f() 108 f()
104 # Use the C function directly (very fast) 109 # Use the C function directly (very fast)
105 return f 110 return f
106 except (AttributeError, ValueError): 111 except (AttributeError, ValueError):
107 return __get_builtin_constructor(name) 112 return __get_builtin_constructor(name)
108 113
109 114
110 def __py_new(name, data=b''): 115 def __py_new(name, data=b'', **kwargs):
116 """new(name, data=b'', **kwargs) - Return a new hashing object using the
117 named algorithm; optionally initialized with data (which must be bytes).
118 """
119 return __get_builtin_constructor(name)(data, **kwargs)
120
121
122 def __hash_new(name, data=b'', **kwargs):
111 """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;
112 optionally initialized with data (which must be bytes). 124 optionally initialized with data (which must be bytes).
113 """ 125 """
114 return __get_builtin_constructor(name)(data) 126 if name in {'blake2b', 'blake2s'}:
115 127 # Prefer our blake2 implementation.
116 128 # OpenSSL 1.1.0 comes with a limited implementation of blake2b/s.
117 def __hash_new(name, data=b''): 129 # It does neither support keyed blake2 nor advanced features like
118 """new(name, data=b'') - Return a new hashing object using the named algorit hm; 130 # salt, personal, tree hashing or SSE.
119 optionally initialized with data (which must be bytes). 131 return __get_builtin_constructor(name)(data, **kwargs)
120 """
121 try: 132 try:
122 return _hashlib.new(name, data) 133 return _hashlib.new(name, data)
123 except ValueError: 134 except ValueError:
124 # If the _hashlib module (OpenSSL) doesn't support the named 135 # If the _hashlib module (OpenSSL) doesn't support the named
125 # hash, try using our builtin implementations. 136 # hash, try using our builtin implementations.
126 # This allows for SHA224/256 and SHA384/512 support even though 137 # This allows for SHA224/256 and SHA384/512 support even though
127 # 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.
128 return __get_builtin_constructor(name)(data) 139 return __get_builtin_constructor(name)(data)
129 140
130 141
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 215
205 216
206 for __func_name in __always_supported: 217 for __func_name in __always_supported:
207 # try them all, some may not work due to the OpenSSL 218 # try them all, some may not work due to the OpenSSL
208 # version not supporting that algorithm. 219 # version not supporting that algorithm.
209 try: 220 try:
210 globals()[__func_name] = __get_hash(__func_name) 221 globals()[__func_name] = __get_hash(__func_name)
211 except ValueError: 222 except ValueError:
212 import logging 223 import logging
213 logging.exception('code for hash %s was not found.', __func_name) 224 logging.exception('code for hash %s was not found.', __func_name)
225
214 226
215 # Cleanup locals() 227 # Cleanup locals()
216 del __always_supported, __func_name, __get_hash 228 del __always_supported, __func_name, __get_hash
217 del __py_new, __hash_new, __get_openssl_constructor 229 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+