Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`hmac` --- Keyed-Hashing for Message Authentication |
| 2 | ======================================================== |
| 3 | |
| 4 | .. module:: hmac |
Georg Brandl | 80b75fd | 2010-10-17 09:43:35 +0000 | [diff] [blame] | 5 | :synopsis: Keyed-Hashing for Message Authentication (HMAC) implementation |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | .. moduleauthor:: Gerhard Häring <ghaering@users.sourceforge.net> |
| 8 | .. sectionauthor:: Gerhard Häring <ghaering@users.sourceforge.net> |
| 9 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 10 | **Source code:** :source:`Lib/hmac.py` |
| 11 | |
| 12 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | This module implements the HMAC algorithm as described by :rfc:`2104`. |
| 15 | |
| 16 | |
Georg Brandl | 036490d | 2009-05-17 13:00:36 +0000 | [diff] [blame] | 17 | .. function:: new(key, msg=None, digestmod=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | |
Christian Heimes | 04926ae | 2013-07-01 13:08:42 +0200 | [diff] [blame] | 19 | Return a new hmac object. *key* is a bytes or bytearray object giving the |
| 20 | secret key. If *msg* is present, the method call ``update(msg)`` is made. |
Christian Heimes | 634919a | 2013-11-20 17:23:06 +0100 | [diff] [blame] | 21 | *digestmod* is the digest name, digest constructor or module for the HMAC |
Matthias Bussonnier | 51a4743 | 2018-09-10 20:10:01 +0200 | [diff] [blame] | 22 | object to use. It supports any name suitable to :func:`hashlib.new`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 23 | |
Christian Heimes | 04926ae | 2013-07-01 13:08:42 +0200 | [diff] [blame] | 24 | .. versionchanged:: 3.4 |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 25 | Parameter *key* can be a bytes or bytearray object. |
| 26 | Parameter *msg* can be of any type supported by :mod:`hashlib`. |
Berker Peksag | 740c730 | 2014-07-09 20:15:28 +0300 | [diff] [blame] | 27 | Parameter *digestmod* can be the name of a hash algorithm. |
Christian Heimes | 634919a | 2013-11-20 17:23:06 +0100 | [diff] [blame] | 28 | |
Matthias Bussonnier | 8bb0b5b | 2018-05-22 15:55:31 -0700 | [diff] [blame] | 29 | .. deprecated-removed:: 3.4 3.8 |
Christian Heimes | 634919a | 2013-11-20 17:23:06 +0100 | [diff] [blame] | 30 | MD5 as implicit default digest for *digestmod* is deprecated. |
| 31 | |
| 32 | |
Christian Heimes | 2f050c7 | 2018-01-27 09:53:43 +0100 | [diff] [blame] | 33 | .. function:: digest(key, msg, digest) |
| 34 | |
| 35 | Return digest of *msg* for given secret *key* and *digest*. The |
| 36 | function is equivalent to ``HMAC(key, msg, digest).digest()``, but |
| 37 | uses an optimized C or inline implementation, which is faster for messages |
| 38 | that fit into memory. The parameters *key*, *msg*, and *digest* have |
| 39 | the same meaning as in :func:`~hmac.new`. |
| 40 | |
| 41 | CPython implementation detail, the optimized C implementation is only used |
| 42 | when *digest* is a string and name of a digest algorithm, which is |
| 43 | supported by OpenSSL. |
| 44 | |
| 45 | .. versionadded:: 3.7 |
| 46 | |
| 47 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 48 | An HMAC object has the following methods: |
| 49 | |
Georg Brandl | 9701eb6 | 2012-02-05 09:25:22 +0100 | [diff] [blame] | 50 | .. method:: HMAC.update(msg) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | |
Christian Heimes | 04926ae | 2013-07-01 13:08:42 +0200 | [diff] [blame] | 52 | Update the hmac object with *msg*. Repeated calls are equivalent to a |
| 53 | single call with the concatenation of all the arguments: |
Georg Brandl | 80b75fd | 2010-10-17 09:43:35 +0000 | [diff] [blame] | 54 | ``m.update(a); m.update(b)`` is equivalent to ``m.update(a + b)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 | |
Christian Heimes | 04926ae | 2013-07-01 13:08:42 +0200 | [diff] [blame] | 56 | .. versionchanged:: 3.4 |
| 57 | Parameter *msg* can be of any type supported by :mod:`hashlib`. |
| 58 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
Georg Brandl | 9701eb6 | 2012-02-05 09:25:22 +0100 | [diff] [blame] | 60 | .. method:: HMAC.digest() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 61 | |
Georg Brandl | 80b75fd | 2010-10-17 09:43:35 +0000 | [diff] [blame] | 62 | Return the digest of the bytes passed to the :meth:`update` method so far. |
| 63 | This bytes object will be the same length as the *digest_size* of the digest |
| 64 | given to the constructor. It may contain non-ASCII bytes, including NUL |
| 65 | bytes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | |
Charles-François Natali | 7feb9f4 | 2012-05-13 19:53:07 +0200 | [diff] [blame] | 67 | .. warning:: |
| 68 | |
| 69 | When comparing the output of :meth:`digest` to an externally-supplied |
| 70 | digest during a verification routine, it is recommended to use the |
Nick Coghlan | 807770e | 2012-06-15 21:14:08 +1000 | [diff] [blame] | 71 | :func:`compare_digest` function instead of the ``==`` operator |
| 72 | to reduce the vulnerability to timing attacks. |
Charles-François Natali | 7feb9f4 | 2012-05-13 19:53:07 +0200 | [diff] [blame] | 73 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | |
Georg Brandl | 9701eb6 | 2012-02-05 09:25:22 +0100 | [diff] [blame] | 75 | .. method:: HMAC.hexdigest() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | |
Georg Brandl | 80b75fd | 2010-10-17 09:43:35 +0000 | [diff] [blame] | 77 | Like :meth:`digest` except the digest is returned as a string twice the |
| 78 | length containing only hexadecimal digits. This may be used to exchange the |
| 79 | value safely in email or other non-binary environments. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 80 | |
Charles-François Natali | 7feb9f4 | 2012-05-13 19:53:07 +0200 | [diff] [blame] | 81 | .. warning:: |
| 82 | |
Antoine Pitrou | 9df73da | 2012-06-24 16:03:50 +0200 | [diff] [blame] | 83 | When comparing the output of :meth:`hexdigest` to an externally-supplied |
| 84 | digest during a verification routine, it is recommended to use the |
| 85 | :func:`compare_digest` function instead of the ``==`` operator |
| 86 | to reduce the vulnerability to timing attacks. |
Charles-François Natali | 7feb9f4 | 2012-05-13 19:53:07 +0200 | [diff] [blame] | 87 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | |
Georg Brandl | 9701eb6 | 2012-02-05 09:25:22 +0100 | [diff] [blame] | 89 | .. method:: HMAC.copy() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 | |
| 91 | Return a copy ("clone") of the hmac object. This can be used to efficiently |
| 92 | compute the digests of strings that share a common initial substring. |
| 93 | |
| 94 | |
Christian Heimes | c4ab110 | 2013-11-20 17:35:06 +0100 | [diff] [blame] | 95 | A hash object has the following attributes: |
| 96 | |
| 97 | .. attribute:: HMAC.digest_size |
| 98 | |
| 99 | The size of the resulting HMAC digest in bytes. |
| 100 | |
| 101 | .. attribute:: HMAC.block_size |
| 102 | |
| 103 | The internal block size of the hash algorithm in bytes. |
| 104 | |
| 105 | .. versionadded:: 3.4 |
| 106 | |
| 107 | .. attribute:: HMAC.name |
| 108 | |
| 109 | The canonical name of this HMAC, always lowercase, e.g. ``hmac-md5``. |
| 110 | |
| 111 | .. versionadded:: 3.4 |
| 112 | |
| 113 | |
Charles-François Natali | 7feb9f4 | 2012-05-13 19:53:07 +0200 | [diff] [blame] | 114 | This module also provides the following helper function: |
| 115 | |
Nick Coghlan | 807770e | 2012-06-15 21:14:08 +1000 | [diff] [blame] | 116 | .. function:: compare_digest(a, b) |
Charles-François Natali | 7feb9f4 | 2012-05-13 19:53:07 +0200 | [diff] [blame] | 117 | |
Antoine Pitrou | 5f762af | 2012-06-24 16:23:54 +0200 | [diff] [blame] | 118 | Return ``a == b``. This function uses an approach designed to prevent |
| 119 | timing analysis by avoiding content-based short circuiting behaviour, |
| 120 | making it appropriate for cryptography. *a* and *b* must both be of the |
| 121 | same type: either :class:`str` (ASCII only, as e.g. returned by |
Ezio Melotti | c228e96 | 2013-05-04 18:06:34 +0300 | [diff] [blame] | 122 | :meth:`HMAC.hexdigest`), or a :term:`bytes-like object`. |
Nick Coghlan | 807770e | 2012-06-15 21:14:08 +1000 | [diff] [blame] | 123 | |
Charles-François Natali | 7feb9f4 | 2012-05-13 19:53:07 +0200 | [diff] [blame] | 124 | .. note:: |
| 125 | |
Antoine Pitrou | f61e791 | 2012-06-24 16:21:10 +0200 | [diff] [blame] | 126 | If *a* and *b* are of different lengths, or if an error occurs, |
Larry Hastings | 48986d6 | 2012-06-25 00:59:34 -0700 | [diff] [blame] | 127 | a timing attack could theoretically reveal information about the |
Martin Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 128 | types and lengths of *a* and *b*—but not their values. |
Antoine Pitrou | f61e791 | 2012-06-24 16:21:10 +0200 | [diff] [blame] | 129 | |
Charles-François Natali | 7feb9f4 | 2012-05-13 19:53:07 +0200 | [diff] [blame] | 130 | |
Charles-François Natali | 9b704ec | 2012-05-15 21:00:32 +0200 | [diff] [blame] | 131 | .. versionadded:: 3.3 |
Charles-François Natali | 7feb9f4 | 2012-05-13 19:53:07 +0200 | [diff] [blame] | 132 | |
Georg Brandl | a1bc35f | 2012-06-24 16:07:33 +0200 | [diff] [blame] | 133 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 134 | .. seealso:: |
| 135 | |
| 136 | Module :mod:`hashlib` |
Ezio Melotti | 0639d5a | 2009-12-19 23:26:38 +0000 | [diff] [blame] | 137 | The Python module providing secure hash functions. |