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