aboutsummaryrefslogtreecommitdiffstats
path: root/cryptography/hazmat/primitives/hmac.py
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2013-12-27 08:21:54 -0800
committerAlex Gaynor <alex.gaynor@gmail.com>2013-12-27 08:21:54 -0800
commit37c88a0dea800b3028f95bf71a8cd6e344254d4e (patch)
treebcf227588c0a96073336041978880763ce03bc7e /cryptography/hazmat/primitives/hmac.py
parentb645521e84026633f666aa107816ac2fc5e05cc6 (diff)
parentb808f8cc91e302d4120eefa80c946a7cdcf9a155 (diff)
downloadcryptography-37c88a0dea800b3028f95bf71a8cd6e344254d4e.tar.gz
cryptography-37c88a0dea800b3028f95bf71a8cd6e344254d4e.tar.bz2
cryptography-37c88a0dea800b3028f95bf71a8cd6e344254d4e.zip
Merge pull request #315 from juliankrause/verify
Add verify function to hmac and hashes.
Diffstat (limited to 'cryptography/hazmat/primitives/hmac.py')
-rw-r--r--cryptography/hazmat/primitives/hmac.py11
1 files changed, 9 insertions, 2 deletions
diff --git a/cryptography/hazmat/primitives/hmac.py b/cryptography/hazmat/primitives/hmac.py
index 618bccc5..76d658aa 100644
--- a/cryptography/hazmat/primitives/hmac.py
+++ b/cryptography/hazmat/primitives/hmac.py
@@ -16,8 +16,8 @@ from __future__ import absolute_import, division, print_function
import six
from cryptography import utils
-from cryptography.exceptions import AlreadyFinalized
-from cryptography.hazmat.primitives import interfaces
+from cryptography.exceptions import AlreadyFinalized, InvalidSignature
+from cryptography.hazmat.primitives import constant_time, interfaces
@utils.register_interface(interfaces.HashContext)
@@ -57,3 +57,10 @@ class HMAC(object):
digest = self._ctx.finalize()
self._ctx = None
return digest
+
+ def verify(self, signature):
+ if isinstance(signature, six.text_type):
+ raise TypeError("Unicode-objects must be encoded before verifying")
+ digest = self.finalize()
+ if not constant_time.bytes_eq(digest, signature):
+ raise InvalidSignature("Signature did not match digest.")