diff options
author | David Reid <dreid@dreid.org> | 2013-11-01 14:52:16 -0700 |
---|---|---|
committer | David Reid <dreid@dreid.org> | 2013-11-01 14:52:16 -0700 |
commit | e3960f62df56bd8931f758026738812bce3c45f2 (patch) | |
tree | cb8f1cf7c3477d9ff58996fa435ae8e479b592f9 /cryptography | |
parent | ce46b89eed2d3a35043910c62f711690cb69c393 (diff) | |
download | cryptography-e3960f62df56bd8931f758026738812bce3c45f2.tar.gz cryptography-e3960f62df56bd8931f758026738812bce3c45f2.tar.bz2 cryptography-e3960f62df56bd8931f758026738812bce3c45f2.zip |
Strip down the HMAC interface to be HashContext.
Diffstat (limited to 'cryptography')
-rw-r--r-- | cryptography/hazmat/primitives/hmac.py | 34 |
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) |