| 1 |
:mod:`!hmac` --- Keyed-Hashing for Message Authentication |
| 2 |
========================================================= |
| 3 |
|
| 4 |
.. module:: hmac |
| 5 |
:synopsis: Keyed-Hashing for Message Authentication (HMAC) implementation |
| 6 |
|
| 7 |
.. moduleauthor:: Gerhard Häring <[email protected]> |
| 8 |
.. sectionauthor:: Gerhard Häring <[email protected]> |
| 9 |
|
| 10 |
**Source code:** :source:`Lib/hmac.py` |
| 11 |
|
| 12 |
-------------- |
| 13 |
|
| 14 |
This module implements the HMAC algorithm as described by :rfc:`2104`. |
| 15 |
The interface allows to use any hash function with a *fixed* digest size. |
| 16 |
In particular, extendable output functions such as SHAKE-128 or SHAKE-256 |
| 17 |
cannot be used with HMAC. |
| 18 |
|
| 19 |
|
| 20 |
.. function:: new(key, msg=None, digestmod) |
| 21 |
|
| 22 |
Return a new hmac object. *key* is a bytes or bytearray object giving the |
| 23 |
secret key. If *msg* is present, the method call ``update(msg)`` is made. |
| 24 |
*digestmod* is the digest name, digest constructor or module for the HMAC |
| 25 |
object to use. It may be any name suitable to :func:`hashlib.new`. |
| 26 |
Despite its argument position, it is required. |
| 27 |
|
| 28 |
.. versionchanged:: 3.4 |
| 29 |
Parameter *key* can be a bytes or bytearray object. |
| 30 |
Parameter *msg* can be of any type supported by :mod:`hashlib`. |
| 31 |
Parameter *digestmod* can be the name of a hash algorithm. |
| 32 |
|
| 33 |
.. versionchanged:: 3.8 |
| 34 |
The *digestmod* argument is now required. Pass it as a keyword |
| 35 |
argument to avoid awkwardness when you do not have an initial *msg*. |
| 36 |
|
| 37 |
|
| 38 |
.. function:: digest(key, msg, digest) |
| 39 |
|
| 40 |
Return digest of *msg* for given secret *key* and *digest*. The |
| 41 |
function is equivalent to ``HMAC(key, msg, digest).digest()``, but |
| 42 |
uses an optimized C or inline implementation, which is faster for messages |
| 43 |
that fit into memory. The parameters *key*, *msg*, and *digest* have |
| 44 |
the same meaning as in :func:`~hmac.new`. |
| 45 |
|
| 46 |
CPython implementation detail, the optimized C implementation is only used |
| 47 |
when *digest* is a string and name of a digest algorithm, which is |
| 48 |
supported by OpenSSL. |
| 49 |
|
| 50 |
.. versionadded:: 3.7 |
| 51 |
|
| 52 |
|
| 53 |
.. class:: HMAC |
| 54 |
|
| 55 |
An HMAC object has the following methods: |
| 56 |
|
| 57 |
.. method:: HMAC.update(msg) |
| 58 |
|
| 59 |
Update the hmac object with *msg*. Repeated calls are equivalent to a |
| 60 |
single call with the concatenation of all the arguments: |
| 61 |
``m.update(a); m.update(b)`` is equivalent to ``m.update(a + b)``. |
| 62 |
|
| 63 |
.. versionchanged:: 3.4 |
| 64 |
Parameter *msg* can be of any type supported by :mod:`hashlib`. |
| 65 |
|
| 66 |
|
| 67 |
.. method:: HMAC.digest() |
| 68 |
|
| 69 |
Return the digest of the bytes passed to the :meth:`update` method so far. |
| 70 |
This bytes object will be the same length as the *digest_size* of the digest |
| 71 |
given to the constructor. It may contain non-ASCII bytes, including NUL |
| 72 |
bytes. |
| 73 |
|
| 74 |
.. warning:: |
| 75 |
|
| 76 |
When comparing the output of :meth:`digest` to an externally supplied |
| 77 |
digest during a verification routine, it is recommended to use the |
| 78 |
:func:`compare_digest` function instead of the ``==`` operator |
| 79 |
to reduce the vulnerability to timing attacks. |
| 80 |
|
| 81 |
|
| 82 |
.. method:: HMAC.hexdigest() |
| 83 |
|
| 84 |
Like :meth:`digest` except the digest is returned as a string twice the |
| 85 |
length containing only hexadecimal digits. This may be used to exchange the |
| 86 |
value safely in email or other non-binary environments. |
| 87 |
|
| 88 |
.. warning:: |
| 89 |
|
| 90 |
When comparing the output of :meth:`hexdigest` to an externally supplied |
| 91 |
digest during a verification routine, it is recommended to use the |
| 92 |
:func:`compare_digest` function instead of the ``==`` operator |
| 93 |
to reduce the vulnerability to timing attacks. |
| 94 |
|
| 95 |
|
| 96 |
.. method:: HMAC.copy() |
| 97 |
|
| 98 |
Return a copy ("clone") of the hmac object. This can be used to efficiently |
| 99 |
compute the digests of strings that share a common initial substring. |
| 100 |
|
| 101 |
|
| 102 |
A hash object has the following attributes: |
| 103 |
|
| 104 |
.. attribute:: HMAC.digest_size |
| 105 |
|
| 106 |
The size of the resulting HMAC digest in bytes. |
| 107 |
|
| 108 |
.. attribute:: HMAC.block_size |
| 109 |
|
| 110 |
The internal block size of the hash algorithm in bytes. |
| 111 |
|
| 112 |
.. versionadded:: 3.4 |
| 113 |
|
| 114 |
.. attribute:: HMAC.name |
| 115 |
|
| 116 |
The canonical name of this HMAC, always lowercase, e.g. ``hmac-md5``. |
| 117 |
|
| 118 |
.. versionadded:: 3.4 |
| 119 |
|
| 120 |
|
| 121 |
.. versionchanged:: 3.10 |
| 122 |
Removed the undocumented attributes ``HMAC.digest_cons``, ``HMAC.inner``, |
| 123 |
and ``HMAC.outer``. |
| 124 |
|
| 125 |
This module also provides the following helper function: |
| 126 |
|
| 127 |
.. function:: compare_digest(a, b) |
| 128 |
|
| 129 |
Return ``a == b``. This function uses an approach designed to prevent |
| 130 |
timing analysis by avoiding content-based short circuiting behaviour, |
| 131 |
making it appropriate for cryptography. *a* and *b* must both be of the |
| 132 |
same type: either :class:`str` (ASCII only, as e.g. returned by |
| 133 |
:meth:`HMAC.hexdigest`), or a :term:`bytes-like object`. |
| 134 |
|
| 135 |
.. note:: |
| 136 |
|
| 137 |
If *a* and *b* are of different lengths, or if an error occurs, |
| 138 |
a timing attack could theoretically reveal information about the |
| 139 |
types and lengths of *a* and *b*—but not their values. |
| 140 |
|
| 141 |
.. versionadded:: 3.3 |
| 142 |
|
| 143 |
.. versionchanged:: 3.10 |
| 144 |
|
| 145 |
The function uses OpenSSL's ``CRYPTO_memcmp()`` internally when |
| 146 |
available. |
| 147 |
|
| 148 |
|
| 149 |
.. seealso:: |
| 150 |
|
| 151 |
Module :mod:`hashlib` |
| 152 |
The Python module providing secure hash functions. |