annotate Lib/hmac.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents bb8716095a81
children c3a67ce58245
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
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 import string
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 def _strxor(s1, s2):
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 """Utility method. XOR the two strings s1 and s2 (must have same length).
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 """
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 return "".join(map(lambda x, y: chr(ord(x) ^ ord(y)), s1, s2))
19953
0f9df563b65d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 19862
diff changeset
12
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 # The size of the digests returned by HMAC depends on the underlying
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 # hashing module used.
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 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
16
19862
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 class HMAC:
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 """RFC2104 HMAC class.
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
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
20 This supports the API for Cryptographic Hash Functions (PEP 247).
19953
0f9df563b65d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 19862
diff changeset
21 """
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
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 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
24 """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
25
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 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
27 msg: Initial input for the hash, if provided.
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 digestmod: A module supporting PEP 247. Defaults to the md5 module.
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 if 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
31 import md5
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 digestmod = md5
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
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
34 self.digestmod = digestmod
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 self.outer = digestmod.new()
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 self.inner = digestmod.new()
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
37 self.digest_size = digestmod.digest_size
20967
bb8716095a81 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20847
diff changeset
38
19862
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 blocksize = 64
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 ipad = "\x36" * blocksize
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 opad = "\x5C" * blocksize
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 if len(key) > blocksize:
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 key = digestmod.new(key).digest()
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 key = key + chr(0) * (blocksize - len(key))
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 self.outer.update(_strxor(key, opad))
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 self.inner.update(_strxor(key, ipad))
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 if (msg):
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 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
51
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 ## 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
53 ## 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
54
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 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
56 """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
57 """
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 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
59
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 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
61 """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
62
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 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
64 """
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
65 other = HMAC("")
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
66 other.digestmod = self.digestmod
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
67 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
68 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
69 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
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 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
72 """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
73
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 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
75 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
76 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
77 """
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 h = self.outer.copy()
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 h.update(self.inner.digest())
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 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
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 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
83 """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
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 return "".join([string.zfill(hex(ord(x))[2:], 2)
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 for x in tuple(self.digest())])
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 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
89 """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
90
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 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
92 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
93 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
94
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 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
96 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
97 method.
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 """
36ca16dc35b9 HMAC algorithm as described by RFC 2104, by Gerhard Häring (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 return HMAC(key, msg, digestmod)