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