blob: 0abe42169b280c4a8d30fa437bbeafbe3dd6ac61 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`hmac` --- Keyed-Hashing for Message Authentication
3========================================================
4
5.. module:: hmac
6 :synopsis: Keyed-Hashing for Message Authentication (HMAC) implementation for Python.
7.. moduleauthor:: Gerhard Häring <ghaering@users.sourceforge.net>
8.. sectionauthor:: Gerhard Häring <ghaering@users.sourceforge.net>
9
10
Georg Brandl116aa622007-08-15 14:28:22 +000011This module implements the HMAC algorithm as described by :rfc:`2104`.
12
13
14.. function:: new(key[, msg[, digestmod]])
15
16 Return a new hmac object. If *msg* is present, the method call ``update(msg)``
17 is made. *digestmod* is the digest constructor or module for the HMAC object to
18 use. It defaults to the :func:`hashlib.md5` constructor.
19
20 .. note::
21
22 The md5 hash has known weaknesses but remains the default for backwards
23 compatibility. Choose a better one for your application.
24
25An HMAC object has the following methods:
26
27
28.. method:: hmac.update(msg)
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
35.. method:: hmac.digest()
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
42.. method:: hmac.hexdigest()
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
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`
58 The python module providing secure hash functions.
59