Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | This module implements the HMAC algorithm as described by :rfc:`2104`. |
| 11 | |
| 12 | |
Georg Brandl | 036490d | 2009-05-17 13:00:36 +0000 | [diff] [blame] | 13 | .. function:: new(key, msg=None, digestmod=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | |
| 15 | Return a new hmac object. If *msg* is present, the method call ``update(msg)`` |
| 16 | is made. *digestmod* is the digest constructor or module for the HMAC object to |
| 17 | use. It defaults to the :func:`hashlib.md5` constructor. |
| 18 | |
| 19 | .. note:: |
| 20 | |
| 21 | The md5 hash has known weaknesses but remains the default for backwards |
| 22 | compatibility. Choose a better one for your application. |
| 23 | |
| 24 | An HMAC object has the following methods: |
| 25 | |
| 26 | |
| 27 | .. method:: hmac.update(msg) |
| 28 | |
| 29 | Update the hmac object with the string *msg*. Repeated calls are equivalent to |
| 30 | a single call with the concatenation of all the arguments: ``m.update(a); |
| 31 | m.update(b)`` is equivalent to ``m.update(a + b)``. |
| 32 | |
| 33 | |
| 34 | .. method:: hmac.digest() |
| 35 | |
| 36 | Return the digest of the strings passed to the :meth:`update` method so far. |
| 37 | This string will be the same length as the *digest_size* of the digest given to |
| 38 | the constructor. It may contain non-ASCII characters, including NUL bytes. |
| 39 | |
| 40 | |
| 41 | .. method:: hmac.hexdigest() |
| 42 | |
| 43 | Like :meth:`digest` except the digest is returned as a string twice the length |
| 44 | containing only hexadecimal digits. This may be used to exchange the value |
| 45 | safely in email or other non-binary environments. |
| 46 | |
| 47 | |
| 48 | .. method:: hmac.copy() |
| 49 | |
| 50 | Return a copy ("clone") of the hmac object. This can be used to efficiently |
| 51 | compute the digests of strings that share a common initial substring. |
| 52 | |
| 53 | |
| 54 | .. seealso:: |
| 55 | |
| 56 | Module :mod:`hashlib` |
Ezio Melotti | 0639d5a | 2009-12-19 23:26:38 +0000 | [diff] [blame] | 57 | The Python module providing secure hash functions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | |