summaryrefslogtreecommitdiffstats
path: root/Lib/hashlib.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/hashlib.py')
-rw-r--r--Lib/hashlib.py41
1 files changed, 20 insertions, 21 deletions
diff --git a/Lib/hashlib.py b/Lib/hashlib.py
index 56a9360..77673f0 100644
--- a/Lib/hashlib.py
+++ b/Lib/hashlib.py
@@ -64,43 +64,42 @@ __all__ = __always_supported + ('new', 'algorithms_guaranteed',
'algorithms_available', 'pbkdf2_hmac')
+__builtin_constructor_cache = {}
+
def __get_builtin_constructor(name):
+ cache = __builtin_constructor_cache
+ constructor = cache.get(name)
+ if constructor is not None:
+ return constructor
try:
if name in ('SHA1', 'sha1'):
import _sha1
- return _sha1.sha1
+ cache['SHA1'] = cache['sha1'] = _sha1.sha1
elif name in ('MD5', 'md5'):
import _md5
- return _md5.md5
+ cache['MD5'] = cache['md5'] = _md5.md5
elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):
import _sha256
- bs = name[3:]
- if bs == '256':
- return _sha256.sha256
- elif bs == '224':
- return _sha256.sha224
+ cache['SHA224'] = cache['sha224'] = _sha256.sha224
+ cache['SHA256'] = cache['sha256'] = _sha256.sha256
elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):
import _sha512
- bs = name[3:]
- if bs == '512':
- return _sha512.sha512
- elif bs == '384':
- return _sha512.sha384
+ cache['SHA384'] = cache['sha384'] = _sha512.sha384
+ cache['SHA512'] = cache['sha512'] = _sha512.sha512
elif name in {'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
'SHA3_224', 'SHA3_256', 'SHA3_384', 'SHA3_512'}:
import _sha3
- bs = name[5:]
- if bs == '224':
- return _sha3.sha3_224
- elif bs == '256':
- return _sha3.sha3_256
- elif bs == '384':
- return _sha3.sha3_384
- elif bs == '512':
- return _sha3.sha3_512
+ cache['SHA3_224'] = cache['sha3_224'] = _sha3.sha3_224
+ cache['SHA3_256'] = cache['sha3_256'] = _sha3.sha3_256
+ cache['SHA3_384'] = cache['sha3_384'] = _sha3.sha3_384
+ cache['SHA3_512'] = cache['sha3_512'] = _sha3.sha3_512
except ImportError:
pass # no extension module, this hash is unsupported.
+ constructor = cache.get(name)
+ if constructor is not None:
+ return constructor
+
raise ValueError('unsupported hash type ' + name)