blob: e962ff0de7183355a9ddef9cffd102da1f003067 [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
Georg Brandl922ae2c2012-02-05 09:25:22 +010028.. method:: HMAC.update(msg)
Georg Brandl8ec7f652007-08-15 14:28:01 +000029
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 Brandl922ae2c2012-02-05 09:25:22 +010035.. method:: HMAC.digest()
Georg Brandl8ec7f652007-08-15 14:28:01 +000036
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 Brandl922ae2c2012-02-05 09:25:22 +010042.. method:: HMAC.hexdigest()
Georg Brandl8ec7f652007-08-15 14:28:01 +000043
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 Brandl922ae2c2012-02-05 09:25:22 +010049.. method:: HMAC.copy()
Georg Brandl8ec7f652007-08-15 14:28:01 +000050
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 Melotti062d2b52009-12-19 22:41:49 +000058 The Python module providing secure hash functions.
Georg Brandl8ec7f652007-08-15 14:28:01 +000059