Block OpenSSL implementation of _hashlib.new().
(blocked_name)
| 846 | |
| 847 | |
| 848 | def _block_openssl_hash_new(blocked_name): |
| 849 | """Block OpenSSL implementation of _hashlib.new().""" |
| 850 | assert isinstance(blocked_name, str), blocked_name |
| 851 | |
| 852 | # re-import '_hashlib' in case it was mocked |
| 853 | if (_hashlib := _import_module("_hashlib")) is None: |
| 854 | return contextlib.nullcontext() |
| 855 | |
| 856 | @functools.wraps(wrapped := _hashlib.new) |
| 857 | def _hashlib_new(name, data=b'', *, usedforsecurity=True, string=None): |
| 858 | if name == blocked_name: |
| 859 | raise _hashlib.UnsupportedDigestmodError(blocked_name) |
| 860 | return wrapped(name, data, |
| 861 | usedforsecurity=usedforsecurity, string=string) |
| 862 | |
| 863 | _ensure_wrapper_signature(_hashlib_new, wrapped) |
| 864 | return unittest.mock.patch('_hashlib.new', _hashlib_new) |
| 865 | |
| 866 | |
| 867 | def _block_openssl_hmac_new(blocked_name): |
no test coverage detected
searching dependent graphs…