blob: 968b7a4d24e0d7c5372563867d9e8bf3c65c33db [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001: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 Araujo29a0b572011-08-19 02:14:03 +020012**Source code:** :source:`Lib/hmac.py`
13
14--------------
15
Georg Brandl8ec7f652007-08-15 14:28:01 +000016This 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 Brandl8ec7f652007-08-15 14:28:01 +000025
26An HMAC object has the following methods:
27
28
29.. method:: hmac.update(msg)
30
31 Update the hmac object with the string *msg*. Repeated calls are equivalent to
32 a single call with the concatenation of all the arguments: ``m.update(a);
33 m.update(b)`` is equivalent to ``m.update(a + b)``.
34
35
36.. method:: hmac.digest()
37
38 Return the digest of the strings passed to the :meth:`update` method so far.
39 This string will be the same length as the *digest_size* of the digest given to
40 the constructor. It may contain non-ASCII characters, including NUL bytes.
41
42
43.. method:: hmac.hexdigest()
44
45 Like :meth:`digest` except the digest is returned as a string twice the length
46 containing only hexadecimal digits. This may be used to exchange the value
47 safely in email or other non-binary environments.
48
49
50.. method:: hmac.copy()
51
52 Return a copy ("clone") of the hmac object. This can be used to efficiently
53 compute the digests of strings that share a common initial substring.
54
55
56.. seealso::
57
58 Module :mod:`hashlib`
Ezio Melotti062d2b52009-12-19 22:41:49 +000059 The Python module providing secure hash functions.
Georg Brandl8ec7f652007-08-15 14:28:01 +000060