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

Side by Side Diff: Lib/hashlib.py

Issue 26798: add BLAKE2 to hashlib
Patch Set: Created 3 years, 2 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
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.
Martin Panter 2016/09/04 15:58:52 Is this out of date regarding 32-bit platforms?
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 21 matching lines...) Expand all
47 50
48 More condensed: 51 More condensed:
49 52
50 >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest() 53 >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
51 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2' 54 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
52 55
53 """ 56 """
54 57
55 # This tuple and __get_builtin_constructor() must be modified if a new 58 # This tuple and __get_builtin_constructor() must be modified if a new
56 # always available algorithm is added. 59 # always available algorithm is added.
57 __always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512') 60 __always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512',
61 'blake2b', 'blake2s')
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 ('blake2b', 'blake2s'):
93 import _blake2
94 cache['blake2b'] = _blake2.blake2b
95 cache['blake2s'] = _blake2.blake2s
88 except ImportError: 96 except ImportError:
89 pass # no extension module, this hash is unsupported. 97 pass # no extension module, this hash is unsupported.
90 98
91 constructor = cache.get(name) 99 constructor = cache.get(name)
92 if constructor is not None: 100 if constructor is not None:
93 return constructor 101 return constructor
94 102
95 raise ValueError('unsupported hash type ' + name) 103 raise ValueError('unsupported hash type ' + name)
96 104
97 105
98 def __get_openssl_constructor(name): 106 def __get_openssl_constructor(name):
99 try: 107 try:
100 f = getattr(_hashlib, 'openssl_' + name) 108 f = getattr(_hashlib, 'openssl_' + name)
101 # Allow the C module to raise ValueError. The function will be 109 # Allow the C module to raise ValueError. The function will be
102 # defined but the hash not actually available thanks to OpenSSL. 110 # defined but the hash not actually available thanks to OpenSSL.
103 f() 111 f()
104 # Use the C function directly (very fast) 112 # Use the C function directly (very fast)
105 return f 113 return f
106 except (AttributeError, ValueError): 114 except (AttributeError, ValueError):
107 return __get_builtin_constructor(name) 115 return __get_builtin_constructor(name)
108 116
109 117
110 def __py_new(name, data=b''): 118 def __py_new(name, data=b'', **kwargs):
119 """new(name, data=b'', **kwargs) - Return a new hashing object using the
120 named algorithm; optionally initialized with data (which must be bytes).
121 """
122 return __get_builtin_constructor(name)(data, **kwargs)
123
124
125 def __hash_new(name, data=b'', **kwargs):
111 """new(name, data=b'') - Return a new hashing object using the named algorit hm; 126 """new(name, data=b'') - Return a new hashing object using the named algorit hm;
112 optionally initialized with data (which must be bytes). 127 optionally initialized with data (which must be bytes).
113 """ 128 """
114 return __get_builtin_constructor(name)(data) 129 if name in {'blake2b', 'blake2s'}:
115 130 # Prefer our blake2 implementation.
116 131 # OpenSSL 1.1.0 comes with a limited implementation of blake2b/s.
117 def __hash_new(name, data=b''): 132 # 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; 133 # salt, personal, tree hashing or SSE.
119 optionally initialized with data (which must be bytes). 134 return __get_builtin_constructor(name)(data, **kwargs)
120 """
121 try: 135 try:
122 return _hashlib.new(name, data) 136 return _hashlib.new(name, data)
123 except ValueError: 137 except ValueError:
124 # If the _hashlib module (OpenSSL) doesn't support the named 138 # If the _hashlib module (OpenSSL) doesn't support the named
125 # hash, try using our builtin implementations. 139 # hash, try using our builtin implementations.
126 # This allows for SHA224/256 and SHA384/512 support even though 140 # This allows for SHA224/256 and SHA384/512 support even though
127 # the OpenSSL library prior to 0.9.8 doesn't provide them. 141 # the OpenSSL library prior to 0.9.8 doesn't provide them.
128 return __get_builtin_constructor(name)(data) 142 return __get_builtin_constructor(name)(data)
129 143
130 144
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 218
205 219
206 for __func_name in __always_supported: 220 for __func_name in __always_supported:
207 # try them all, some may not work due to the OpenSSL 221 # try them all, some may not work due to the OpenSSL
208 # version not supporting that algorithm. 222 # version not supporting that algorithm.
209 try: 223 try:
210 globals()[__func_name] = __get_hash(__func_name) 224 globals()[__func_name] = __get_hash(__func_name)
211 except ValueError: 225 except ValueError:
212 import logging 226 import logging
213 logging.exception('code for hash %s was not found.', __func_name) 227 logging.exception('code for hash %s was not found.', __func_name)
228
214 229
215 # Cleanup locals() 230 # Cleanup locals()
216 del __always_supported, __func_name, __get_hash 231 del __always_supported, __func_name, __get_hash
217 del __py_new, __hash_new, __get_openssl_constructor 232 del __py_new, __hash_new, __get_openssl_constructor
OLDNEW
« Doc/library/hashlib.rst ('K') | « 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+