blob: 09e819ae5a204390edcf4ec0c7caad3a998579e2 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`hmac` --- Keyed-Hashing for Message Authentication
2========================================================
3
4.. module:: hmac
5 :synopsis: Keyed-Hashing for Message Authentication (HMAC) implementation for Python.
6.. moduleauthor:: Gerhard Häring <ghaering@users.sourceforge.net>
7.. sectionauthor:: Gerhard Häring <ghaering@users.sourceforge.net>
8
9
10.. versionadded:: 2.2
11
Éric Araujo29a0b572011-08-19 02:14:03 +020012**Source code:** :source:`Lib/hmac.py`
13
14--------------
15
Georg Brandl8ec7f652007-08-15 14:28:01 +000016This module implements the HMAC algorithm as described by :rfc:`2104`.
17
18
19.. function:: new(key[, msg[, digestmod]])
20
21 Return a new hmac object. If *msg* is present, the method call ``update(msg)``
22 is made. *digestmod* is the digest constructor or module for the HMAC object to
Georg Brandl05c8baa2013-10-06 09:48:26 +020023 use. It defaults to the :data:`hashlib.md5` constructor.
Georg Brandl8ec7f652007-08-15 14:28:01 +000024
Georg Brandl8ec7f652007-08-15 14:28:01 +000025
26An HMAC object has the following methods:
27
Georg Brandl922ae2c2012-02-05 09:25:22 +010028.. method:: HMAC.update(msg)
Georg Brandl8ec7f652007-08-15 14:28:01 +000029
30 Update the hmac object with the string *msg*. Repeated calls are equivalent to
31 a single call with the concatenation of all the arguments: ``m.update(a);
32 m.update(b)`` is equivalent to ``m.update(a + b)``.
33
34
Georg Brandl922ae2c2012-02-05 09:25:22 +010035.. method:: HMAC.digest()
Georg Brandl8ec7f652007-08-15 14:28:01 +000036
37 Return the digest of the strings passed to the :meth:`update` method so far.
38 This string will be the same length as the *digest_size* of the digest given to
39 the constructor. It may contain non-ASCII characters, including NUL bytes.
40
Benjamin Peterson629026a2014-05-11 16:11:44 -070041 .. warning::
42
43 When comparing the output of :meth:`digest` to an externally-supplied
44 digest during a verification routine, it is recommended to use the
45 :func:`compare_digest` function instead of the ``==`` operator
46 to reduce the vulnerability to timing attacks.
47
Georg Brandl8ec7f652007-08-15 14:28:01 +000048
Georg Brandl922ae2c2012-02-05 09:25:22 +010049.. method:: HMAC.hexdigest()
Georg Brandl8ec7f652007-08-15 14:28:01 +000050
51 Like :meth:`digest` except the digest is returned as a string twice the length
52 containing only hexadecimal digits. This may be used to exchange the value
53 safely in email or other non-binary environments.
54
Benjamin Peterson629026a2014-05-11 16:11:44 -070055 .. warning::
56
57 When comparing the output of :meth:`hexdigest` to an externally-supplied
58 digest during a verification routine, it is recommended to use the
59 :func:`compare_digest` function instead of the ``==`` operator
60 to reduce the vulnerability to timing attacks.
61
Georg Brandl8ec7f652007-08-15 14:28:01 +000062
Georg Brandl922ae2c2012-02-05 09:25:22 +010063.. method:: HMAC.copy()
Georg Brandl8ec7f652007-08-15 14:28:01 +000064
65 Return a copy ("clone") of the hmac object. This can be used to efficiently
66 compute the digests of strings that share a common initial substring.
67
68
Benjamin Peterson629026a2014-05-11 16:11:44 -070069This module also provides the following helper function:
70
71.. function:: compare_digest(a, b)
72
73 Return ``a == b``. This function uses an approach designed to prevent
74 timing analysis by avoiding content-based short circuiting behaviour,
75 making it appropriate for cryptography. *a* and *b* must both be of the
76 same type: either :class:`unicode` or a :term:`bytes-like object`.
77
78 .. note::
79
80 If *a* and *b* are of different lengths, or if an error occurs,
81 a timing attack could theoretically reveal information about the
82 types and lengths of *a* and *b*--but not their values.
83
84
85 .. versionadded:: 2.7.7
86
87
Georg Brandl8ec7f652007-08-15 14:28:01 +000088.. seealso::
89
90 Module :mod:`hashlib`
Ezio Melotti062d2b52009-12-19 22:41:49 +000091 The Python module providing secure hash functions.
Georg Brandl8ec7f652007-08-15 14:28:01 +000092