Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +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 | |
| 10 | .. versionadded:: 2.2 |
| 11 | |
Éric Araujo | 29a0b57 | 2011-08-19 02:14:03 +0200 | [diff] [blame] | 12 | **Source code:** :source:`Lib/hmac.py` |
| 13 | |
| 14 | -------------- |
| 15 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 |
| 23 | use. It defaults to the :func:`hashlib.md5` constructor. |
| 24 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 25 | |
| 26 | An HMAC object has the following methods: |
| 27 | |
Georg Brandl | 922ae2c | 2012-02-05 09:25:22 +0100 | [diff] [blame^] | 28 | .. method:: HMAC.update(msg) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 922ae2c | 2012-02-05 09:25:22 +0100 | [diff] [blame^] | 35 | .. method:: HMAC.digest() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 922ae2c | 2012-02-05 09:25:22 +0100 | [diff] [blame^] | 42 | .. method:: HMAC.hexdigest() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 922ae2c | 2012-02-05 09:25:22 +0100 | [diff] [blame^] | 49 | .. method:: HMAC.copy() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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` |
Ezio Melotti | 062d2b5 | 2009-12-19 22:41:49 +0000 | [diff] [blame] | 58 | The Python module providing secure hash functions. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 59 | |