Mercurial > cpython-lru_cache-weakref
annotate Lib/hmac.py @ 79691:05c8928362ee
Merge __next__ method link fixes with 3.3.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 12 Oct 2012 13:46:37 +0300 |
| parents | b0605b34b2de |
| children | 97834382c6cc |
| 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 |
|
43776
b310fd2b0184
Merged revisions 58862-58885 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
6 import warnings as _warnings |
|
77702
c82451eeb595
Issue #15061: Re-implemented hmac.compare_digest() in C
Christian Heimes <christian@cheimes.de>
parents:
77436
diff
changeset
|
7 from operator import _compare_digest as compare_digest |
|
43776
b310fd2b0184
Merged revisions 58862-58885 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
8 |
|
42179
f4a1b76a5a63
Make hmac use bytes. Make test_hmac pass.
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
9 trans_5C = bytes((x ^ 0x5C) for x in range(256)) |
|
f4a1b76a5a63
Make hmac use bytes. Make test_hmac pass.
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
10 trans_36 = bytes((x ^ 0x36) for x in range(256)) |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19862
diff
changeset
|
11 |
|
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
|
12 # The size of the digests returned by HMAC depends on the underlying |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35375
diff
changeset
|
13 # 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
|
14 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
|
15 |
|
31704
490eb64bc5c9
Speed HMAC.copy() by installing a secret backdoor argument to
Tim Peters <tim.peters@gmail.com>
parents:
23645
diff
changeset
|
16 |
|
76906
ddcc8ee680d7
Issue #14532: Add a secure_compare() helper to the hmac module, to mitigate
Charles-François Natali <neologix@free.fr>
parents:
73165
diff
changeset
|
17 |
|
19862
36ca16dc35b9
HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 class HMAC: |
|
43776
b310fd2b0184
Merged revisions 58862-58885 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
19 """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
|
20 |
|
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
|
21 This supports the API for Cryptographic Hash Functions (PEP 247). |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19862
diff
changeset
|
22 """ |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35375
diff
changeset
|
23 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
|
24 |
|
36ca16dc35b9
HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 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
|
26 """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
|
27 |
|
36ca16dc35b9
HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 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
|
29 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
|
30 digestmod: A module supporting PEP 247. *OR* |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
31704
diff
changeset
|
31 A hashlib constructor returning a new hash object. |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
31704
diff
changeset
|
32 Defaults to hashlib.md5. |
|
42179
f4a1b76a5a63
Make hmac use bytes. Make test_hmac pass.
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
33 |
|
f4a1b76a5a63
Make hmac use bytes. Make test_hmac pass.
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
34 Note: key and msg must be bytes objects. |
|
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 |
|
45627
24f4230baebe
Fixed failing unit tests due to str/bytes mismatch.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43776
diff
changeset
|
37 if not isinstance(key, bytes): |
|
77871
b0605b34b2de
Issue #15225: improve error message when hmac is passed a wrong key type.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77702
diff
changeset
|
38 raise TypeError("key: expected bytes, but got %r" % type(key).__name__) |
|
42179
f4a1b76a5a63
Make hmac use bytes. Make test_hmac pass.
Guido van Rossum <guido@python.org>
parents:
41824
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 |
|
73165
8e57b5d8f58f
Closes #13258: Use callable() built-in in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
64197
diff
changeset
|
44 if callable(digestmod): |
|
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: |
|
42179
f4a1b76a5a63
Make hmac use bytes. Make test_hmac pass.
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
47 self.digest_cons = lambda d=b'': digestmod.new(d) |
|
35375
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 |
|
43776
b310fd2b0184
Merged revisions 58862-58885 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
53 if hasattr(self.inner, 'block_size'): |
|
b310fd2b0184
Merged revisions 58862-58885 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
54 blocksize = self.inner.block_size |
|
b310fd2b0184
Merged revisions 58862-58885 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
55 if blocksize < 16: |
|
b310fd2b0184
Merged revisions 58862-58885 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
56 _warnings.warn('block_size of %d seems too small; using our ' |
|
b310fd2b0184
Merged revisions 58862-58885 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
57 'default of %d.' % (blocksize, self.blocksize), |
|
b310fd2b0184
Merged revisions 58862-58885 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
58 RuntimeWarning, 2) |
|
b310fd2b0184
Merged revisions 58862-58885 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
59 blocksize = self.blocksize |
|
b310fd2b0184
Merged revisions 58862-58885 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
60 else: |
|
b310fd2b0184
Merged revisions 58862-58885 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
61 _warnings.warn('No block_size attribute on given digest object; ' |
|
b310fd2b0184
Merged revisions 58862-58885 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
62 'Assuming %d.' % (self.blocksize), |
|
b310fd2b0184
Merged revisions 58862-58885 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
63 RuntimeWarning, 2) |
|
b310fd2b0184
Merged revisions 58862-58885 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
64 blocksize = self.blocksize |
|
b310fd2b0184
Merged revisions 58862-58885 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
65 |
|
19862
36ca16dc35b9
HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 if len(key) > blocksize: |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
31704
diff
changeset
|
67 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
|
68 |
|
42179
f4a1b76a5a63
Make hmac use bytes. Make test_hmac pass.
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
69 key = key + bytes(blocksize - len(key)) |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35375
diff
changeset
|
70 self.outer.update(key.translate(trans_5C)) |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35375
diff
changeset
|
71 self.inner.update(key.translate(trans_36)) |
|
23612
3caee2958387
Replace boolean test with is None
Raymond Hettinger <python@rcn.com>
parents:
23594
diff
changeset
|
72 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
|
73 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
|
74 |
|
36ca16dc35b9
HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 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
|
76 """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
|
77 """ |
|
45627
24f4230baebe
Fixed failing unit tests due to str/bytes mismatch.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43776
diff
changeset
|
78 if not isinstance(msg, bytes): |
|
24f4230baebe
Fixed failing unit tests due to str/bytes mismatch.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43776
diff
changeset
|
79 raise TypeError("expected bytes, but got %r" % type(msg).__name__) |
|
19862
36ca16dc35b9
HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 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
|
81 |
|
36ca16dc35b9
HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 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
|
83 """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
|
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 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
|
86 """ |
|
64197
8397f2e007eb
speed hack can be implemented more cleanly by directly calling __new__
Benjamin Peterson <benjamin@python.org>
parents:
64196
diff
changeset
|
87 # Call __new__ directly to avoid the expensive __init__. |
|
8397f2e007eb
speed hack can be implemented more cleanly by directly calling __new__
Benjamin Peterson <benjamin@python.org>
parents:
64196
diff
changeset
|
88 other = self.__class__.__new__(self.__class__) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
31704
diff
changeset
|
89 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
|
90 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
|
91 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
|
92 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
|
93 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
|
94 |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35375
diff
changeset
|
95 def _current(self): |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35375
diff
changeset
|
96 """Return a hash object for the current state. |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35375
diff
changeset
|
97 |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35375
diff
changeset
|
98 To be used only internally with digest() and hexdigest(). |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35375
diff
changeset
|
99 """ |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35375
diff
changeset
|
100 h = self.outer.copy() |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35375
diff
changeset
|
101 h.update(self.inner.digest()) |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35375
diff
changeset
|
102 return h |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35375
diff
changeset
|
103 |
|
19862
36ca16dc35b9
HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 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
|
105 """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
|
106 |
|
36ca16dc35b9
HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 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
|
108 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
|
109 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
|
110 """ |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35375
diff
changeset
|
111 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
|
112 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
|
113 |
|
36ca16dc35b9
HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 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
|
115 """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
|
116 """ |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35375
diff
changeset
|
117 h = self._current() |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35375
diff
changeset
|
118 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
|
119 |
|
36ca16dc35b9
HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 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
|
121 """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
|
122 |
|
36ca16dc35b9
HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 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
|
124 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
|
125 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
|
126 |
|
36ca16dc35b9
HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 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
|
128 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
|
129 method. |
|
36ca16dc35b9
HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 """ |
|
36ca16dc35b9
HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 return HMAC(key, msg, digestmod) |
