10a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport hmac
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport hashlib
30a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport unittest
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport warnings
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom test import test_support
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestVectorsTestCase(unittest.TestCase):
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_md5_vectors(self):
100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Test the HMAC module against test vectors from the RFC.
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def md5test(key, data, digest):
130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            h = hmac.HMAC(key, data)
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(h.hexdigest().upper(), digest.upper())
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        md5test(chr(0x0b) * 16,
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "Hi There",
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "9294727A3638BB1C13F48EF8158BFC9D")
190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        md5test("Jefe",
210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "what do ya want for nothing?",
220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "750c783e6ab0b503eaa86e310a5db738")
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        md5test(chr(0xAA)*16,
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                chr(0xDD)*50,
260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "56be34521d144c88dbb8c733f0e8b3f6")
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        md5test("".join([chr(i) for i in range(1, 26)]),
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                chr(0xCD) * 50,
300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "697eaf0aca3a3aea3a75164746ffaa79")
310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        md5test(chr(0x0C) * 16,
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "Test With Truncation",
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "56461ef2342edc00f9bab995690efd4c")
350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        md5test(chr(0xAA) * 80,
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "Test Using Larger Than Block-Size Key - Hash Key First",
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        md5test(chr(0xAA) * 80,
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                ("Test Using Larger Than Block-Size Key "
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 "and Larger Than One Block-Size Data"),
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "6f630fad67cda0ee1fb1f562db3aa53e")
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_sha_vectors(self):
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def shatest(key, data, digest):
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            h = hmac.HMAC(key, data, digestmod=hashlib.sha1)
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(h.hexdigest().upper(), digest.upper())
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        shatest(chr(0x0b) * 20,
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "Hi There",
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "b617318655057264e28bc0b6fb378c8ef146be00")
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        shatest("Jefe",
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "what do ya want for nothing?",
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        shatest(chr(0xAA)*20,
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                chr(0xDD)*50,
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "125d7342b9ac11cd91a39af48aa17b4f63f175d3")
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        shatest("".join([chr(i) for i in range(1, 26)]),
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                chr(0xCD) * 50,
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "4c9007f4026250c6bc8414f9bf50c86c2d7235da")
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        shatest(chr(0x0C) * 20,
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "Test With Truncation",
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        shatest(chr(0xAA) * 80,
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "Test Using Larger Than Block-Size Key - Hash Key First",
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "aa4ae5e15272d00e95705637ce8a3b55ed402112")
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        shatest(chr(0xAA) * 80,
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                ("Test Using Larger Than Block-Size Key "
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 "and Larger Than One Block-Size Data"),
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "e8e99d0f45237d786d6bbaa7965c7808bbff1a91")
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _rfc4231_test_cases(self, hashfunc):
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def hmactest(key, data, hexdigests):
810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            h = hmac.HMAC(key, data, digestmod=hashfunc)
820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc])
830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # 4.2.  Test Case 1
850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        hmactest(key = '\x0b'*20,
860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 data = 'Hi There',
870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 hexdigests = {
880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha224: '896fb1128abbdf196832107cd49df33f'
890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '47b4b1169912ba4f53684b22',
900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha256: 'b0344c61d8db38535ca8afceaf0bf12b'
910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '881dc200c9833da726e9376c2e32cff7',
920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha384: 'afd03944d84895626b0825f4ab46907f'
930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '15f9dadbe4101ec682aa034c7cebc59c'
940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   'faea9ea9076ede7f4af152e8b2fa9cb6',
950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha512: '87aa7cdea5ef619d4ff0b4241a1d6cb0'
960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '2379f4e2ce4ec2787ad0b30545e17cde'
970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   'daa833b7d6b8a702038b274eaea3f4e4'
980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   'be9d914eeb61f1702e696c203a126854',
990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 })
1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # 4.3.  Test Case 2
1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        hmactest(key = 'Jefe',
1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 data = 'what do ya want for nothing?',
1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 hexdigests = {
1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha224: 'a30e01098bc6dbbf45690f3a7e9e6d0f'
1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '8bbea2a39e6148008fd05e44',
1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha256: '5bdcc146bf60754e6a042426089575c7'
1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '5a003f089d2739839dec58b964ec3843',
1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha384: 'af45d2e376484031617f78d2b58a6b1b'
1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '9c7ef464f5a01b47e42ec3736322445e'
1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '8e2240ca5e69e2c78b3239ecfab21649',
1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha512: '164b7a7bfcf819e2e395fbe73b56e0a3'
1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '87bd64222e831fd610270cd7ea250554'
1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '9758bf75c05a994a6d034f65f8f0e6fd'
1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   'caeab1a34d4a6b4b636e070a38bce737',
1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 })
1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # 4.4.  Test Case 3
1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        hmactest(key = '\xaa'*20,
1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 data = '\xdd'*50,
1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 hexdigests = {
1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha224: '7fb3cb3588c6c1f6ffa9694d7d6ad264'
1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '9365b0c1f65d69d1ec8333ea',
1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha256: '773ea91e36800e46854db8ebd09181a7'
1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '2959098b3ef8c122d9635514ced565fe',
1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha384: '88062608d3e6ad8a0aa2ace014c8a86f'
1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '0aa635d947ac9febe83ef4e55966144b'
1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '2a5ab39dc13814b94e3ab6e101a34f27',
1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha512: 'fa73b0089d56a284efb0f0756c890be9'
1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   'b1b5dbdd8ee81a3655f83e33b2279d39'
1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   'bf3e848279a722c806b485a47e67c807'
1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   'b946a337bee8942674278859e13292fb',
1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 })
1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # 4.5.  Test Case 4
1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        hmactest(key = ''.join([chr(x) for x in xrange(0x01, 0x19+1)]),
1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 data = '\xcd'*50,
1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 hexdigests = {
1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha224: '6c11506874013cac6a2abc1bb382627c'
1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   'ec6a90d86efc012de7afec5a',
1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha256: '82558a389a443c0ea4cc819899f2083a'
1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '85f0faa3e578f8077a2e3ff46729665b',
1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha384: '3e8a69b7783c25851933ab6290af6ca7'
1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '7a9981480850009cc5577c6e1f573b4e'
1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '6801dd23c4a7d679ccf8a386c674cffb',
1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha512: 'b0ba465637458c6990e5a8c5f61d4af7'
1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   'e576d97ff94b872de76f8050361ee3db'
1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   'a91ca5c11aa25eb4d679275cc5788063'
1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   'a5f19741120c4f2de2adebeb10a298dd',
1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 })
1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # 4.7.  Test Case 6
1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        hmactest(key = '\xaa'*131,
1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 data = 'Test Using Larger Than Block-Siz'
1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        'e Key - Hash Key First',
1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 hexdigests = {
1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha224: '95e9a0db962095adaebe9b2d6f0dbce2'
1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   'd499f112f2d2b7273fa6870e',
1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha256: '60e431591ee0b67f0d8a26aacbf5b77f'
1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '8e0bc6213728c5140546040f0ee37f54',
1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha384: '4ece084485813e9088d2c63a041bc5b4'
1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '4f9ef1012a2b588f3cd11f05033ac4c6'
1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '0c2ef6ab4030fe8296248df163f44952',
1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha512: '80b24263c7c1a3ebb71493c1dd7be8b4'
1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '9b46d1f41b4aeec1121b013783f8f352'
1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '6b56d037e05f2598bd0fd2215d6a1e52'
1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '95e64f73f63f0aec8b915a985d786598',
1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 })
1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # 4.8.  Test Case 7
1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        hmactest(key = '\xaa'*131,
1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 data = 'This is a test using a larger th'
1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        'an block-size key and a larger t'
1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        'han block-size data. The key nee'
1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        'ds to be hashed before being use'
1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        'd by the HMAC algorithm.',
1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 hexdigests = {
1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha224: '3a854166ac5d9f023f54d517d0b39dbd'
1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '946770db9c2b95c9f6f565d1',
1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha256: '9b09ffa71b942fcb27635fbcd5b0e944'
1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   'bfdc63644f0713938a7f51535c3a35e2',
1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha384: '6617178e941f020d351e2f254e8fd32c'
1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '602420feb0b8fb9adccebb82461e99c5'
1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   'a678cc31e799176d3860e6110c46523e',
1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   hashlib.sha512: 'e37b6a775dc87dbaa4dfa9f96e5e3ffd'
1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   'debd71f8867289865df5a32d20cdc944'
1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   'b6022cac3c4982b10d5eeb55c3e4de15'
1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   '134676fb6de0446065c97440fa8c6a58',
1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 })
1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_sha224_rfc4231(self):
1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._rfc4231_test_cases(hashlib.sha224)
1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_sha256_rfc4231(self):
1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._rfc4231_test_cases(hashlib.sha256)
1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_sha384_rfc4231(self):
1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._rfc4231_test_cases(hashlib.sha384)
1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_sha512_rfc4231(self):
2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._rfc4231_test_cases(hashlib.sha512)
2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_legacy_block_size_warnings(self):
2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class MockCrazyHash(object):
2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            """Ain't no block_size attribute here."""
2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __init__(self, *args):
2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._x = hashlib.sha1(*args)
2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.digest_size = self._x.digest_size
2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def update(self, v):
2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._x.update(v)
2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def digest(self):
2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return self._x.digest()
2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with warnings.catch_warnings():
2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            warnings.simplefilter('error', RuntimeWarning)
2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with self.assertRaises(RuntimeWarning):
2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                hmac.HMAC('a', 'b', digestmod=MockCrazyHash)
2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.fail('Expected warning about missing block_size')
2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            MockCrazyHash.block_size = 1
2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with self.assertRaises(RuntimeWarning):
2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                hmac.HMAC('a', 'b', digestmod=MockCrazyHash)
2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.fail('Expected warning about small block_size')
2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass ConstructorTestCase(unittest.TestCase):
2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_normal(self):
2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Standard constructor call.
2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        failed = 0
2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            h = hmac.HMAC("key")
2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except:
2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("Standard constructor call raised exception.")
2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_withtext(self):
2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Constructor call with text.
2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            h = hmac.HMAC("key", "hash this!")
2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except:
2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("Constructor call with text argument raised exception.")
2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_withmodule(self):
2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Constructor call with text and digest module.
2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            h = hmac.HMAC("key", "", hashlib.sha1)
2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except:
2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("Constructor call with hashlib.sha1 raised exception.")
2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass SanityTestCase(unittest.TestCase):
2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_default_is_md5(self):
2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Testing if HMAC defaults to MD5 algorithm.
2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # NOTE: this whitebox test depends on the hmac class internals
2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        h = hmac.HMAC("key")
2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(h.digest_cons == hashlib.md5)
2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_exercise_all_methods(self):
2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Exercising all methods once.
2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # This must not raise any exceptions
2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            h = hmac.HMAC("my secret key")
2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            h.update("compute the hash of this text!")
2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            dig = h.digest()
2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            dig = h.hexdigest()
2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            h2 = h.copy()
2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except:
2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("Exception raised during normal usage of HMAC class.")
2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass CopyTestCase(unittest.TestCase):
2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_attributes(self):
2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Testing if attributes are of same type.
2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        h1 = hmac.HMAC("key")
2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        h2 = h1.copy()
2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(h1.digest_cons == h2.digest_cons,
2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "digest constructors don't match.")
2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(type(h1.inner) == type(h2.inner),
2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "Types of inner don't match.")
2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(type(h1.outer) == type(h2.outer),
2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "Types of outer don't match.")
2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_realcopy(self):
2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Testing if the copy method created a real copy.
2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        h1 = hmac.HMAC("key")
2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        h2 = h1.copy()
2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Using id() in case somebody has overridden __cmp__.
2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(id(h1) != id(h2), "No real copy of the HMAC instance.")
2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(id(h1.inner) != id(h2.inner),
2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "No real copy of the attribute 'inner'.")
2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(id(h1.outer) != id(h2.outer),
2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "No real copy of the attribute 'outer'.")
2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_equality(self):
2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Testing if the copy has the same digests.
2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        h1 = hmac.HMAC("key")
2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        h1.update("some random text")
2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        h2 = h1.copy()
3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(h1.digest() == h2.digest(),
3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "Digest of copy doesn't match original digest.")
3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(h1.hexdigest() == h2.hexdigest(),
3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "Hexdigest of copy doesn't match original hexdigest.")
3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_main():
3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    test_support.run_unittest(
3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        TestVectorsTestCase,
3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ConstructorTestCase,
3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        SanityTestCase,
3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        CopyTestCase
3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    )
3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == "__main__":
3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    test_main()
315