• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. hazmat::
2
3Hash-based message authentication codes (HMAC)
4==============================================
5
6.. currentmodule:: cryptography.hazmat.primitives.hmac
7
8.. testsetup::
9
10    import binascii
11    key = binascii.unhexlify(b"0" * 32)
12
13Hash-based message authentication codes (or HMACs) are a tool for calculating
14message authentication codes using a cryptographic hash function coupled with a
15secret key. You can use an HMAC to verify both the integrity and authenticity
16of a message.
17
18.. class:: HMAC(key, algorithm, backend=None)
19
20    HMAC objects take a ``key`` and a
21    :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm` instance.
22    The ``key`` should be :doc:`randomly generated bytes </random-numbers>` and
23    is recommended to be equal in length to the ``digest_size`` of the hash
24    function chosen. You must keep the ``key`` secret.
25
26    This is an implementation of :rfc:`2104`.
27
28    .. doctest::
29
30        >>> from cryptography.hazmat.primitives import hashes, hmac
31        >>> h = hmac.HMAC(key, hashes.SHA256())
32        >>> h.update(b"message to hash")
33        >>> h.finalize()
34        b'#F\xdaI\x8b"e\xc4\xf1\xbb\x9a\x8fc\xff\xf5\xdex.\xbc\xcd/+\x8a\x86\x1d\x84\'\xc3\xa6\x1d\xd8J'
35
36    If the backend doesn't support the requested ``algorithm`` an
37    :class:`~cryptography.exceptions.UnsupportedAlgorithm` exception will be
38    raised.
39
40    If ``algorithm`` isn't a
41    :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm` instance
42    then ``TypeError`` will be raised.
43
44    To check that a given signature is correct use the :meth:`verify` method.
45    You will receive an exception if the signature is wrong:
46
47    .. doctest::
48
49        >>> h = hmac.HMAC(key, hashes.SHA256())
50        >>> h.update(b"message to hash")
51        >>> h.verify(b"an incorrect signature")
52        Traceback (most recent call last):
53        ...
54        cryptography.exceptions.InvalidSignature: Signature did not match digest.
55
56    :param key: Secret key as ``bytes``.
57    :type key: :term:`bytes-like`
58    :param algorithm: An
59        :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`
60        instance such as those described in
61        :ref:`Cryptographic Hashes <cryptographic-hash-algorithms>`.
62    :param backend: An optional
63        :class:`~cryptography.hazmat.backends.interfaces.HMACBackend`
64        instance.
65
66    :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the
67        provided ``backend`` does not implement
68        :class:`~cryptography.hazmat.backends.interfaces.HMACBackend`
69
70    .. method:: update(msg)
71
72        :param msg: The bytes to hash and authenticate.
73        :type msg: :term:`bytes-like`
74        :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`
75        :raises TypeError: This exception is raised if ``msg`` is not ``bytes``.
76
77    .. method:: copy()
78
79        Copy this :class:`HMAC` instance, usually so that we may call
80        :meth:`finalize` to get an intermediate digest value while we continue
81        to call :meth:`update` on the original instance.
82
83        :return: A new instance of :class:`HMAC` that can be updated
84            and finalized independently of the original instance.
85        :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`
86
87    .. method:: verify(signature)
88
89        Finalize the current context and securely compare digest to
90        ``signature``.
91
92        :param bytes signature: The bytes to compare the current digest
93                                against.
94        :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`
95        :raises cryptography.exceptions.InvalidSignature: If signature does not
96                                                          match digest
97        :raises TypeError: This exception is raised if ``signature`` is not
98                           ``bytes``.
99
100    .. method:: finalize()
101
102        Finalize the current context and return the message digest as bytes.
103
104        After ``finalize`` has been called this object can no longer be used
105        and :meth:`update`, :meth:`copy`, :meth:`verify` and :meth:`finalize`
106        will raise an :class:`~cryptography.exceptions.AlreadyFinalized`
107        exception.
108
109        :return bytes: The message digest as bytes.
110        :raises cryptography.exceptions.AlreadyFinalized:
111