mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
Make hmac use bytes. Make test_hmac pass.
This commit is contained in:
parent
f895307a94
commit
3f42908051
2 changed files with 37 additions and 26 deletions
19
Lib/hmac.py
19
Lib/hmac.py
|
@ -3,8 +3,8 @@
|
||||||
Implements the HMAC algorithm as described by RFC 2104.
|
Implements the HMAC algorithm as described by RFC 2104.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
trans_5C = "".join ([chr (x ^ 0x5C) for x in range(256)])
|
trans_5C = bytes((x ^ 0x5C) for x in range(256))
|
||||||
trans_36 = "".join ([chr (x ^ 0x36) for x in range(256)])
|
trans_36 = bytes((x ^ 0x36) for x in range(256))
|
||||||
|
|
||||||
# The size of the digests returned by HMAC depends on the underlying
|
# The size of the digests returned by HMAC depends on the underlying
|
||||||
# hashing module used. Use digest_size from the instance of HMAC instead.
|
# hashing module used. Use digest_size from the instance of HMAC instead.
|
||||||
|
@ -30,11 +30,18 @@ class HMAC:
|
||||||
digestmod: A module supporting PEP 247. *OR*
|
digestmod: A module supporting PEP 247. *OR*
|
||||||
A hashlib constructor returning a new hash object.
|
A hashlib constructor returning a new hash object.
|
||||||
Defaults to hashlib.md5.
|
Defaults to hashlib.md5.
|
||||||
|
|
||||||
|
Note: key and msg must be bytes objects.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if key is _secret_backdoor_key: # cheap
|
if key is _secret_backdoor_key: # cheap
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if not isinstance(key, bytes):
|
||||||
|
if hasattr(key, "__index__"):
|
||||||
|
raise TypeError("key can't be a number")
|
||||||
|
key = bytes(key)
|
||||||
|
|
||||||
if digestmod is None:
|
if digestmod is None:
|
||||||
import hashlib
|
import hashlib
|
||||||
digestmod = hashlib.md5
|
digestmod = hashlib.md5
|
||||||
|
@ -42,7 +49,7 @@ class HMAC:
|
||||||
if hasattr(digestmod, '__call__'):
|
if hasattr(digestmod, '__call__'):
|
||||||
self.digest_cons = digestmod
|
self.digest_cons = digestmod
|
||||||
else:
|
else:
|
||||||
self.digest_cons = lambda d='': digestmod.new(d)
|
self.digest_cons = lambda d=b'': digestmod.new(d)
|
||||||
|
|
||||||
self.outer = self.digest_cons()
|
self.outer = self.digest_cons()
|
||||||
self.inner = self.digest_cons()
|
self.inner = self.digest_cons()
|
||||||
|
@ -52,7 +59,7 @@ class HMAC:
|
||||||
if len(key) > blocksize:
|
if len(key) > blocksize:
|
||||||
key = self.digest_cons(key).digest()
|
key = self.digest_cons(key).digest()
|
||||||
|
|
||||||
key = key + chr(0) * (blocksize - len(key))
|
key = key + bytes(blocksize - len(key))
|
||||||
self.outer.update(key.translate(trans_5C))
|
self.outer.update(key.translate(trans_5C))
|
||||||
self.inner.update(key.translate(trans_36))
|
self.inner.update(key.translate(trans_36))
|
||||||
if msg is not None:
|
if msg is not None:
|
||||||
|
@ -64,6 +71,10 @@ class HMAC:
|
||||||
def update(self, msg):
|
def update(self, msg):
|
||||||
"""Update this hashing object with the string msg.
|
"""Update this hashing object with the string msg.
|
||||||
"""
|
"""
|
||||||
|
if not isinstance(msg, bytes):
|
||||||
|
if hasattr(msg, "__index__"):
|
||||||
|
raise TypeError("msg can't be a number")
|
||||||
|
msg = bytes(msg)
|
||||||
self.inner.update(msg)
|
self.inner.update(msg)
|
||||||
|
|
||||||
def copy(self):
|
def copy(self):
|
||||||
|
|
|
@ -12,31 +12,31 @@ class TestVectorsTestCase(unittest.TestCase):
|
||||||
h = hmac.HMAC(key, data)
|
h = hmac.HMAC(key, data)
|
||||||
self.assertEqual(h.hexdigest().upper(), digest.upper())
|
self.assertEqual(h.hexdigest().upper(), digest.upper())
|
||||||
|
|
||||||
md5test(chr(0x0b) * 16,
|
md5test(b"\x0b" * 16,
|
||||||
"Hi There",
|
b"Hi There",
|
||||||
"9294727A3638BB1C13F48EF8158BFC9D")
|
"9294727A3638BB1C13F48EF8158BFC9D")
|
||||||
|
|
||||||
md5test("Jefe",
|
md5test(b"Jefe",
|
||||||
"what do ya want for nothing?",
|
b"what do ya want for nothing?",
|
||||||
"750c783e6ab0b503eaa86e310a5db738")
|
"750c783e6ab0b503eaa86e310a5db738")
|
||||||
|
|
||||||
md5test(chr(0xAA)*16,
|
md5test(b"\xaa" * 16,
|
||||||
chr(0xDD)*50,
|
b"\xdd" * 50,
|
||||||
"56be34521d144c88dbb8c733f0e8b3f6")
|
"56be34521d144c88dbb8c733f0e8b3f6")
|
||||||
|
|
||||||
md5test("".join([chr(i) for i in range(1, 26)]),
|
md5test("".join([chr(i) for i in range(1, 26)]),
|
||||||
chr(0xCD) * 50,
|
b"\xcd" * 50,
|
||||||
"697eaf0aca3a3aea3a75164746ffaa79")
|
"697eaf0aca3a3aea3a75164746ffaa79")
|
||||||
|
|
||||||
md5test(chr(0x0C) * 16,
|
md5test(chr(0x0C) * 16,
|
||||||
"Test With Truncation",
|
"Test With Truncation",
|
||||||
"56461ef2342edc00f9bab995690efd4c")
|
"56461ef2342edc00f9bab995690efd4c")
|
||||||
|
|
||||||
md5test(chr(0xAA) * 80,
|
md5test(b"\xaa" * 80,
|
||||||
"Test Using Larger Than Block-Size Key - Hash Key First",
|
"Test Using Larger Than Block-Size Key - Hash Key First",
|
||||||
"6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")
|
"6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")
|
||||||
|
|
||||||
md5test(chr(0xAA) * 80,
|
md5test(b"\xaa" * 80,
|
||||||
("Test Using Larger Than Block-Size Key "
|
("Test Using Larger Than Block-Size Key "
|
||||||
"and Larger Than One Block-Size Data"),
|
"and Larger Than One Block-Size Data"),
|
||||||
"6f630fad67cda0ee1fb1f562db3aa53e")
|
"6f630fad67cda0ee1fb1f562db3aa53e")
|
||||||
|
@ -46,33 +46,33 @@ class TestVectorsTestCase(unittest.TestCase):
|
||||||
h = hmac.HMAC(key, data, digestmod=sha1)
|
h = hmac.HMAC(key, data, digestmod=sha1)
|
||||||
self.assertEqual(h.hexdigest().upper(), digest.upper())
|
self.assertEqual(h.hexdigest().upper(), digest.upper())
|
||||||
|
|
||||||
shatest(chr(0x0b) * 20,
|
shatest(b"\x0b" * 20,
|
||||||
"Hi There",
|
b"Hi There",
|
||||||
"b617318655057264e28bc0b6fb378c8ef146be00")
|
"b617318655057264e28bc0b6fb378c8ef146be00")
|
||||||
|
|
||||||
shatest("Jefe",
|
shatest(b"Jefe",
|
||||||
"what do ya want for nothing?",
|
b"what do ya want for nothing?",
|
||||||
"effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")
|
"effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")
|
||||||
|
|
||||||
shatest(chr(0xAA)*20,
|
shatest(b"\xAA" * 20,
|
||||||
chr(0xDD)*50,
|
b"\xDD" * 50,
|
||||||
"125d7342b9ac11cd91a39af48aa17b4f63f175d3")
|
"125d7342b9ac11cd91a39af48aa17b4f63f175d3")
|
||||||
|
|
||||||
shatest("".join([chr(i) for i in range(1, 26)]),
|
shatest(bytes(range(1, 26)),
|
||||||
chr(0xCD) * 50,
|
b"\xCD" * 50,
|
||||||
"4c9007f4026250c6bc8414f9bf50c86c2d7235da")
|
"4c9007f4026250c6bc8414f9bf50c86c2d7235da")
|
||||||
|
|
||||||
shatest(chr(0x0C) * 20,
|
shatest(chr(0x0C) * 20,
|
||||||
"Test With Truncation",
|
"Test With Truncation",
|
||||||
"4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")
|
"4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")
|
||||||
|
|
||||||
shatest(chr(0xAA) * 80,
|
shatest(b"\xAA" * 80,
|
||||||
"Test Using Larger Than Block-Size Key - Hash Key First",
|
b"Test Using Larger Than Block-Size Key - Hash Key First",
|
||||||
"aa4ae5e15272d00e95705637ce8a3b55ed402112")
|
"aa4ae5e15272d00e95705637ce8a3b55ed402112")
|
||||||
|
|
||||||
shatest(chr(0xAA) * 80,
|
shatest(b"\xAA" * 80,
|
||||||
("Test Using Larger Than Block-Size Key "
|
(b"Test Using Larger Than Block-Size Key "
|
||||||
"and Larger Than One Block-Size Data"),
|
b"and Larger Than One Block-Size Data"),
|
||||||
"e8e99d0f45237d786d6bbaa7965c7808bbff1a91")
|
"e8e99d0f45237d786d6bbaa7965c7808bbff1a91")
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue