(name)
| 80 | } |
| 81 | |
| 82 | def __get_builtin_constructor(name): |
| 83 | if not isinstance(name, str): |
| 84 | # Since this function is only used by new(), we use the same |
| 85 | # exception as _hashlib.new() when 'name' is of incorrect type. |
| 86 | err = f"new() argument 'name' must be str, not {type(name).__name__}" |
| 87 | raise TypeError(err) |
| 88 | cache = __builtin_constructor_cache |
| 89 | constructor = cache.get(name) |
| 90 | if constructor is not None: |
| 91 | return constructor |
| 92 | try: |
| 93 | if name in {'SHA1', 'sha1'}: |
| 94 | import _sha1 |
| 95 | cache['SHA1'] = cache['sha1'] = _sha1.sha1 |
| 96 | elif name in {'MD5', 'md5'}: |
| 97 | import _md5 |
| 98 | cache['MD5'] = cache['md5'] = _md5.md5 |
| 99 | elif name in {'SHA256', 'sha256', 'SHA224', 'sha224'}: |
| 100 | import _sha2 |
| 101 | cache['SHA224'] = cache['sha224'] = _sha2.sha224 |
| 102 | cache['SHA256'] = cache['sha256'] = _sha2.sha256 |
| 103 | elif name in {'SHA512', 'sha512', 'SHA384', 'sha384'}: |
| 104 | import _sha2 |
| 105 | cache['SHA384'] = cache['sha384'] = _sha2.sha384 |
| 106 | cache['SHA512'] = cache['sha512'] = _sha2.sha512 |
| 107 | elif name in {'blake2b', 'blake2s'}: |
| 108 | import _blake2 |
| 109 | cache['blake2b'] = _blake2.blake2b |
| 110 | cache['blake2s'] = _blake2.blake2s |
| 111 | elif name in {'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512'}: |
| 112 | import _sha3 |
| 113 | cache['sha3_224'] = _sha3.sha3_224 |
| 114 | cache['sha3_256'] = _sha3.sha3_256 |
| 115 | cache['sha3_384'] = _sha3.sha3_384 |
| 116 | cache['sha3_512'] = _sha3.sha3_512 |
| 117 | elif name in {'shake_128', 'shake_256'}: |
| 118 | import _sha3 |
| 119 | cache['shake_128'] = _sha3.shake_128 |
| 120 | cache['shake_256'] = _sha3.shake_256 |
| 121 | except ImportError: |
| 122 | pass # no extension module, this hash is unsupported. |
| 123 | |
| 124 | constructor = cache.get(name) |
| 125 | if constructor is not None: |
| 126 | return constructor |
| 127 | |
| 128 | # Keep the message in sync with hashlib.h::HASHLIB_UNSUPPORTED_ALGORITHM. |
| 129 | raise ValueError(f'unsupported hash algorithm {name}') |
| 130 | |
| 131 | |
| 132 | def __get_openssl_constructor(name): |
no test coverage detected
searching dependent graphs…