Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3f42908

Browse files
committed
Make hmac use bytes. Make test_hmac pass.
1 parent f895307 commit 3f42908

2 files changed

Lines changed: 37 additions & 26 deletions

File tree

Lib/hmac.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
Implements the HMAC algorithm as described by RFC 2104.
44
"""
55

6-
trans_5C = "".join ([chr (x ^ 0x5C) for x in range(256)])
7-
trans_36 = "".join ([chr (x ^ 0x36) for x in range(256)])
6+
trans_5C = bytes((x ^ 0x5C) for x in range(256))
7+
trans_36 = bytes((x ^ 0x36) for x in range(256))
88

99
# The size of the digests returned by HMAC depends on the underlying
1010
# hashing module used. Use digest_size from the instance of HMAC instead.
@@ -30,19 +30,26 @@ def __init__(self, key, msg = None, digestmod = None):
3030
digestmod: A module supporting PEP 247. *OR*
3131
A hashlib constructor returning a new hash object.
3232
Defaults to hashlib.md5.
33+
34+
Note: key and msg must be bytes objects.
3335
"""
3436

3537
if key is _secret_backdoor_key: # cheap
3638
return
3739

40+
if not isinstance(key, bytes):
41+
if hasattr(key, "__index__"):
42+
raise TypeError("key can't be a number")
43+
key = bytes(key)
44+
3845
if digestmod is None:
3946
import hashlib
4047
digestmod = hashlib.md5
4148

4249
if hasattr(digestmod, '__call__'):
4350
self.digest_cons = digestmod
4451
else:
45-
self.digest_cons = lambda d='': digestmod.new(d)
52+
self.digest_cons = lambda d=b'': digestmod.new(d)
4653

4754
self.outer = self.digest_cons()
4855
self.inner = self.digest_cons()
@@ -52,7 +59,7 @@ def __init__(self, key, msg = None, digestmod = None):
5259
if len(key) > blocksize:
5360
key = self.digest_cons(key).digest()
5461

55-
key = key + chr(0) * (blocksize - len(key))
62+
key = key + bytes(blocksize - len(key))
5663
self.outer.update(key.translate(trans_5C))
5764
self.inner.update(key.translate(trans_36))
5865
if msg is not None:
@@ -64,6 +71,10 @@ def __init__(self, key, msg = None, digestmod = None):
6471
def update(self, msg):
6572
"""Update this hashing object with the string msg.
6673
"""
74+
if not isinstance(msg, bytes):
75+
if hasattr(msg, "__index__"):
76+
raise TypeError("msg can't be a number")
77+
msg = bytes(msg)
6778
self.inner.update(msg)
6879

6980
def copy(self):

Lib/test/test_hmac.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@ def md5test(key, data, digest):
1212
h = hmac.HMAC(key, data)
1313
self.assertEqual(h.hexdigest().upper(), digest.upper())
1414

15-
md5test(chr(0x0b) * 16,
16-
"Hi There",
15+
md5test(b"\x0b" * 16,
16+
b"Hi There",
1717
"9294727A3638BB1C13F48EF8158BFC9D")
1818

19-
md5test("Jefe",
20-
"what do ya want for nothing?",
19+
md5test(b"Jefe",
20+
b"what do ya want for nothing?",
2121
"750c783e6ab0b503eaa86e310a5db738")
2222

23-
md5test(chr(0xAA)*16,
24-
chr(0xDD)*50,
23+
md5test(b"\xaa" * 16,
24+
b"\xdd" * 50,
2525
"56be34521d144c88dbb8c733f0e8b3f6")
2626

2727
md5test("".join([chr(i) for i in range(1, 26)]),
28-
chr(0xCD) * 50,
28+
b"\xcd" * 50,
2929
"697eaf0aca3a3aea3a75164746ffaa79")
3030

3131
md5test(chr(0x0C) * 16,
3232
"Test With Truncation",
3333
"56461ef2342edc00f9bab995690efd4c")
3434

35-
md5test(chr(0xAA) * 80,
35+
md5test(b"\xaa" * 80,
3636
"Test Using Larger Than Block-Size Key - Hash Key First",
3737
"6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")
3838

39-
md5test(chr(0xAA) * 80,
39+
md5test(b"\xaa" * 80,
4040
("Test Using Larger Than Block-Size Key "
4141
"and Larger Than One Block-Size Data"),
4242
"6f630fad67cda0ee1fb1f562db3aa53e")
@@ -46,33 +46,33 @@ def shatest(key, data, digest):
4646
h = hmac.HMAC(key, data, digestmod=sha1)
4747
self.assertEqual(h.hexdigest().upper(), digest.upper())
4848

49-
shatest(chr(0x0b) * 20,
50-
"Hi There",
49+
shatest(b"\x0b" * 20,
50+
b"Hi There",
5151
"b617318655057264e28bc0b6fb378c8ef146be00")
5252

53-
shatest("Jefe",
54-
"what do ya want for nothing?",
53+
shatest(b"Jefe",
54+
b"what do ya want for nothing?",
5555
"effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")
5656

57-
shatest(chr(0xAA)*20,
58-
chr(0xDD)*50,
57+
shatest(b"\xAA" * 20,
58+
b"\xDD" * 50,
5959
"125d7342b9ac11cd91a39af48aa17b4f63f175d3")
6060

61-
shatest("".join([chr(i) for i in range(1, 26)]),
62-
chr(0xCD) * 50,
61+
shatest(bytes(range(1, 26)),
62+
b"\xCD" * 50,
6363
"4c9007f4026250c6bc8414f9bf50c86c2d7235da")
6464

6565
shatest(chr(0x0C) * 20,
6666
"Test With Truncation",
6767
"4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")
6868

69-
shatest(chr(0xAA) * 80,
70-
"Test Using Larger Than Block-Size Key - Hash Key First",
69+
shatest(b"\xAA" * 80,
70+
b"Test Using Larger Than Block-Size Key - Hash Key First",
7171
"aa4ae5e15272d00e95705637ce8a3b55ed402112")
7272

73-
shatest(chr(0xAA) * 80,
74-
("Test Using Larger Than Block-Size Key "
75-
"and Larger Than One Block-Size Data"),
73+
shatest(b"\xAA" * 80,
74+
(b"Test Using Larger Than Block-Size Key "
75+
b"and Larger Than One Block-Size Data"),
7676
"e8e99d0f45237d786d6bbaa7965c7808bbff1a91")
7777

7878

0 commit comments

Comments
 (0)