annotate Lib/hmac.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 1a059aafe49f
children 787b969d37f0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19862
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """HMAC (Keyed-Hashing for Message Authentication) Python module.
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 Implements the HMAC algorithm as described by RFC 2104.
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 """
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
43773
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
6 import warnings as _warnings
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
7
40473
21480d89b47f [Patch #1618455 by Ben Maurer] Improve speed of HMAC by using str.translate()
Andrew M. Kuchling <amk@amk.ca>
parents: 35375
diff changeset
8 trans_5C = "".join ([chr (x ^ 0x5C) for x in xrange(256)])
21480d89b47f [Patch #1618455 by Ben Maurer] Improve speed of HMAC by using str.translate()
Andrew M. Kuchling <amk@amk.ca>
parents: 35375
diff changeset
9 trans_36 = "".join ([chr (x ^ 0x36) for x in xrange(256)])
19953
0f9df563b65d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 19862
diff changeset
10
20847
ec632a41ee35 [Patch #477336] Make hmac.py match PEP247, and fix the copy method() so that
Andrew M. Kuchling <amk@amk.ca>
parents: 19953
diff changeset
11 # The size of the digests returned by HMAC depends on the underlying
40521
1e1b9c2e2af0 [Part of patch #1182394] Move the HMAC blocksize to be a class-level
Andrew M. Kuchling <amk@amk.ca>
parents: 40473
diff changeset
12 # hashing module used. Use digest_size from the instance of HMAC instead.
20847
ec632a41ee35 [Patch #477336] Make hmac.py match PEP247, and fix the copy method() so that
Andrew M. Kuchling <amk@amk.ca>
parents: 19953
diff changeset
13 digest_size = None
ec632a41ee35 [Patch #477336] Make hmac.py match PEP247, and fix the copy method() so that
Andrew M. Kuchling <amk@amk.ca>
parents: 19953
diff changeset
14
31704
490eb64bc5c9 Speed HMAC.copy() by installing a secret backdoor argument to
Tim Peters <tim.peters@gmail.com>
parents: 23645
diff changeset
15 # A unique object passed by HMAC.copy() to the HMAC constructor, in order
490eb64bc5c9 Speed HMAC.copy() by installing a secret backdoor argument to
Tim Peters <tim.peters@gmail.com>
parents: 23645
diff changeset
16 # that the latter return very quickly. HMAC("") in contrast is quite
490eb64bc5c9 Speed HMAC.copy() by installing a secret backdoor argument to
Tim Peters <tim.peters@gmail.com>
parents: 23645
diff changeset
17 # expensive.
490eb64bc5c9 Speed HMAC.copy() by installing a secret backdoor argument to
Tim Peters <tim.peters@gmail.com>
parents: 23645
diff changeset
18 _secret_backdoor_key = []
490eb64bc5c9 Speed HMAC.copy() by installing a secret backdoor argument to
Tim Peters <tim.peters@gmail.com>
parents: 23645
diff changeset
19
19862
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 class HMAC:
43773
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
21 """RFC 2104 HMAC class. Also complies with RFC 4231.
19862
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
20847
ec632a41ee35 [Patch #477336] Make hmac.py match PEP247, and fix the copy method() so that
Andrew M. Kuchling <amk@amk.ca>
parents: 19953
diff changeset
23 This supports the API for Cryptographic Hash Functions (PEP 247).
19953
0f9df563b65d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 19862
diff changeset
24 """
40521
1e1b9c2e2af0 [Part of patch #1182394] Move the HMAC blocksize to be a class-level
Andrew M. Kuchling <amk@amk.ca>
parents: 40473
diff changeset
25 blocksize = 64 # 512-bit HMAC; can be changed in subclasses.
19862
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 def __init__(self, key, msg = None, digestmod = None):
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 """Create a new HMAC object.
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 key: key for the keyed hash object.
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 msg: Initial input for the hash, if provided.
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 31704
diff changeset
32 digestmod: A module supporting PEP 247. *OR*
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 31704
diff changeset
33 A hashlib constructor returning a new hash object.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 31704
diff changeset
34 Defaults to hashlib.md5.
19862
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 """
31704
490eb64bc5c9 Speed HMAC.copy() by installing a secret backdoor argument to
Tim Peters <tim.peters@gmail.com>
parents: 23645
diff changeset
36
490eb64bc5c9 Speed HMAC.copy() by installing a secret backdoor argument to
Tim Peters <tim.peters@gmail.com>
parents: 23645
diff changeset
37 if key is _secret_backdoor_key: # cheap
490eb64bc5c9 Speed HMAC.copy() by installing a secret backdoor argument to
Tim Peters <tim.peters@gmail.com>
parents: 23645
diff changeset
38 return
490eb64bc5c9 Speed HMAC.copy() by installing a secret backdoor argument to
Tim Peters <tim.peters@gmail.com>
parents: 23645
diff changeset
39
23594
c3a67ce58245 Replace '== None' with 'is None'
Raymond Hettinger <python@rcn.com>
parents: 20967
diff changeset
40 if digestmod is None:
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 31704
diff changeset
41 import hashlib
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 31704
diff changeset
42 digestmod = hashlib.md5
19862
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
48303
1a059aafe49f silence callable warning in hmac
Benjamin Peterson <benjamin@python.org>
parents: 43773
diff changeset
44 if hasattr(digestmod, '__call__'):
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 31704
diff changeset
45 self.digest_cons = digestmod
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 31704
diff changeset
46 else:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 31704
diff changeset
47 self.digest_cons = lambda d='': digestmod.new(d)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 31704
diff changeset
48
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 31704
diff changeset
49 self.outer = self.digest_cons()
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 31704
diff changeset
50 self.inner = self.digest_cons()
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 31704
diff changeset
51 self.digest_size = self.inner.digest_size
20967
bb8716095a81 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20847
diff changeset
52
43773
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
53 if hasattr(self.inner, 'block_size'):
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
54 blocksize = self.inner.block_size
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
55 if blocksize < 16:
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
56 # Very low blocksize, most likely a legacy value like
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
57 # Lib/sha.py and Lib/md5.py have.
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
58 _warnings.warn('block_size of %d seems too small; using our '
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
59 'default of %d.' % (blocksize, self.blocksize),
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
60 RuntimeWarning, 2)
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
61 blocksize = self.blocksize
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
62 else:
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
63 _warnings.warn('No block_size attribute on given digest object; '
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
64 'Assuming %d.' % (self.blocksize),
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
65 RuntimeWarning, 2)
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
66 blocksize = self.blocksize
d7db467cd940 Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents: 40522
diff changeset
67
19862
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 if len(key) > blocksize:
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 31704
diff changeset
69 key = self.digest_cons(key).digest()
19862
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 key = key + chr(0) * (blocksize - len(key))
40473
21480d89b47f [Patch #1618455 by Ben Maurer] Improve speed of HMAC by using str.translate()
Andrew M. Kuchling <amk@amk.ca>
parents: 35375
diff changeset
72 self.outer.update(key.translate(trans_5C))
21480d89b47f [Patch #1618455 by Ben Maurer] Improve speed of HMAC by using str.translate()
Andrew M. Kuchling <amk@amk.ca>
parents: 35375
diff changeset
73 self.inner.update(key.translate(trans_36))
23612
3caee2958387 Replace boolean test with is None
Raymond Hettinger <python@rcn.com>
parents: 23594
diff changeset
74 if msg is not None:
19862
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 self.update(msg)
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 ## def clear(self):
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 ## raise NotImplementedError, "clear() method not available in HMAC."
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 def update(self, msg):
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 """Update this hashing object with the string msg.
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 """
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 self.inner.update(msg)
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 def copy(self):
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 """Return a separate copy of this hashing object.
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 An update to this copy won't affect the original object.
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 """
40521
1e1b9c2e2af0 [Part of patch #1182394] Move the HMAC blocksize to be a class-level
Andrew M. Kuchling <amk@amk.ca>
parents: 40473
diff changeset
90 other = self.__class__(_secret_backdoor_key)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 31704
diff changeset
91 other.digest_cons = self.digest_cons
31704
490eb64bc5c9 Speed HMAC.copy() by installing a secret backdoor argument to
Tim Peters <tim.peters@gmail.com>
parents: 23645
diff changeset
92 other.digest_size = self.digest_size
20847
ec632a41ee35 [Patch #477336] Make hmac.py match PEP247, and fix the copy method() so that
Andrew M. Kuchling <amk@amk.ca>
parents: 19953
diff changeset
93 other.inner = self.inner.copy()
ec632a41ee35 [Patch #477336] Make hmac.py match PEP247, and fix the copy method() so that
Andrew M. Kuchling <amk@amk.ca>
parents: 19953
diff changeset
94 other.outer = self.outer.copy()
ec632a41ee35 [Patch #477336] Make hmac.py match PEP247, and fix the copy method() so that
Andrew M. Kuchling <amk@amk.ca>
parents: 19953
diff changeset
95 return other
19862
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
40522
12af6788d911 [Rest of patch #1182394] Add ._current() method so that we can use the written-in-C .hexdigest() method
Andrew M. Kuchling <amk@amk.ca>
parents: 40521
diff changeset
97 def _current(self):
12af6788d911 [Rest of patch #1182394] Add ._current() method so that we can use the written-in-C .hexdigest() method
Andrew M. Kuchling <amk@amk.ca>
parents: 40521
diff changeset
98 """Return a hash object for the current state.
12af6788d911 [Rest of patch #1182394] Add ._current() method so that we can use the written-in-C .hexdigest() method
Andrew M. Kuchling <amk@amk.ca>
parents: 40521
diff changeset
99
12af6788d911 [Rest of patch #1182394] Add ._current() method so that we can use the written-in-C .hexdigest() method
Andrew M. Kuchling <amk@amk.ca>
parents: 40521
diff changeset
100 To be used only internally with digest() and hexdigest().
12af6788d911 [Rest of patch #1182394] Add ._current() method so that we can use the written-in-C .hexdigest() method
Andrew M. Kuchling <amk@amk.ca>
parents: 40521
diff changeset
101 """
12af6788d911 [Rest of patch #1182394] Add ._current() method so that we can use the written-in-C .hexdigest() method
Andrew M. Kuchling <amk@amk.ca>
parents: 40521
diff changeset
102 h = self.outer.copy()
12af6788d911 [Rest of patch #1182394] Add ._current() method so that we can use the written-in-C .hexdigest() method
Andrew M. Kuchling <amk@amk.ca>
parents: 40521
diff changeset
103 h.update(self.inner.digest())
12af6788d911 [Rest of patch #1182394] Add ._current() method so that we can use the written-in-C .hexdigest() method
Andrew M. Kuchling <amk@amk.ca>
parents: 40521
diff changeset
104 return h
12af6788d911 [Rest of patch #1182394] Add ._current() method so that we can use the written-in-C .hexdigest() method
Andrew M. Kuchling <amk@amk.ca>
parents: 40521
diff changeset
105
19862
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 def digest(self):
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 """Return the hash value of this hashing object.
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 This returns a string containing 8-bit data. The object is
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 not altered in any way by this function; you can continue
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 updating the object after calling this function.
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 """
40522
12af6788d911 [Rest of patch #1182394] Add ._current() method so that we can use the written-in-C .hexdigest() method
Andrew M. Kuchling <amk@amk.ca>
parents: 40521
diff changeset
113 h = self._current()
19862
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 return h.digest()
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 def hexdigest(self):
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 """Like digest(), but returns a string of hexadecimal digits instead.
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 """
40522
12af6788d911 [Rest of patch #1182394] Add ._current() method so that we can use the written-in-C .hexdigest() method
Andrew M. Kuchling <amk@amk.ca>
parents: 40521
diff changeset
119 h = self._current()
12af6788d911 [Rest of patch #1182394] Add ._current() method so that we can use the written-in-C .hexdigest() method
Andrew M. Kuchling <amk@amk.ca>
parents: 40521
diff changeset
120 return h.hexdigest()
19862
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 def new(key, msg = None, digestmod = None):
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 """Create a new hashing object and return it.
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 key: The starting key for the hash.
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 msg: if available, will immediately be hashed into the object's starting
19953
0f9df563b65d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 19862
diff changeset
127 state.
19862
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
128
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 You can now feed arbitrary strings into the object using its update()
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 method, and can ask for the hash value at any time by calling its digest()
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 method.
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 """
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 return HMAC(key, msg, digestmod)