aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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.py18
3 files changed, 26 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..a7af8a8d 100644
--- a/tests/hazmat/primitives/test_hashes.py
+++ b/tests/hazmat/primitives/test_hashes.py
@@ -20,7 +20,10 @@ 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 +42,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 +178,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)