[2] | 1 | :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 |
|
---|
[391] | 12 | **Source code:** :source:`Lib/hmac.py`
|
---|
| 13 |
|
---|
| 14 | --------------
|
---|
| 15 |
|
---|
[2] | 16 | This 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
|
---|
[391] | 23 | use. It defaults to the :data:`hashlib.md5` constructor.
|
---|
[2] | 24 |
|
---|
| 25 |
|
---|
| 26 | An HMAC object has the following methods:
|
---|
| 27 |
|
---|
[391] | 28 | .. method:: HMAC.update(msg)
|
---|
[2] | 29 |
|
---|
| 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 |
|
---|
[391] | 35 | .. method:: HMAC.digest()
|
---|
[2] | 36 |
|
---|
| 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 |
|
---|
| 41 |
|
---|
[391] | 42 | .. method:: HMAC.hexdigest()
|
---|
[2] | 43 |
|
---|
| 44 | Like :meth:`digest` except the digest is returned as a string twice the length
|
---|
| 45 | containing only hexadecimal digits. This may be used to exchange the value
|
---|
| 46 | safely in email or other non-binary environments.
|
---|
| 47 |
|
---|
| 48 |
|
---|
[391] | 49 | .. method:: HMAC.copy()
|
---|
[2] | 50 |
|
---|
| 51 | Return a copy ("clone") of the hmac object. This can be used to efficiently
|
---|
| 52 | compute the digests of strings that share a common initial substring.
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | .. seealso::
|
---|
| 56 |
|
---|
| 57 | Module :mod:`hashlib`
|
---|
| 58 | The Python module providing secure hash functions.
|
---|
| 59 |
|
---|