test_hmac.py revision 48581c5f08d368942840f99687fce7f10758fa7c
1f166994b8346708a867df823def3ab12e02d43e9Guido van Rossumimport hmac 2e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smithimport hashlib 3f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingimport unittest 4e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smithimport warnings 504f357cffef6d764f2f0ff2671dabde75ec250d1Barry Warsawfrom test import test_support 6f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 7f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingclass TestVectorsTestCase(unittest.TestCase): 87e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum 9893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton def test_md5_vectors(self): 107e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum # Test the HMAC module against test vectors from the RFC. 11f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 12f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def md5test(key, data, digest): 13f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h = hmac.HMAC(key, data) 14893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton self.assertEqual(h.hexdigest().upper(), digest.upper()) 15f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 16f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling md5test(chr(0x0b) * 16, 17f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "Hi There", 18f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "9294727A3638BB1C13F48EF8158BFC9D") 19f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 20f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling md5test("Jefe", 21f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "what do ya want for nothing?", 22f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "750c783e6ab0b503eaa86e310a5db738") 23f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 24f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling md5test(chr(0xAA)*16, 25f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling chr(0xDD)*50, 26f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "56be34521d144c88dbb8c733f0e8b3f6") 27f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 28893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton md5test("".join([chr(i) for i in range(1, 26)]), 29893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton chr(0xCD) * 50, 30893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "697eaf0aca3a3aea3a75164746ffaa79") 31893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton 32893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton md5test(chr(0x0C) * 16, 33893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "Test With Truncation", 34893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "56461ef2342edc00f9bab995690efd4c") 35893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton 36893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton md5test(chr(0xAA) * 80, 37893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "Test Using Larger Than Block-Size Key - Hash Key First", 38893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd") 39893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton 40893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton md5test(chr(0xAA) * 80, 41893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton ("Test Using Larger Than Block-Size Key " 42893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "and Larger Than One Block-Size Data"), 43893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "6f630fad67cda0ee1fb1f562db3aa53e") 44893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton 45893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton def test_sha_vectors(self): 46893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton def shatest(key, data, digest): 47e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith h = hmac.HMAC(key, data, digestmod=hashlib.sha1) 48893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton self.assertEqual(h.hexdigest().upper(), digest.upper()) 49893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton 50893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton shatest(chr(0x0b) * 20, 51893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "Hi There", 52893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "b617318655057264e28bc0b6fb378c8ef146be00") 53893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton 54893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton shatest("Jefe", 55893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "what do ya want for nothing?", 56893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79") 57893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton 58893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton shatest(chr(0xAA)*20, 59893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton chr(0xDD)*50, 60893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "125d7342b9ac11cd91a39af48aa17b4f63f175d3") 61893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton 62893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton shatest("".join([chr(i) for i in range(1, 26)]), 63893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton chr(0xCD) * 50, 64893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "4c9007f4026250c6bc8414f9bf50c86c2d7235da") 65893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton 66893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton shatest(chr(0x0C) * 20, 67893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "Test With Truncation", 68893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04") 69893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton 70893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton shatest(chr(0xAA) * 80, 71893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "Test Using Larger Than Block-Size Key - Hash Key First", 72893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "aa4ae5e15272d00e95705637ce8a3b55ed402112") 73893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton 74893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton shatest(chr(0xAA) * 80, 75893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton ("Test Using Larger Than Block-Size Key " 76893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "and Larger Than One Block-Size Data"), 77893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton "e8e99d0f45237d786d6bbaa7965c7808bbff1a91") 78893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton 79e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith def _rfc4231_test_cases(self, hashfunc): 80e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith def hmactest(key, data, hexdigests): 81e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith h = hmac.HMAC(key, data, digestmod=hashfunc) 82e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc]) 83e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 84e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith # 4.2. Test Case 1 85e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hmactest(key = '\x0b'*20, 86e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith data = 'Hi There', 87e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hexdigests = { 88e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha224: '896fb1128abbdf196832107cd49df33f' 89e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '47b4b1169912ba4f53684b22', 90e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha256: 'b0344c61d8db38535ca8afceaf0bf12b' 91e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '881dc200c9833da726e9376c2e32cff7', 92e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha384: 'afd03944d84895626b0825f4ab46907f' 93e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '15f9dadbe4101ec682aa034c7cebc59c' 94e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'faea9ea9076ede7f4af152e8b2fa9cb6', 95e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha512: '87aa7cdea5ef619d4ff0b4241a1d6cb0' 96e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '2379f4e2ce4ec2787ad0b30545e17cde' 97e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'daa833b7d6b8a702038b274eaea3f4e4' 98e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'be9d914eeb61f1702e696c203a126854', 99e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith }) 100e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 101e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith # 4.3. Test Case 2 102e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hmactest(key = 'Jefe', 103e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith data = 'what do ya want for nothing?', 104e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hexdigests = { 105e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha224: 'a30e01098bc6dbbf45690f3a7e9e6d0f' 106e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '8bbea2a39e6148008fd05e44', 107e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha256: '5bdcc146bf60754e6a042426089575c7' 108e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '5a003f089d2739839dec58b964ec3843', 109e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha384: 'af45d2e376484031617f78d2b58a6b1b' 110e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '9c7ef464f5a01b47e42ec3736322445e' 111e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '8e2240ca5e69e2c78b3239ecfab21649', 112e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha512: '164b7a7bfcf819e2e395fbe73b56e0a3' 113e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '87bd64222e831fd610270cd7ea250554' 114e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '9758bf75c05a994a6d034f65f8f0e6fd' 115e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'caeab1a34d4a6b4b636e070a38bce737', 116e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith }) 117e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 118e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith # 4.4. Test Case 3 119e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hmactest(key = '\xaa'*20, 120e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith data = '\xdd'*50, 121e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hexdigests = { 122e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha224: '7fb3cb3588c6c1f6ffa9694d7d6ad264' 123e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '9365b0c1f65d69d1ec8333ea', 124e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha256: '773ea91e36800e46854db8ebd09181a7' 125e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '2959098b3ef8c122d9635514ced565fe', 126e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha384: '88062608d3e6ad8a0aa2ace014c8a86f' 127e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '0aa635d947ac9febe83ef4e55966144b' 128e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '2a5ab39dc13814b94e3ab6e101a34f27', 129e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha512: 'fa73b0089d56a284efb0f0756c890be9' 130e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'b1b5dbdd8ee81a3655f83e33b2279d39' 131e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'bf3e848279a722c806b485a47e67c807' 132e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'b946a337bee8942674278859e13292fb', 133e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith }) 134e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 135e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith # 4.5. Test Case 4 136e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hmactest(key = ''.join([chr(x) for x in xrange(0x01, 0x19+1)]), 137e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith data = '\xcd'*50, 138e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hexdigests = { 139e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha224: '6c11506874013cac6a2abc1bb382627c' 140e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'ec6a90d86efc012de7afec5a', 141e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha256: '82558a389a443c0ea4cc819899f2083a' 142e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '85f0faa3e578f8077a2e3ff46729665b', 143e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha384: '3e8a69b7783c25851933ab6290af6ca7' 144e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '7a9981480850009cc5577c6e1f573b4e' 145e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '6801dd23c4a7d679ccf8a386c674cffb', 146e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha512: 'b0ba465637458c6990e5a8c5f61d4af7' 147e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'e576d97ff94b872de76f8050361ee3db' 148e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'a91ca5c11aa25eb4d679275cc5788063' 149e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'a5f19741120c4f2de2adebeb10a298dd', 150e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith }) 151e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 152e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith # 4.7. Test Case 6 153e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hmactest(key = '\xaa'*131, 154e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith data = 'Test Using Larger Than Block-Siz' 155e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'e Key - Hash Key First', 156e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hexdigests = { 157e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha224: '95e9a0db962095adaebe9b2d6f0dbce2' 158e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'd499f112f2d2b7273fa6870e', 159e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha256: '60e431591ee0b67f0d8a26aacbf5b77f' 160e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '8e0bc6213728c5140546040f0ee37f54', 161e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha384: '4ece084485813e9088d2c63a041bc5b4' 162e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '4f9ef1012a2b588f3cd11f05033ac4c6' 163e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '0c2ef6ab4030fe8296248df163f44952', 164e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha512: '80b24263c7c1a3ebb71493c1dd7be8b4' 165e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '9b46d1f41b4aeec1121b013783f8f352' 166e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '6b56d037e05f2598bd0fd2215d6a1e52' 167e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '95e64f73f63f0aec8b915a985d786598', 168e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith }) 169e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 170e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith # 4.8. Test Case 7 171e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hmactest(key = '\xaa'*131, 172e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith data = 'This is a test using a larger th' 173e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'an block-size key and a larger t' 174e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'han block-size data. The key nee' 175e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'ds to be hashed before being use' 176e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'd by the HMAC algorithm.', 177e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hexdigests = { 178e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha224: '3a854166ac5d9f023f54d517d0b39dbd' 179e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '946770db9c2b95c9f6f565d1', 180e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha256: '9b09ffa71b942fcb27635fbcd5b0e944' 181e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'bfdc63644f0713938a7f51535c3a35e2', 182e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha384: '6617178e941f020d351e2f254e8fd32c' 183e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '602420feb0b8fb9adccebb82461e99c5' 184e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'a678cc31e799176d3860e6110c46523e', 185e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hashlib.sha512: 'e37b6a775dc87dbaa4dfa9f96e5e3ffd' 186e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'debd71f8867289865df5a32d20cdc944' 187e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 'b6022cac3c4982b10d5eeb55c3e4de15' 188e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith '134676fb6de0446065c97440fa8c6a58', 189e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith }) 190e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 191e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith def test_sha224_rfc4231(self): 192e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith self._rfc4231_test_cases(hashlib.sha224) 193e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 194e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith def test_sha256_rfc4231(self): 195e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith self._rfc4231_test_cases(hashlib.sha256) 196e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 197e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith def test_sha384_rfc4231(self): 198e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith self._rfc4231_test_cases(hashlib.sha384) 199e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 200e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith def test_sha512_rfc4231(self): 201e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith self._rfc4231_test_cases(hashlib.sha512) 202e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 203e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith def test_legacy_block_size_warnings(self): 204e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith class MockCrazyHash(object): 205e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith """Ain't no block_size attribute here.""" 206e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith def __init__(self, *args): 207e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith self._x = hashlib.sha1(*args) 208e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith self.digest_size = self._x.digest_size 209e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith def update(self, v): 210e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith self._x.update(v) 211e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith def digest(self): 212e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith return self._x.digest() 213e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 21448581c5f08d368942840f99687fce7f10758fa7cBrett Cannon with test_support.catch_warning(): 21548581c5f08d368942840f99687fce7f10758fa7cBrett Cannon warnings.simplefilter('error', RuntimeWarning) 216e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith try: 217e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hmac.HMAC('a', 'b', digestmod=MockCrazyHash) 218e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith except RuntimeWarning: 219e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith pass 220e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith else: 221e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith self.fail('Expected warning about missing block_size') 222e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 223e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith MockCrazyHash.block_size = 1 224e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith try: 225e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith hmac.HMAC('a', 'b', digestmod=MockCrazyHash) 226e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith except RuntimeWarning: 227e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith pass 228e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith else: 229e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith self.fail('Expected warning about small block_size') 230e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 231e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith 232893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton 233f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingclass ConstructorTestCase(unittest.TestCase): 2347e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum 235f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def test_normal(self): 2367e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum # Standard constructor call. 237f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling failed = 0 238f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling try: 239f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h = hmac.HMAC("key") 240f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling except: 241f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.fail("Standard constructor call raised exception.") 242f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 243f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def test_withtext(self): 2447e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum # Constructor call with text. 245f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling try: 246f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h = hmac.HMAC("key", "hash this!") 247f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling except: 248f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.fail("Constructor call with text argument raised exception.") 249f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 250f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def test_withmodule(self): 2517e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum # Constructor call with text and digest module. 252f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling try: 253e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith h = hmac.HMAC("key", "", hashlib.sha1) 254f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling except: 255c2aa09ad8055ba3e03da297608552d04b42e9aacBrett Cannon self.fail("Constructor call with hashlib.sha1 raised exception.") 25688768483235beb5de12addee3d73d4e252f6f7cbTim Peters 257f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingclass SanityTestCase(unittest.TestCase): 2587e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum 259f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def test_default_is_md5(self): 2607e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum # Testing if HMAC defaults to MD5 algorithm. 261f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith # NOTE: this whitebox test depends on the hmac class internals 262f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h = hmac.HMAC("key") 263f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith self.failUnless(h.digest_cons == hashlib.md5) 264f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 265f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def test_exercise_all_methods(self): 2667e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum # Exercising all methods once. 267f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling # This must not raise any exceptions 268f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling try: 269f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h = hmac.HMAC("my secret key") 270f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h.update("compute the hash of this text!") 271f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling dig = h.digest() 272f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling dig = h.hexdigest() 273f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h2 = h.copy() 274f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling except: 27528bb572ab4e72abfd918343f1d6114630daa4564Neal Norwitz self.fail("Exception raised during normal usage of HMAC class.") 276f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 277f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingclass CopyTestCase(unittest.TestCase): 2787e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum 279f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def test_attributes(self): 2807e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum # Testing if attributes are of same type. 281f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h1 = hmac.HMAC("key") 282f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h2 = h1.copy() 283f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith self.failUnless(h1.digest_cons == h2.digest_cons, 284f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith "digest constructors don't match.") 285f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.failUnless(type(h1.inner) == type(h2.inner), 286f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "Types of inner don't match.") 287f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.failUnless(type(h1.outer) == type(h2.outer), 288f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "Types of outer don't match.") 289f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 290f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def test_realcopy(self): 2917e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum # Testing if the copy method created a real copy. 292f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h1 = hmac.HMAC("key") 293f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h2 = h1.copy() 294f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling # Using id() in case somebody has overridden __cmp__. 295f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.failUnless(id(h1) != id(h2), "No real copy of the HMAC instance.") 296f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.failUnless(id(h1.inner) != id(h2.inner), 297f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "No real copy of the attribute 'inner'.") 298f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.failUnless(id(h1.outer) != id(h2.outer), 299f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "No real copy of the attribute 'outer'.") 300f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 301f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def test_equality(self): 3027e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum # Testing if the copy has the same digests. 303f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h1 = hmac.HMAC("key") 304f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h1.update("some random text") 305f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h2 = h1.copy() 306f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.failUnless(h1.digest() == h2.digest(), 307f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "Digest of copy doesn't match original digest.") 308f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.failUnless(h1.hexdigest() == h2.hexdigest(), 309f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "Hexdigest of copy doesn't match original hexdigest.") 310f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 311f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingdef test_main(): 31221d3a32b99c5763444c34c189ef653ac9745f3c4Walter Dörwald test_support.run_unittest( 31321d3a32b99c5763444c34c189ef653ac9745f3c4Walter Dörwald TestVectorsTestCase, 31421d3a32b99c5763444c34c189ef653ac9745f3c4Walter Dörwald ConstructorTestCase, 31521d3a32b99c5763444c34c189ef653ac9745f3c4Walter Dörwald SanityTestCase, 31621d3a32b99c5763444c34c189ef653ac9745f3c4Walter Dörwald CopyTestCase 31721d3a32b99c5763444c34c189ef653ac9745f3c4Walter Dörwald ) 318f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 319f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingif __name__ == "__main__": 320f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling test_main() 321