1:mod:`hmac` --- Keyed-Hashing for Message Authentication 2======================================================== 3 4.. module:: hmac 5 :synopsis: Keyed-Hashing for Message Authentication (HMAC) implementation 6 7.. moduleauthor:: Gerhard Häring <ghaering@users.sourceforge.net> 8.. sectionauthor:: Gerhard Häring <ghaering@users.sourceforge.net> 9 10**Source code:** :source:`Lib/hmac.py` 11 12-------------- 13 14This module implements the HMAC algorithm as described by :rfc:`2104`. 15 16 17.. function:: new(key, msg=None, digestmod='') 18 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. 21 *digestmod* is the digest name, digest constructor or module for the HMAC 22 object to use. It may be any name suitable to :func:`hashlib.new`. 23 Despite its argument position, it is required. 24 25 .. versionchanged:: 3.4 26 Parameter *key* can be a bytes or bytearray object. 27 Parameter *msg* can be of any type supported by :mod:`hashlib`. 28 Parameter *digestmod* can be the name of a hash algorithm. 29 30 .. deprecated-removed:: 3.4 3.8 31 MD5 as implicit default digest for *digestmod* is deprecated. 32 The digestmod parameter is now required. Pass it as a keyword 33 argument to avoid awkwardness when you do not have an initial msg. 34 35 36.. 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 51An HMAC object has the following methods: 52 53.. method:: HMAC.update(msg) 54 55 Update the hmac object with *msg*. Repeated calls are equivalent to a 56 single call with the concatenation of all the arguments: 57 ``m.update(a); m.update(b)`` is equivalent to ``m.update(a + b)``. 58 59 .. versionchanged:: 3.4 60 Parameter *msg* can be of any type supported by :mod:`hashlib`. 61 62 63.. method:: HMAC.digest() 64 65 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. 69 70 .. 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 74 :func:`compare_digest` function instead of the ``==`` operator 75 to reduce the vulnerability to timing attacks. 76 77 78.. method:: HMAC.hexdigest() 79 80 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. 83 84 .. warning:: 85 86 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. 90 91 92.. method:: HMAC.copy() 93 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 98A 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 117.. deprecated:: 3.9 118 119 The undocumented attributes ``HMAC.digest_cons``, ``HMAC.inner``, and 120 ``HMAC.outer`` are internal implementation details and will be removed in 121 Python 3.10. 122 123This module also provides the following helper function: 124 125.. function:: compare_digest(a, b) 126 127 Return ``a == b``. This function uses an approach designed to prevent 128 timing analysis by avoiding content-based short circuiting behaviour, 129 making it appropriate for cryptography. *a* and *b* must both be of the 130 same type: either :class:`str` (ASCII only, as e.g. returned by 131 :meth:`HMAC.hexdigest`), or a :term:`bytes-like object`. 132 133 .. note:: 134 135 If *a* and *b* are of different lengths, or if an error occurs, 136 a timing attack could theoretically reveal information about the 137 types and lengths of *a* and *b*—but not their values. 138 139 .. versionadded:: 3.3 140 141 .. versionchanged:: 3.10 142 143 The function uses OpenSSL's ``CRYPTO_memcmp()`` internally when 144 available. 145 146 147.. seealso:: 148 149 Module :mod:`hashlib` 150 The Python module providing secure hash functions. 151