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

Unified Diff: Lib/hashlib.py

Issue 26798: add BLAKE2 to hashlib
Patch Set: Created 3 years, 5 months ago
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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Doc/library/hashlib.rst ('k') | Lib/test/test_hashlib.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
index 316cecedc209d861db8a46768594b08804348cf7..4ea3d1907772f8836865a1c240abb450c9bbc82b 100644
--- a/Lib/hashlib.py
+++ b/Lib/hashlib.py
@@ -4,15 +4,18 @@
__doc__ = """hashlib module - A common interface to many hash functions.
-new(name, data=b'') - returns a new hash object implementing the
- given hash function; initializing the hash
- using the given binary data.
+new(name, data=b'', **kwargs) - returns a new hash object implementing the
+ given hash function; initializing the hash
+ using the given binary data.
Named constructor functions are also available, these are faster
than using new(name):
md5(), sha1(), sha224(), sha256(), sha384(), and sha512()
+On platforms with support for 64bit integer types (some 32bit platforms,
+all 64bit platforms), blake2b and blake2s are supported.
+
More algorithms may be available on your platform but the above are guaranteed
to exist. See the algorithms_guaranteed and algorithms_available attributes
to find out what algorithm names can be passed to new().
@@ -85,6 +88,10 @@ def __get_builtin_constructor(name):
import _sha512
cache['SHA384'] = cache['sha384'] = _sha512.sha384
cache['SHA512'] = cache['sha512'] = _sha512.sha512
+ elif name in ('blake2b', 'blake2s'):
+ import _blake2
+ cache['blake2b'] = _blake2.blake2b
+ cache['blake2s'] = _blake2.blake2s
except ImportError:
pass # no extension module, this hash is unsupported.
@@ -107,17 +114,22 @@ def __get_openssl_constructor(name):
return __get_builtin_constructor(name)
-def __py_new(name, data=b''):
- """new(name, data=b'') - Return a new hashing object using the named algorithm;
- optionally initialized with data (which must be bytes).
+def __py_new(name, data=b'', **kwargs):
+ """new(name, data=b'', **kwargs) - Return a new hashing object using the
+ named algorithm; optionally initialized with data (which must be bytes).
"""
- return __get_builtin_constructor(name)(data)
+ return __get_builtin_constructor(name)(data, **kwargs)
-def __hash_new(name, data=b''):
+def __hash_new(name, data=b'', **kwargs):
"""new(name, data=b'') - Return a new hashing object using the named algorithm;
optionally initialized with data (which must be bytes).
"""
+ if kwargs:
+ # For now only the builtin constructor takes keyword args
+ # XXX name='blake2b' w/o kwargs may pick OpenSSL 1.1.0's BLAKE2
+ # implementation. AFAIK it's slower and doesn't use SSE.
+ return __get_builtin_constructor(name)(data, **kwargs)
try:
return _hashlib.new(name, data)
except ValueError:
@@ -212,6 +224,16 @@ for __func_name in __always_supported:
import logging
logging.exception('code for hash %s was not found.', __func_name)
+for __func_name in ('blake2b', 'blake2s'):
+ # BLAKE2 is a builtin hash algorithm but it is not available on some
+ # platforms because it requires a 64bit int type.
+ try:
+ globals()[__func_name] = __get_hash(__func_name)
+ except ValueError:
+ pass
+ else:
+ algorithms_available.add(__func_name)
+
# Cleanup locals()
del __always_supported, __func_name, __get_hash
del __py_new, __hash_new, __get_openssl_constructor
« 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+