blob: 0ca3eda083dcc706d8bbd8ae5ac8393a7fcd5431 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`hmac` --- Keyed-Hashing for Message Authentication
2========================================================
3
4.. module:: hmac
Georg Brandl80b75fd2010-10-17 09:43:35 +00005 :synopsis: Keyed-Hashing for Message Authentication (HMAC) implementation
6 for Python.
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Gerhard Häring <ghaering@users.sourceforge.net>
8.. sectionauthor:: Gerhard Häring <ghaering@users.sourceforge.net>
9
Raymond Hettinger469271d2011-01-27 20:38:46 +000010**Source code:** :source:`Lib/hmac.py`
11
12--------------
Georg Brandl116aa622007-08-15 14:28:22 +000013
Georg Brandl116aa622007-08-15 14:28:22 +000014This module implements the HMAC algorithm as described by :rfc:`2104`.
15
16
Georg Brandl036490d2009-05-17 13:00:36 +000017.. function:: new(key, msg=None, digestmod=None)
Georg Brandl116aa622007-08-15 14:28:22 +000018
Georg Brandl80b75fd2010-10-17 09:43:35 +000019 Return a new hmac object. *key* is a bytes object giving the secret key. If
20 *msg* is present, the method call ``update(msg)`` is made. *digestmod* is
21 the digest constructor or module for the HMAC object to use. It defaults to
22 the :func:`hashlib.md5` constructor.
Georg Brandl116aa622007-08-15 14:28:22 +000023
Georg Brandl116aa622007-08-15 14:28:22 +000024
25An HMAC object has the following methods:
26
Georg Brandl116aa622007-08-15 14:28:22 +000027.. method:: hmac.update(msg)
28
Georg Brandl80b75fd2010-10-17 09:43:35 +000029 Update the hmac object with the bytes object *msg*. Repeated calls are
30 equivalent to a single call with the concatenation of all the arguments:
31 ``m.update(a); m.update(b)`` is equivalent to ``m.update(a + b)``.
Georg Brandl116aa622007-08-15 14:28:22 +000032
33
34.. method:: hmac.digest()
35
Georg Brandl80b75fd2010-10-17 09:43:35 +000036 Return the digest of the bytes passed to the :meth:`update` method so far.
37 This bytes object will be the same length as the *digest_size* of the digest
38 given to the constructor. It may contain non-ASCII bytes, including NUL
39 bytes.
Georg Brandl116aa622007-08-15 14:28:22 +000040
41
42.. method:: hmac.hexdigest()
43
Georg Brandl80b75fd2010-10-17 09:43:35 +000044 Like :meth:`digest` except the digest is returned as a string twice the
45 length containing only hexadecimal digits. This may be used to exchange the
46 value safely in email or other non-binary environments.
Georg Brandl116aa622007-08-15 14:28:22 +000047
48
49.. method:: hmac.copy()
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 Melotti0639d5a2009-12-19 23:26:38 +000058 The Python module providing secure hash functions.