blob: 1d928ea86002a2d9edfeda66c222135e8767b219 [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
6 for Python.
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
Georg Brandl036490d2009-05-17 13:00:36 +000017.. function:: new(key, msg=None, digestmod=None)
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.
21 *digestmod* is the digest constructor or module for the HMAC object to use.
Georg Brandl808ddaa2013-10-06 09:49:18 +020022 It 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
25 Parameter *key* can be a bytes or bytearray object. Parameter *msg* can
26 be of any type supported by :mod:`hashlib`.
Georg Brandl116aa622007-08-15 14:28:22 +000027
28An HMAC object has the following methods:
29
Georg Brandl9701eb62012-02-05 09:25:22 +010030.. method:: HMAC.update(msg)
Georg Brandl116aa622007-08-15 14:28:22 +000031
Christian Heimes04926ae2013-07-01 13:08:42 +020032 Update the hmac object with *msg*. Repeated calls are equivalent to a
33 single call with the concatenation of all the arguments:
Georg Brandl80b75fd2010-10-17 09:43:35 +000034 ``m.update(a); m.update(b)`` is equivalent to ``m.update(a + b)``.
Georg Brandl116aa622007-08-15 14:28:22 +000035
Christian Heimes04926ae2013-07-01 13:08:42 +020036 .. versionchanged:: 3.4
37 Parameter *msg* can be of any type supported by :mod:`hashlib`.
38
Georg Brandl116aa622007-08-15 14:28:22 +000039
Georg Brandl9701eb62012-02-05 09:25:22 +010040.. method:: HMAC.digest()
Georg Brandl116aa622007-08-15 14:28:22 +000041
Georg Brandl80b75fd2010-10-17 09:43:35 +000042 Return the digest of the bytes passed to the :meth:`update` method so far.
43 This bytes object will be the same length as the *digest_size* of the digest
44 given to the constructor. It may contain non-ASCII bytes, including NUL
45 bytes.
Georg Brandl116aa622007-08-15 14:28:22 +000046
Charles-François Natali7feb9f42012-05-13 19:53:07 +020047 .. warning::
48
49 When comparing the output of :meth:`digest` to an externally-supplied
50 digest during a verification routine, it is recommended to use the
Nick Coghlan807770e2012-06-15 21:14:08 +100051 :func:`compare_digest` function instead of the ``==`` operator
52 to reduce the vulnerability to timing attacks.
Charles-François Natali7feb9f42012-05-13 19:53:07 +020053
Georg Brandl116aa622007-08-15 14:28:22 +000054
Georg Brandl9701eb62012-02-05 09:25:22 +010055.. method:: HMAC.hexdigest()
Georg Brandl116aa622007-08-15 14:28:22 +000056
Georg Brandl80b75fd2010-10-17 09:43:35 +000057 Like :meth:`digest` except the digest is returned as a string twice the
58 length containing only hexadecimal digits. This may be used to exchange the
59 value safely in email or other non-binary environments.
Georg Brandl116aa622007-08-15 14:28:22 +000060
Charles-François Natali7feb9f42012-05-13 19:53:07 +020061 .. warning::
62
Antoine Pitrou9df73da2012-06-24 16:03:50 +020063 When comparing the output of :meth:`hexdigest` to an externally-supplied
64 digest during a verification routine, it is recommended to use the
65 :func:`compare_digest` function instead of the ``==`` operator
66 to reduce the vulnerability to timing attacks.
Charles-François Natali7feb9f42012-05-13 19:53:07 +020067
Georg Brandl116aa622007-08-15 14:28:22 +000068
Georg Brandl9701eb62012-02-05 09:25:22 +010069.. method:: HMAC.copy()
Georg Brandl116aa622007-08-15 14:28:22 +000070
71 Return a copy ("clone") of the hmac object. This can be used to efficiently
72 compute the digests of strings that share a common initial substring.
73
74
Charles-François Natali7feb9f42012-05-13 19:53:07 +020075This module also provides the following helper function:
76
Nick Coghlan807770e2012-06-15 21:14:08 +100077.. function:: compare_digest(a, b)
Charles-François Natali7feb9f42012-05-13 19:53:07 +020078
Antoine Pitrou5f762af2012-06-24 16:23:54 +020079 Return ``a == b``. This function uses an approach designed to prevent
80 timing analysis by avoiding content-based short circuiting behaviour,
81 making it appropriate for cryptography. *a* and *b* must both be of the
82 same type: either :class:`str` (ASCII only, as e.g. returned by
Ezio Melottic228e962013-05-04 18:06:34 +030083 :meth:`HMAC.hexdigest`), or a :term:`bytes-like object`.
Nick Coghlan807770e2012-06-15 21:14:08 +100084
Charles-François Natali7feb9f42012-05-13 19:53:07 +020085 .. note::
86
Antoine Pitrouf61e7912012-06-24 16:21:10 +020087 If *a* and *b* are of different lengths, or if an error occurs,
Larry Hastings48986d62012-06-25 00:59:34 -070088 a timing attack could theoretically reveal information about the
89 types and lengths of *a* and *b*--but not their values.
Antoine Pitrouf61e7912012-06-24 16:21:10 +020090
Charles-François Natali7feb9f42012-05-13 19:53:07 +020091
Charles-François Natali9b704ec2012-05-15 21:00:32 +020092 .. versionadded:: 3.3
Charles-François Natali7feb9f42012-05-13 19:53:07 +020093
Georg Brandla1bc35f2012-06-24 16:07:33 +020094
Georg Brandl116aa622007-08-15 14:28:22 +000095.. seealso::
96
97 Module :mod:`hashlib`
Ezio Melotti0639d5a2009-12-19 23:26:38 +000098 The Python module providing secure hash functions.