blob: 1446da6ee653bf6bbb7dd6ac8dc5cd7742b62ded [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
Georg Brandl116aa622007-08-15 14:28:22 +00006.. moduleauthor:: Gerhard Häring <ghaering@users.sourceforge.net>
7.. sectionauthor:: Gerhard Häring <ghaering@users.sourceforge.net>
8
Raymond Hettinger469271d2011-01-27 20:38:46 +00009**Source code:** :source:`Lib/hmac.py`
10
11--------------
Georg Brandl116aa622007-08-15 14:28:22 +000012
Georg Brandl116aa622007-08-15 14:28:22 +000013This module implements the HMAC algorithm as described by :rfc:`2104`.
14
15
Georg Brandl036490d2009-05-17 13:00:36 +000016.. function:: new(key, msg=None, digestmod=None)
Georg Brandl116aa622007-08-15 14:28:22 +000017
Christian Heimes04926ae2013-07-01 13:08:42 +020018 Return a new hmac object. *key* is a bytes or bytearray object giving the
19 secret key. If *msg* is present, the method call ``update(msg)`` is made.
Christian Heimes634919a2013-11-20 17:23:06 +010020 *digestmod* is the digest name, digest constructor or module for the HMAC
21 object to use. It supports any name suitable to :func:`hashlib.new` and
22 defaults to the :data:`hashlib.md5` constructor.
Georg Brandl116aa622007-08-15 14:28:22 +000023
Christian Heimes04926ae2013-07-01 13:08:42 +020024 .. versionchanged:: 3.4
Larry Hastings3732ed22014-03-15 21:13:56 -070025 Parameter *key* can be a bytes or bytearray object.
26 Parameter *msg* can be of any type supported by :mod:`hashlib`.
Berker Peksag740c7302014-07-09 20:15:28 +030027 Parameter *digestmod* can be the name of a hash algorithm.
Christian Heimes634919a2013-11-20 17:23:06 +010028
29 .. deprecated:: 3.4
30 MD5 as implicit default digest for *digestmod* is deprecated.
31
32
Georg Brandl116aa622007-08-15 14:28:22 +000033An HMAC object has the following methods:
34
Georg Brandl9701eb62012-02-05 09:25:22 +010035.. method:: HMAC.update(msg)
Georg Brandl116aa622007-08-15 14:28:22 +000036
Christian Heimes04926ae2013-07-01 13:08:42 +020037 Update the hmac object with *msg*. Repeated calls are equivalent to a
38 single call with the concatenation of all the arguments:
Georg Brandl80b75fd2010-10-17 09:43:35 +000039 ``m.update(a); m.update(b)`` is equivalent to ``m.update(a + b)``.
Georg Brandl116aa622007-08-15 14:28:22 +000040
Christian Heimes04926ae2013-07-01 13:08:42 +020041 .. versionchanged:: 3.4
42 Parameter *msg* can be of any type supported by :mod:`hashlib`.
43
Georg Brandl116aa622007-08-15 14:28:22 +000044
Georg Brandl9701eb62012-02-05 09:25:22 +010045.. method:: HMAC.digest()
Georg Brandl116aa622007-08-15 14:28:22 +000046
Georg Brandl80b75fd2010-10-17 09:43:35 +000047 Return the digest of the bytes passed to the :meth:`update` method so far.
48 This bytes object will be the same length as the *digest_size* of the digest
49 given to the constructor. It may contain non-ASCII bytes, including NUL
50 bytes.
Georg Brandl116aa622007-08-15 14:28:22 +000051
Charles-François Natali7feb9f42012-05-13 19:53:07 +020052 .. warning::
53
54 When comparing the output of :meth:`digest` to an externally-supplied
55 digest during a verification routine, it is recommended to use the
Nick Coghlan807770e2012-06-15 21:14:08 +100056 :func:`compare_digest` function instead of the ``==`` operator
57 to reduce the vulnerability to timing attacks.
Charles-François Natali7feb9f42012-05-13 19:53:07 +020058
Georg Brandl116aa622007-08-15 14:28:22 +000059
Georg Brandl9701eb62012-02-05 09:25:22 +010060.. method:: HMAC.hexdigest()
Georg Brandl116aa622007-08-15 14:28:22 +000061
Georg Brandl80b75fd2010-10-17 09:43:35 +000062 Like :meth:`digest` except the digest is returned as a string twice the
63 length containing only hexadecimal digits. This may be used to exchange the
64 value safely in email or other non-binary environments.
Georg Brandl116aa622007-08-15 14:28:22 +000065
Charles-François Natali7feb9f42012-05-13 19:53:07 +020066 .. warning::
67
Antoine Pitrou9df73da2012-06-24 16:03:50 +020068 When comparing the output of :meth:`hexdigest` to an externally-supplied
69 digest during a verification routine, it is recommended to use the
70 :func:`compare_digest` function instead of the ``==`` operator
71 to reduce the vulnerability to timing attacks.
Charles-François Natali7feb9f42012-05-13 19:53:07 +020072
Georg Brandl116aa622007-08-15 14:28:22 +000073
Georg Brandl9701eb62012-02-05 09:25:22 +010074.. method:: HMAC.copy()
Georg Brandl116aa622007-08-15 14:28:22 +000075
76 Return a copy ("clone") of the hmac object. This can be used to efficiently
77 compute the digests of strings that share a common initial substring.
78
79
Christian Heimesc4ab1102013-11-20 17:35:06 +010080A hash object has the following attributes:
81
82.. attribute:: HMAC.digest_size
83
84 The size of the resulting HMAC digest in bytes.
85
86.. attribute:: HMAC.block_size
87
88 The internal block size of the hash algorithm in bytes.
89
90 .. versionadded:: 3.4
91
92.. attribute:: HMAC.name
93
94 The canonical name of this HMAC, always lowercase, e.g. ``hmac-md5``.
95
96 .. versionadded:: 3.4
97
98
Charles-François Natali7feb9f42012-05-13 19:53:07 +020099This module also provides the following helper function:
100
Nick Coghlan807770e2012-06-15 21:14:08 +1000101.. function:: compare_digest(a, b)
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200102
Antoine Pitrou5f762af2012-06-24 16:23:54 +0200103 Return ``a == b``. This function uses an approach designed to prevent
104 timing analysis by avoiding content-based short circuiting behaviour,
105 making it appropriate for cryptography. *a* and *b* must both be of the
106 same type: either :class:`str` (ASCII only, as e.g. returned by
Ezio Melottic228e962013-05-04 18:06:34 +0300107 :meth:`HMAC.hexdigest`), or a :term:`bytes-like object`.
Nick Coghlan807770e2012-06-15 21:14:08 +1000108
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200109 .. note::
110
Antoine Pitrouf61e7912012-06-24 16:21:10 +0200111 If *a* and *b* are of different lengths, or if an error occurs,
Larry Hastings48986d62012-06-25 00:59:34 -0700112 a timing attack could theoretically reveal information about the
113 types and lengths of *a* and *b*--but not their values.
Antoine Pitrouf61e7912012-06-24 16:21:10 +0200114
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200115
Charles-François Natali9b704ec2012-05-15 21:00:32 +0200116 .. versionadded:: 3.3
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200117
Georg Brandla1bc35f2012-06-24 16:07:33 +0200118
Georg Brandl116aa622007-08-15 14:28:22 +0000119.. seealso::
120
121 Module :mod:`hashlib`
Ezio Melotti0639d5a2009-12-19 23:26:38 +0000122 The Python module providing secure hash functions.