X Tutup
Skip to content

Commit 1ccdff9

Browse files
committed
[Patch #477336] Make hmac.py match PEP247, and fix the copy method() so that
it works
1 parent a0b6035 commit 1ccdff9

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

Lib/hmac.py

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ def _strxor(s1, s2):
1010
"""
1111
return "".join(map(lambda x, y: chr(ord(x) ^ ord(y)), s1, s2))
1212

13+
# The size of the digests returned by HMAC depends on the underlying
14+
# hashing module used.
15+
digest_size = None
16+
1317
class HMAC:
1418
"""RFC2104 HMAC class.
1519
16-
This (mostly) supports the API for Cryptographic Hash Functions (PEP 247).
20+
This supports the API for Cryptographic Hash Functions (PEP 247).
1721
"""
1822

1923
def __init__(self, key, msg = None, digestmod = None):
@@ -27,9 +31,11 @@ def __init__(self, key, msg = None, digestmod = None):
2731
import md5
2832
digestmod = md5
2933

34+
self.digestmod = digestmod
3035
self.outer = digestmod.new()
3136
self.inner = digestmod.new()
32-
37+
self.digest_size = digestmod.digest_size
38+
3339
blocksize = 64
3440
ipad = "\x36" * blocksize
3541
opad = "\x5C" * blocksize
@@ -56,7 +62,11 @@ def copy(self):
5662
5763
An update to this copy won't affect the original object.
5864
"""
59-
return HMAC(self)
65+
other = HMAC("")
66+
other.digestmod = self.digestmod
67+
other.inner = self.inner.copy()
68+
other.outer = self.outer.copy()
69+
return other
6070

6171
def digest(self):
6272
"""Return the hash value of this hashing object.
@@ -88,23 +98,3 @@ def new(key, msg = None, digestmod = None):
8898
"""
8999
return HMAC(key, msg, digestmod)
90100

91-
def test():
92-
def md5test(key, data, digest):
93-
h = HMAC(key, data)
94-
assert(h.hexdigest().upper() == digest.upper())
95-
96-
# Test vectors from the RFC
97-
md5test(chr(0x0b) * 16,
98-
"Hi There",
99-
"9294727A3638BB1C13F48EF8158BFC9D")
100-
101-
md5test("Jefe",
102-
"what do ya want for nothing?",
103-
"750c783e6ab0b503eaa86e310a5db738")
104-
105-
md5test(chr(0xAA)*16,
106-
chr(0xDD)*50,
107-
"56be34521d144c88dbb8c733f0e8b3f6")
108-
109-
if __name__ == "__main__":
110-
test()

0 commit comments

Comments
 (0)
X Tutup