From d4cb34d51ae6695a6cda8e3b46eeb0b45a5935f1 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 19 Oct 2013 23:05:12 -0500 Subject: Allow data to be passed in the constructor & reject unicode ala hashlib --- cryptography/primitives/hashes.py | 6 +++++- tests/primitives/test_hashes.py | 11 +++++++++++ tests/primitives/utils.py | 4 +++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/cryptography/primitives/hashes.py b/cryptography/primitives/hashes.py index e8c1f929..c4bd8ad0 100644 --- a/cryptography/primitives/hashes.py +++ b/cryptography/primitives/hashes.py @@ -23,13 +23,17 @@ from cryptography.bindings import _default_api class BaseHash(six.with_metaclass(abc.ABCMeta)): - def __init__(self, api=None, ctx=None): + def __init__(self, data=None, api=None, ctx=None): if api is None: api = _default_api self._api = api self._ctx = self._api.create_hash_context(self) if ctx is None else ctx + if data is not None: + self.update(data) def update(self, data): + if isinstance(data, six.text_type): + raise TypeError("Unicode-objects must be encoded before hashing") self._api.update_hash_context(self._ctx, data) def copy(self): diff --git a/tests/primitives/test_hashes.py b/tests/primitives/test_hashes.py index 901ddabb..805d992b 100644 --- a/tests/primitives/test_hashes.py +++ b/tests/primitives/test_hashes.py @@ -13,11 +13,22 @@ from __future__ import absolute_import, division, print_function +import pytest + +import six + from cryptography.primitives import hashes from .utils import generate_base_hash_test +class TestBaseHash(object): + def test_base_hash_reject_unicode(self, api): + m = hashes.SHA1(api=api) + with pytest.raises(TypeError): + m.update(six.u("\u00FC")) + + class TestSHA1(object): test_SHA1 = generate_base_hash_test( hashes.SHA1, diff --git a/tests/primitives/utils.py b/tests/primitives/utils.py index a3759b03..a15e773c 100644 --- a/tests/primitives/utils.py +++ b/tests/primitives/utils.py @@ -67,6 +67,8 @@ def hash_test(api, hash_cls, params, only_if, skip_message): m = hash_cls(api=api) m.update(binascii.unhexlify(msg)) assert m.hexdigest() == md.replace(" ", "").lower() + digest = hash_cls(api=api, data=binascii.unhexlify(msg)).hexdigest() + assert digest == md.replace(" ", "").lower() def generate_base_hash_test(hash_cls, digest_size, block_size, @@ -115,6 +117,6 @@ def generate_long_string_hash_test(hash_factory, md, only_if=None, def long_string_hash_test(api, hash_factory, md, only_if, skip_message): if only_if is not None and not only_if(api): pytest.skip(skip_message) - m = hash_factory(api) + m = hash_factory(api=api) m.update(b"a" * 1000000) assert m.hexdigest() == md.lower() -- cgit v1.2.3 From b58865b2dd2bddb2ee727adbe238df15dbd3e085 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Mon, 21 Oct 2013 20:21:37 -0700 Subject: Lint setup.py --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index e72eb58b..a514f3cd 100644 --- a/tox.ini +++ b/tox.ini @@ -20,4 +20,4 @@ commands = [testenv:pep8] deps = flake8 # E128 continuation line under-indented for visual indent -commands = flake8 --ignore="E128" cryptography/ tests/ docs/ +commands = flake8 --ignore="E128" cryptography/ tests/ docs/ setup.py -- cgit v1.2.3 From 967c03b3093e2a301a695d80b2e13ead443fcc75 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Mon, 21 Oct 2013 20:22:50 -0700 Subject: Better way --- tox.ini | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index a514f3cd..184a7e3c 100644 --- a/tox.ini +++ b/tox.ini @@ -20,4 +20,7 @@ commands = [testenv:pep8] deps = flake8 # E128 continuation line under-indented for visual indent -commands = flake8 --ignore="E128" cryptography/ tests/ docs/ setup.py +commands = flake8 --ignore="E128" . + +[flake8] +exclude = .tox -- cgit v1.2.3 From 1d8053897def11ae0ec2d946c918e58458271f39 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Mon, 21 Oct 2013 20:44:52 -0700 Subject: Also ignore eggs --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 184a7e3c..187bff84 100644 --- a/tox.ini +++ b/tox.ini @@ -23,4 +23,4 @@ deps = flake8 commands = flake8 --ignore="E128" . [flake8] -exclude = .tox +exclude = .tox,*.egg -- cgit v1.2.3 From afdddcac0a991a30293de6e65d2ac398d3a56b47 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Mon, 21 Oct 2013 21:00:20 -0700 Subject: Enable all the flake8 --- tests/primitives/test_utils.py | 5 +++-- tests/test_utils.py | 10 ++++++---- tox.ini | 3 +-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/primitives/test_utils.py b/tests/primitives/test_utils.py index 9888309e..6e197923 100644 --- a/tests/primitives/test_utils.py +++ b/tests/primitives/test_utils.py @@ -1,7 +1,8 @@ import pytest -from .utils import (base_hash_test, encrypt_test, hash_test, - long_string_hash_test) +from .utils import ( + base_hash_test, encrypt_test, hash_test, long_string_hash_test +) class TestEncryptTest(object): diff --git a/tests/test_utils.py b/tests/test_utils.py index 3fe9e570..f96cf004 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -15,10 +15,12 @@ import textwrap import pytest -from .utils import (load_nist_vectors, load_nist_vectors_from_file, - load_cryptrec_vectors, load_cryptrec_vectors_from_file, - load_openssl_vectors, load_openssl_vectors_from_file, load_hash_vectors, - load_hash_vectors_from_file) +from .utils import ( + load_nist_vectors, load_nist_vectors_from_file, load_cryptrec_vectors, + load_cryptrec_vectors_from_file, load_openssl_vectors, + load_openssl_vectors_from_file, load_hash_vectors, + load_hash_vectors_from_file +) def test_load_nist_vectors_encrypt(): diff --git a/tox.ini b/tox.ini index 187bff84..b437a7a9 100644 --- a/tox.ini +++ b/tox.ini @@ -19,8 +19,7 @@ commands = [testenv:pep8] deps = flake8 -# E128 continuation line under-indented for visual indent -commands = flake8 --ignore="E128" . +commands = flake8 . [flake8] exclude = .tox,*.egg -- cgit v1.2.3