aboutsummaryrefslogtreecommitdiffstats
path: root/cryptography/hazmat/primitives/hmac.py
diff options
context:
space:
mode:
Diffstat (limited to 'cryptography/hazmat/primitives/hmac.py')
-rw-r--r--cryptography/hazmat/primitives/hmac.py34
1 files changed, 13 insertions, 21 deletions
diff --git a/cryptography/hazmat/primitives/hmac.py b/cryptography/hazmat/primitives/hmac.py
index 4da0cc3f..02770863 100644
--- a/cryptography/hazmat/primitives/hmac.py
+++ b/cryptography/hazmat/primitives/hmac.py
@@ -17,43 +17,35 @@ 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__()
+ 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)