aboutsummaryrefslogtreecommitdiffstats
path: root/cryptography/hazmat/primitives/hmac.py
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2013-11-01 16:36:30 -0700
committerAlex Gaynor <alex.gaynor@gmail.com>2013-11-01 16:36:30 -0700
commit33675c3c1d08507ca73b01428ae99b5200af830e (patch)
treeecbb23b094d378cc6d71df8a34722e1ad1bcfe9b /cryptography/hazmat/primitives/hmac.py
parent51a56c26cbd500fdc795450413cbaef660ccb624 (diff)
parent753ae19feaf33e4328a707d5588797c2a800c0c7 (diff)
downloadcryptography-33675c3c1d08507ca73b01428ae99b5200af830e.tar.gz
cryptography-33675c3c1d08507ca73b01428ae99b5200af830e.tar.bz2
cryptography-33675c3c1d08507ca73b01428ae99b5200af830e.zip
Merge pull request #212 from dreid/primitive-hmac
Strip down the HMAC interface to match the Hash interface.
Diffstat (limited to 'cryptography/hazmat/primitives/hmac.py')
-rw-r--r--cryptography/hazmat/primitives/hmac.py38
1 files changed, 15 insertions, 23 deletions
diff --git a/cryptography/hazmat/primitives/hmac.py b/cryptography/hazmat/primitives/hmac.py
index 4da0cc3f..1457ed78 100644
--- a/cryptography/hazmat/primitives/hmac.py
+++ b/cryptography/hazmat/primitives/hmac.py
@@ -13,47 +13,39 @@
from __future__ import absolute_import, division, print_function
-import binascii
-
import six
+from cryptography.hazmat.primitives import interfaces
+
+@interfaces.register(interfaces.HashContext)
class HMAC(object):
- def __init__(self, key, msg=None, digestmod=None, ctx=None, backend=None):
+ def __init__(self, key, algorithm, ctx=None, backend=None):
super(HMAC, self).__init__()
+ if not isinstance(algorithm, interfaces.HashAlgorithm):
+ raise TypeError("Expected instance of interfaces.HashAlgorithm.")
+ self.algorithm = algorithm
+
if backend is None:
from cryptography.hazmat.bindings import _default_backend
backend = _default_backend
- if digestmod is None:
- raise TypeError("digestmod is a required argument")
-
self._backend = backend
- self.digestmod = digestmod
- self.key = key
+ self._key = key
if ctx is None:
- self._ctx = self._backend.hmacs.create_ctx(key, self.digestmod)
+ self._ctx = self._backend.hmacs.create_ctx(key, self.algorithm)
else:
self._ctx = ctx
- if msg is not None:
- self.update(msg)
-
def update(self, msg):
if isinstance(msg, six.text_type):
raise TypeError("Unicode-objects must be encoded before hashing")
self._backend.hmacs.update_ctx(self._ctx, msg)
def copy(self):
- return self.__class__(self.key, digestmod=self.digestmod,
- backend=self._backend, ctx=self._copy_ctx())
-
- def digest(self):
- return self._backend.hmacs.finalize_ctx(self._copy_ctx(),
- self.digestmod.digest_size)
-
- def hexdigest(self):
- return str(binascii.hexlify(self.digest()).decode("ascii"))
+ return self.__class__(self._key, self.algorithm, backend=self._backend,
+ ctx=self._backend.hmacs.copy_ctx(self._ctx))
- def _copy_ctx(self):
- return self._backend.hmacs.copy_ctx(self._ctx)
+ def finalize(self):
+ return self._backend.hmacs.finalize_ctx(self._ctx,
+ self.algorithm.digest_size)