blob: 6ffd00aa04f47938cc7f1c9d54acd5d4ce560242 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +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
Georg Brandl116aa622007-08-15 14:28:22 +000010This module implements the HMAC algorithm as described by :rfc:`2104`.
11
12
Georg Brandl036490d2009-05-17 13:00:36 +000013.. function:: new(key, msg=None, digestmod=None)
Georg Brandl116aa622007-08-15 14:28:22 +000014
15 Return a new hmac object. If *msg* is present, the method call ``update(msg)``
16 is made. *digestmod* is the digest constructor or module for the HMAC object to
17 use. It defaults to the :func:`hashlib.md5` constructor.
18
19 .. note::
20
21 The md5 hash has known weaknesses but remains the default for backwards
22 compatibility. Choose a better one for your application.
23
24An HMAC object has the following methods:
25
26
27.. method:: hmac.update(msg)
28
29 Update the hmac object with the string *msg*. Repeated calls are equivalent to
30 a single call with the concatenation of all the arguments: ``m.update(a);
31 m.update(b)`` is equivalent to ``m.update(a + b)``.
32
33
34.. method:: hmac.digest()
35
36 Return the digest of the strings passed to the :meth:`update` method so far.
37 This string will be the same length as the *digest_size* of the digest given to
38 the constructor. It may contain non-ASCII characters, including NUL bytes.
39
40
41.. method:: hmac.hexdigest()
42
43 Like :meth:`digest` except the digest is returned as a string twice the length
44 containing only hexadecimal digits. This may be used to exchange the value
45 safely in email or other non-binary environments.
46
47
48.. method:: hmac.copy()
49
50 Return a copy ("clone") of the hmac object. This can be used to efficiently
51 compute the digests of strings that share a common initial substring.
52
53
54.. seealso::
55
56 Module :mod:`hashlib`
Ezio Melotti890c1932009-12-19 23:33:46 +000057 The Python module providing secure hash functions.
Georg Brandl116aa622007-08-15 14:28:22 +000058