diff options
author | Paul Kehrer <paul.l.kehrer@gmail.com> | 2013-10-28 17:34:27 -0500 |
---|---|---|
committer | Paul Kehrer <paul.l.kehrer@gmail.com> | 2013-10-28 17:34:27 -0500 |
commit | 0317b04b119ceb55e11cf1be28c5223bad240c26 (patch) | |
tree | 7ddf3ce8f7e7f51cdca419e915e60a853d678fed /tests/hazmat/primitives/utils.py | |
parent | 0a0d749e38d849c6d4b1767b6a3994408f0977f1 (diff) | |
download | cryptography-0317b04b119ceb55e11cf1be28c5223bad240c26.tar.gz cryptography-0317b04b119ceb55e11cf1be28c5223bad240c26.tar.bz2 cryptography-0317b04b119ceb55e11cf1be28c5223bad240c26.zip |
HMAC support
Conflicts:
docs/primitives/index.rst
tests/hazmat/primitives/utils.py
Diffstat (limited to 'tests/hazmat/primitives/utils.py')
-rw-r--r-- | tests/hazmat/primitives/utils.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/hazmat/primitives/utils.py b/tests/hazmat/primitives/utils.py index fabdca01..73a2469a 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 hmac from cryptography.hazmat.primitives.block import BlockCipher @@ -125,3 +126,57 @@ def long_string_hash_test(backend, hash_factory, md, only_if, skip_message): m = hash_factory(backend=backend) m.update(b"a" * 1000000) assert m.hexdigest() == md.lower() + + +def generate_hmac_test(param_loader, path, file_names, hash_cls, + only_if=None, skip_message=None): + def test_hmac(self): + for backend in _ALL_BACKENDS: + for file_name in file_names: + for params in param_loader(os.path.join(path, file_name)): + yield ( + hmac_test, + backend, + hash_cls, + params, + only_if, + skip_message + ) + return test_hmac + + +def hmac_test(backend, hash_cls, 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), hash_cls) + h.update(binascii.unhexlify(msg)) + assert h.hexdigest() == md + digest = hmac.HMAC(binascii.unhexlify(key), hash_cls, + data=binascii.unhexlify(msg)).hexdigest() + assert digest == md + + +def generate_base_hmac_test(hash_cls, only_if=None, skip_message=None): + def test_base_hmac(self): + for backend in _ALL_BACKENDS: + yield ( + base_hmac_test, + backend, + hash_cls, + only_if, + skip_message, + ) + return test_base_hmac + + +def base_hmac_test(backend, hash_cls, 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), hash_cls) + h_copy = h.copy() + assert h != h_copy + assert h._ctx != h_copy._ctx |