aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2013-10-19 23:05:12 -0500
committerPaul Kehrer <paul.l.kehrer@gmail.com>2013-10-19 23:17:00 -0500
commitd4cb34d51ae6695a6cda8e3b46eeb0b45a5935f1 (patch)
treed26ed4e241a32915aa8addccebaf9efbca3b71f7
parent746815b8f2b6a485b41e37c67969ed21338946db (diff)
downloadcryptography-d4cb34d51ae6695a6cda8e3b46eeb0b45a5935f1.tar.gz
cryptography-d4cb34d51ae6695a6cda8e3b46eeb0b45a5935f1.tar.bz2
cryptography-d4cb34d51ae6695a6cda8e3b46eeb0b45a5935f1.zip
Allow data to be passed in the constructor & reject unicode ala hashlib
-rw-r--r--cryptography/primitives/hashes.py6
-rw-r--r--tests/primitives/test_hashes.py11
-rw-r--r--tests/primitives/utils.py4
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()