aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2014-03-16 08:03:21 -0430
committerPaul Kehrer <paul.l.kehrer@gmail.com>2014-03-16 08:03:21 -0430
commite2083365826c231f0a11efb7555280b4a1579a9e (patch)
tree8efa000377e25dc9b99fb77dcc03b7ba663395ae
parent3fb221f1fb02ffed7a558bd06ba41bb75c329fc5 (diff)
parentb018dbc6a3a5cb0c4697f8623ddb021cebc27330 (diff)
downloadcryptography-e2083365826c231f0a11efb7555280b4a1579a9e.tar.gz
cryptography-e2083365826c231f0a11efb7555280b4a1579a9e.tar.bz2
cryptography-e2083365826c231f0a11efb7555280b4a1579a9e.zip
Merge pull request #804 from Ayrx/add-backend-check-to-hashes
Added backend check to hash primitives
-rw-r--r--cryptography/hazmat/primitives/hashes.py7
-rw-r--r--docs/hazmat/primitives/cryptographic-hashes.rst4
-rw-r--r--tests/hazmat/primitives/test_hashes.py19
3 files changed, 27 insertions, 3 deletions
diff --git a/cryptography/hazmat/primitives/hashes.py b/cryptography/hazmat/primitives/hashes.py
index bee188b3..409f564e 100644
--- a/cryptography/hazmat/primitives/hashes.py
+++ b/cryptography/hazmat/primitives/hashes.py
@@ -16,13 +16,18 @@ from __future__ import absolute_import, division, print_function
import six
from cryptography import utils
-from cryptography.exceptions import AlreadyFinalized
+from cryptography.exceptions import AlreadyFinalized, UnsupportedInterface
+from cryptography.hazmat.backends.interfaces import HashBackend
from cryptography.hazmat.primitives import interfaces
@utils.register_interface(interfaces.HashContext)
class Hash(object):
def __init__(self, algorithm, backend, ctx=None):
+ if not isinstance(backend, HashBackend):
+ raise UnsupportedInterface(
+ "Backend object does not implement HashBackend")
+
if not isinstance(algorithm, interfaces.HashAlgorithm):
raise TypeError("Expected instance of interfaces.HashAlgorithm.")
self.algorithm = algorithm
diff --git a/docs/hazmat/primitives/cryptographic-hashes.rst b/docs/hazmat/primitives/cryptographic-hashes.rst
index 627ca7bd..b7eee2f5 100644
--- a/docs/hazmat/primitives/cryptographic-hashes.rst
+++ b/docs/hazmat/primitives/cryptographic-hashes.rst
@@ -45,6 +45,10 @@ Message Digests
:class:`~cryptography.hazmat.backends.interfaces.HashBackend`
provider.
+ :raises cryptography.exceptions.UnsupportedInterface: This is raised if the
+ provided ``backend`` does not implement
+ :class:`~cryptography.hazmat.backends.interfaces.HashBackend`
+
.. method:: update(data)
:param bytes data: The bytes to be hashed.
diff --git a/tests/hazmat/primitives/test_hashes.py b/tests/hazmat/primitives/test_hashes.py
index fc53d635..d74d1d82 100644
--- a/tests/hazmat/primitives/test_hashes.py
+++ b/tests/hazmat/primitives/test_hashes.py
@@ -20,7 +20,11 @@ import pytest
import six
from cryptography import utils
-from cryptography.exceptions import AlreadyFinalized, UnsupportedHash
+from cryptography.exceptions import (
+ AlreadyFinalized, UnsupportedHash, UnsupportedInterface
+)
+
+from cryptography.hazmat.backends.interfaces import HashBackend
from cryptography.hazmat.primitives import hashes, interfaces
from .utils import generate_base_hash_test
@@ -39,7 +43,11 @@ class TestHashContext(object):
m.update(six.u("\u00FC"))
def test_copy_backend_object(self):
- pretend_backend = pretend.stub()
+ @utils.register_interface(HashBackend)
+ class PretendBackend(object):
+ pass
+
+ pretend_backend = PretendBackend()
copied_ctx = pretend.stub()
pretend_ctx = pretend.stub(copy=lambda: copied_ctx)
h = hashes.Hash(hashes.SHA1(), backend=pretend_backend,
@@ -171,3 +179,10 @@ class TestMD5(object):
digest_size=16,
block_size=64,
)
+
+
+def test_invalid_backend():
+ pretend_backend = object()
+
+ with pytest.raises(UnsupportedInterface):
+ hashes.Hash(hashes.SHA1(), pretend_backend)