diff options
author | Paul Kehrer <paul.l.kehrer@gmail.com> | 2013-10-28 11:06:55 -0500 |
---|---|---|
committer | Paul Kehrer <paul.l.kehrer@gmail.com> | 2013-10-28 17:36:50 -0500 |
commit | 2824ab72d30e8423d17496e2c3baa47106505c8c (patch) | |
tree | 49f1e87ceb6d3f8a387231cf1a81cb444ee94ee6 /cryptography/hazmat/primitives/hmac.py | |
parent | 1bb8b710d444012b7218a08f098a85c4a31ca1bc (diff) | |
download | cryptography-2824ab72d30e8423d17496e2c3baa47106505c8c.tar.gz cryptography-2824ab72d30e8423d17496e2c3baa47106505c8c.tar.bz2 cryptography-2824ab72d30e8423d17496e2c3baa47106505c8c.zip |
make hmac (mostly) compatible with stdlib hmac
Diffstat (limited to 'cryptography/hazmat/primitives/hmac.py')
-rw-r--r-- | cryptography/hazmat/primitives/hmac.py | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/cryptography/hazmat/primitives/hmac.py b/cryptography/hazmat/primitives/hmac.py index f635e36e..c417cd2e 100644 --- a/cryptography/hazmat/primitives/hmac.py +++ b/cryptography/hazmat/primitives/hmac.py @@ -19,34 +19,38 @@ import six class HMAC(object): - def __init__(self, key, hash_cls, data=None, ctx=None, backend=None): + def __init__(self, key, msg=None, digestmod=None, ctx=None, backend=None): super(HMAC, self).__init__() if backend is None: from cryptography.hazmat.bindings import _default_backend backend = _default_backend + + if digestmod is None: + raise ValueError("digestmod is a required argument") + self._backend = backend - self.hash_cls = hash_cls + self.digestmod = digestmod self.key = key if ctx is None: - self._ctx = self._backend.hmacs.create_ctx(key, self.hash_cls) + self._ctx = self._backend.hmacs.create_ctx(key, self.digestmod) else: self._ctx = ctx - if data is not None: - self.update(data) + if msg is not None: + self.update(msg) - def update(self, data): - if isinstance(data, six.text_type): + 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, data) + self._backend.hmacs.update_ctx(self._ctx, msg) def copy(self): - return self.__class__(self.key, hash_cls=self.hash_cls, + 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.hash_cls.digest_size) + self.digestmod.digest_size) def hexdigest(self): return str(binascii.hexlify(self.digest()).decode("ascii")) |