blob: 57ac8bb16120f5d75e44f168fe023a5809f3b6a4 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`hmac` --- Keyed-Hashing for Message Authentication
2========================================================
3
4.. module:: hmac
Georg Brandl80b75fd2010-10-17 09:43:35 +00005 :synopsis: Keyed-Hashing for Message Authentication (HMAC) implementation
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Gerhard Häring <ghaering@users.sourceforge.net>
8.. sectionauthor:: Gerhard Häring <ghaering@users.sourceforge.net>
9
Raymond Hettinger469271d2011-01-27 20:38:46 +000010**Source code:** :source:`Lib/hmac.py`
11
12--------------
Georg Brandl116aa622007-08-15 14:28:22 +000013
Georg Brandl116aa622007-08-15 14:28:22 +000014This module implements the HMAC algorithm as described by :rfc:`2104`.
15
16
Gregory P. Smithf33c57d52019-10-17 20:30:42 -070017.. function:: new(key, msg=None, digestmod='')
Georg Brandl116aa622007-08-15 14:28:22 +000018
Christian Heimes04926ae2013-07-01 13:08:42 +020019 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 Heimes634919a2013-11-20 17:23:06 +010021 *digestmod* is the digest name, digest constructor or module for the HMAC
Gregory P. Smithf33c57d52019-10-17 20:30:42 -070022 object to use. It may be any name suitable to :func:`hashlib.new`.
23 Despite its argument position, it is required.
Georg Brandl116aa622007-08-15 14:28:22 +000024
Christian Heimes04926ae2013-07-01 13:08:42 +020025 .. versionchanged:: 3.4
Larry Hastings3732ed22014-03-15 21:13:56 -070026 Parameter *key* can be a bytes or bytearray object.
27 Parameter *msg* can be of any type supported by :mod:`hashlib`.
Berker Peksag740c7302014-07-09 20:15:28 +030028 Parameter *digestmod* can be the name of a hash algorithm.
Christian Heimes634919a2013-11-20 17:23:06 +010029
Matthias Bussonnier8bb0b5b2018-05-22 15:55:31 -070030 .. deprecated-removed:: 3.4 3.8
Christian Heimes634919a2013-11-20 17:23:06 +010031 MD5 as implicit default digest for *digestmod* is deprecated.
Gregory P. Smithf33c57d52019-10-17 20:30:42 -070032 The digestmod parameter is now required. Pass it as a keyword
33 argument to avoid awkwardness when you do not have an initial msg.
Christian Heimes634919a2013-11-20 17:23:06 +010034
35
Christian Heimes2f050c72018-01-27 09:53:43 +010036.. function:: digest(key, msg, digest)
37
38 Return digest of *msg* for given secret *key* and *digest*. The
39 function is equivalent to ``HMAC(key, msg, digest).digest()``, but
40 uses an optimized C or inline implementation, which is faster for messages
41 that fit into memory. The parameters *key*, *msg*, and *digest* have
42 the same meaning as in :func:`~hmac.new`.
43
44 CPython implementation detail, the optimized C implementation is only used
45 when *digest* is a string and name of a digest algorithm, which is
46 supported by OpenSSL.
47
48 .. versionadded:: 3.7
49
50
Georg Brandl116aa622007-08-15 14:28:22 +000051An HMAC object has the following methods:
52
Georg Brandl9701eb62012-02-05 09:25:22 +010053.. method:: HMAC.update(msg)
Georg Brandl116aa622007-08-15 14:28:22 +000054
Christian Heimes04926ae2013-07-01 13:08:42 +020055 Update the hmac object with *msg*. Repeated calls are equivalent to a
56 single call with the concatenation of all the arguments:
Georg Brandl80b75fd2010-10-17 09:43:35 +000057 ``m.update(a); m.update(b)`` is equivalent to ``m.update(a + b)``.
Georg Brandl116aa622007-08-15 14:28:22 +000058
Christian Heimes04926ae2013-07-01 13:08:42 +020059 .. versionchanged:: 3.4
60 Parameter *msg* can be of any type supported by :mod:`hashlib`.
61
Georg Brandl116aa622007-08-15 14:28:22 +000062
Georg Brandl9701eb62012-02-05 09:25:22 +010063.. method:: HMAC.digest()
Georg Brandl116aa622007-08-15 14:28:22 +000064
Georg Brandl80b75fd2010-10-17 09:43:35 +000065 Return the digest of the bytes passed to the :meth:`update` method so far.
66 This bytes object will be the same length as the *digest_size* of the digest
67 given to the constructor. It may contain non-ASCII bytes, including NUL
68 bytes.
Georg Brandl116aa622007-08-15 14:28:22 +000069
Charles-François Natali7feb9f42012-05-13 19:53:07 +020070 .. warning::
71
72 When comparing the output of :meth:`digest` to an externally-supplied
73 digest during a verification routine, it is recommended to use the
Nick Coghlan807770e2012-06-15 21:14:08 +100074 :func:`compare_digest` function instead of the ``==`` operator
75 to reduce the vulnerability to timing attacks.
Charles-François Natali7feb9f42012-05-13 19:53:07 +020076
Georg Brandl116aa622007-08-15 14:28:22 +000077
Georg Brandl9701eb62012-02-05 09:25:22 +010078.. method:: HMAC.hexdigest()
Georg Brandl116aa622007-08-15 14:28:22 +000079
Georg Brandl80b75fd2010-10-17 09:43:35 +000080 Like :meth:`digest` except the digest is returned as a string twice the
81 length containing only hexadecimal digits. This may be used to exchange the
82 value safely in email or other non-binary environments.
Georg Brandl116aa622007-08-15 14:28:22 +000083
Charles-François Natali7feb9f42012-05-13 19:53:07 +020084 .. warning::
85
Antoine Pitrou9df73da2012-06-24 16:03:50 +020086 When comparing the output of :meth:`hexdigest` to an externally-supplied
87 digest during a verification routine, it is recommended to use the
88 :func:`compare_digest` function instead of the ``==`` operator
89 to reduce the vulnerability to timing attacks.
Charles-François Natali7feb9f42012-05-13 19:53:07 +020090
Georg Brandl116aa622007-08-15 14:28:22 +000091
Georg Brandl9701eb62012-02-05 09:25:22 +010092.. method:: HMAC.copy()
Georg Brandl116aa622007-08-15 14:28:22 +000093
94 Return a copy ("clone") of the hmac object. This can be used to efficiently
95 compute the digests of strings that share a common initial substring.
96
97
Christian Heimesc4ab1102013-11-20 17:35:06 +010098A hash object has the following attributes:
99
100.. attribute:: HMAC.digest_size
101
102 The size of the resulting HMAC digest in bytes.
103
104.. attribute:: HMAC.block_size
105
106 The internal block size of the hash algorithm in bytes.
107
108 .. versionadded:: 3.4
109
110.. attribute:: HMAC.name
111
112 The canonical name of this HMAC, always lowercase, e.g. ``hmac-md5``.
113
114 .. versionadded:: 3.4
115
116
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200117This module also provides the following helper function:
118
Nick Coghlan807770e2012-06-15 21:14:08 +1000119.. function:: compare_digest(a, b)
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200120
Antoine Pitrou5f762af2012-06-24 16:23:54 +0200121 Return ``a == b``. This function uses an approach designed to prevent
122 timing analysis by avoiding content-based short circuiting behaviour,
123 making it appropriate for cryptography. *a* and *b* must both be of the
124 same type: either :class:`str` (ASCII only, as e.g. returned by
Ezio Melottic228e962013-05-04 18:06:34 +0300125 :meth:`HMAC.hexdigest`), or a :term:`bytes-like object`.
Nick Coghlan807770e2012-06-15 21:14:08 +1000126
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200127 .. note::
128
Antoine Pitrouf61e7912012-06-24 16:21:10 +0200129 If *a* and *b* are of different lengths, or if an error occurs,
Larry Hastings48986d62012-06-25 00:59:34 -0700130 a timing attack could theoretically reveal information about the
Martin Panter357ed2e2016-11-21 00:15:20 +0000131 types and lengths of *a* and *b*—but not their values.
Antoine Pitrouf61e7912012-06-24 16:21:10 +0200132
Charles-François Natali9b704ec2012-05-15 21:00:32 +0200133 .. versionadded:: 3.3
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200134
Georg Brandla1bc35f2012-06-24 16:07:33 +0200135
Georg Brandl116aa622007-08-15 14:28:22 +0000136.. seealso::
137
138 Module :mod:`hashlib`
Ezio Melotti0639d5a2009-12-19 23:26:38 +0000139 The Python module providing secure hash functions.