aboutsummaryrefslogtreecommitdiffstats
path: root/tests/hazmat/primitives/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/hazmat/primitives/utils.py')
-rw-r--r--tests/hazmat/primitives/utils.py46
1 files changed, 22 insertions, 24 deletions
diff --git a/tests/hazmat/primitives/utils.py b/tests/hazmat/primitives/utils.py
index c51fef52..b4a8a3e6 100644
--- a/tests/hazmat/primitives/utils.py
+++ b/tests/hazmat/primitives/utils.py
@@ -4,6 +4,7 @@ import os
import pytest
from cryptography.hazmat.bindings import _ALL_BACKENDS
+from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import hmac
from cryptography.hazmat.primitives.block import BlockCipher
@@ -65,26 +66,25 @@ def generate_hash_test(param_loader, path, file_names, hash_cls,
return test_hash
-def hash_test(backend, hash_cls, params, only_if, skip_message):
+def hash_test(backend, algorithm, params, only_if, skip_message):
if only_if is not None and not only_if(backend):
pytest.skip(skip_message)
msg = params[0]
md = params[1]
- m = hash_cls(backend=backend)
+ m = hashes.Hash(algorithm, backend=backend)
m.update(binascii.unhexlify(msg))
- assert m.hexdigest() == md.replace(" ", "").lower()
- digst = hash_cls(backend=backend, data=binascii.unhexlify(msg)).hexdigest()
- assert digst == md.replace(" ", "").lower()
+ expected_md = md.replace(" ", "").lower().encode("ascii")
+ assert m.finalize() == binascii.unhexlify(expected_md)
-def generate_base_hash_test(hash_cls, digest_size, block_size,
+def generate_base_hash_test(algorithm, digest_size, block_size,
only_if=None, skip_message=None):
def test_base_hash(self):
for backend in _ALL_BACKENDS:
yield (
base_hash_test,
backend,
- hash_cls,
+ algorithm,
digest_size,
block_size,
only_if,
@@ -93,13 +93,14 @@ def generate_base_hash_test(hash_cls, digest_size, block_size,
return test_base_hash
-def base_hash_test(backend, digestmod, digest_size, block_size, only_if,
+def base_hash_test(backend, algorithm, digest_size, block_size, only_if,
skip_message):
if only_if is not None and not only_if(backend):
pytest.skip(skip_message)
- m = digestmod(backend=backend)
- assert m.digest_size == digest_size
- assert m.block_size == block_size
+
+ m = hashes.Hash(algorithm, backend=backend)
+ assert m.algorithm.digest_size == digest_size
+ assert m.algorithm.block_size == block_size
m_copy = m.copy()
assert m != m_copy
assert m._ctx != m_copy._ctx
@@ -120,15 +121,15 @@ def generate_long_string_hash_test(hash_factory, md, only_if=None,
return test_long_string_hash
-def long_string_hash_test(backend, hash_factory, md, only_if, skip_message):
+def long_string_hash_test(backend, algorithm, md, only_if, skip_message):
if only_if is not None and not only_if(backend):
pytest.skip(skip_message)
- m = hash_factory(backend=backend)
+ m = hashes.Hash(algorithm, backend=backend)
m.update(b"a" * 1000000)
- assert m.hexdigest() == md.lower()
+ assert m.finalize() == binascii.unhexlify(md.lower().encode("ascii"))
-def generate_hmac_test(param_loader, path, file_names, digestmod,
+def generate_hmac_test(param_loader, path, file_names, algorithm,
only_if=None, skip_message=None):
def test_hmac(self):
for backend in _ALL_BACKENDS:
@@ -137,7 +138,7 @@ def generate_hmac_test(param_loader, path, file_names, digestmod,
yield (
hmac_test,
backend,
- digestmod,
+ algorithm,
params,
only_if,
skip_message
@@ -145,18 +146,15 @@ def generate_hmac_test(param_loader, path, file_names, digestmod,
return test_hmac
-def hmac_test(backend, digestmod, params, only_if, skip_message):
+def hmac_test(backend, algorithm, params, only_if, skip_message):
if only_if is not None and not only_if(backend):
pytest.skip(skip_message)
msg = params[0]
md = params[1]
key = params[2]
- h = hmac.HMAC(binascii.unhexlify(key), digestmod=digestmod)
+ h = hmac.HMAC(binascii.unhexlify(key), algorithm)
h.update(binascii.unhexlify(msg))
- assert h.hexdigest() == md
- digest = hmac.HMAC(binascii.unhexlify(key), digestmod=digestmod,
- msg=binascii.unhexlify(msg)).hexdigest()
- assert digest == md
+ assert h.finalize() == binascii.unhexlify(md.encode("ascii"))
def generate_base_hmac_test(hash_cls, only_if=None, skip_message=None):
@@ -172,11 +170,11 @@ def generate_base_hmac_test(hash_cls, only_if=None, skip_message=None):
return test_base_hmac
-def base_hmac_test(backend, digestmod, only_if, skip_message):
+def base_hmac_test(backend, algorithm, only_if, skip_message):
if only_if is not None and not only_if(backend):
pytest.skip(skip_message)
key = b"ab"
- h = hmac.HMAC(binascii.unhexlify(key), digestmod=digestmod)
+ h = hmac.HMAC(binascii.unhexlify(key), algorithm)
h_copy = h.copy()
assert h != h_copy
assert h._ctx != h_copy._ctx