|
26 | 26 | # Were we compiled --with-pydebug or with #define Py_DEBUG?
|
27 | 27 | COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount')
|
28 | 28 |
|
29 |
| -c_hashlib = import_fresh_module('hashlib', fresh=['_hashlib']) |
30 |
| -py_hashlib = import_fresh_module('hashlib', blocked=['_hashlib']) |
31 |
| - |
| 29 | +# default builtin hash module |
| 30 | +default_builtin_hashes = {'md5', 'sha1', 'sha256', 'sha512', 'sha3', 'blake2'} |
| 31 | +# --with-builtin-hashlib-hashes override |
32 | 32 | builtin_hashes = sysconfig.get_config_var("PY_BUILTIN_HASHLIB_HASHES")
|
33 | 33 | if builtin_hashes is None:
|
34 |
| - builtin_hashes = {'md5', 'sha1', 'sha256', 'sha512', 'sha3', 'blake2'} |
| 34 | + builtin_hashes = default_builtin_hashes |
35 | 35 | else:
|
36 | 36 | builtin_hashes = {
|
37 | 37 | m.strip() for m in builtin_hashes.strip('"').lower().split(",")
|
38 | 38 | }
|
39 | 39 |
|
| 40 | +# hashlib with and without OpenSSL backend for PBKDF2 |
| 41 | +# only import builtin_hashlib when all builtin hashes are available. |
| 42 | +# Otherwise import prints noise on stderr |
| 43 | +openssl_hashlib = import_fresh_module('hashlib', fresh=['_hashlib']) |
| 44 | +if builtin_hashes == default_builtin_hashes: |
| 45 | + builtin_hashlib = import_fresh_module('hashlib', blocked=['_hashlib']) |
| 46 | +else: |
| 47 | + builtin_hashlib = None |
| 48 | + |
40 | 49 | try:
|
41 | 50 | from _hashlib import HASH, HASHXOF, openssl_md_meth_names
|
42 | 51 | except ImportError:
|
@@ -1032,16 +1041,16 @@ def _test_pbkdf2_hmac(self, pbkdf2, supported):
|
1032 | 1041 | iterations=1, dklen=None)
|
1033 | 1042 | self.assertEqual(out, self.pbkdf2_results['sha1'][0][0])
|
1034 | 1043 |
|
| 1044 | + @unittest.skipIf(builtin_hashlib is None, "test requires builtin_hashlib") |
1035 | 1045 | def test_pbkdf2_hmac_py(self):
|
1036 |
| - self._test_pbkdf2_hmac(py_hashlib.pbkdf2_hmac, builtin_hashes) |
| 1046 | + self._test_pbkdf2_hmac(builtin_hashlib.pbkdf2_hmac, builtin_hashes) |
1037 | 1047 |
|
1038 |
| - @unittest.skipUnless(hasattr(c_hashlib, 'pbkdf2_hmac'), |
| 1048 | + @unittest.skipUnless(hasattr(openssl_hashlib, 'pbkdf2_hmac'), |
1039 | 1049 | ' test requires OpenSSL > 1.0')
|
1040 | 1050 | def test_pbkdf2_hmac_c(self):
|
1041 |
| - self._test_pbkdf2_hmac(c_hashlib.pbkdf2_hmac, openssl_md_meth_names) |
1042 |
| - |
| 1051 | + self._test_pbkdf2_hmac(openssl_hashlib.pbkdf2_hmac, openssl_md_meth_names) |
1043 | 1052 |
|
1044 |
| - @unittest.skipUnless(hasattr(c_hashlib, 'scrypt'), |
| 1053 | + @unittest.skipUnless(hasattr(hashlib, 'scrypt'), |
1045 | 1054 | ' test requires OpenSSL > 1.1')
|
1046 | 1055 | def test_scrypt(self):
|
1047 | 1056 | for password, salt, n, r, p, expected in self.scrypt_test_vectors:
|
|
0 commit comments