| 
									
										
										
										
											2001-09-11 15:54:16 +00:00
										 |  |  | import hmac | 
					
						
							| 
									
										
										
										
											2007-05-31 19:20:00 +00:00
										 |  |  | from hashlib import sha1 | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2002-07-23 19:04:11 +00:00
										 |  |  | from test import test_support | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class TestVectorsTestCase(unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2002-08-22 19:38:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-27 16:16:41 +00:00
										 |  |  |     def test_md5_vectors(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 19:38:14 +00:00
										 |  |  |         # Test the HMAC module against test vectors from the RFC. | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def md5test(key, data, digest): | 
					
						
							|  |  |  |             h = hmac.HMAC(key, data) | 
					
						
							| 
									
										
										
										
											2003-05-27 16:16:41 +00:00
										 |  |  |             self.assertEqual(h.hexdigest().upper(), digest.upper()) | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         md5test(chr(0x0b) * 16, | 
					
						
							|  |  |  |                 "Hi There", | 
					
						
							|  |  |  |                 "9294727A3638BB1C13F48EF8158BFC9D") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         md5test("Jefe", | 
					
						
							|  |  |  |                 "what do ya want for nothing?", | 
					
						
							|  |  |  |                 "750c783e6ab0b503eaa86e310a5db738") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         md5test(chr(0xAA)*16, | 
					
						
							|  |  |  |                 chr(0xDD)*50, | 
					
						
							|  |  |  |                 "56be34521d144c88dbb8c733f0e8b3f6") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-27 16:16:41 +00:00
										 |  |  |         md5test("".join([chr(i) for i in range(1, 26)]), | 
					
						
							|  |  |  |                 chr(0xCD) * 50, | 
					
						
							|  |  |  |                 "697eaf0aca3a3aea3a75164746ffaa79") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         md5test(chr(0x0C) * 16, | 
					
						
							|  |  |  |                 "Test With Truncation", | 
					
						
							|  |  |  |                 "56461ef2342edc00f9bab995690efd4c") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         md5test(chr(0xAA) * 80, | 
					
						
							|  |  |  |                 "Test Using Larger Than Block-Size Key - Hash Key First", | 
					
						
							|  |  |  |                 "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         md5test(chr(0xAA) * 80, | 
					
						
							|  |  |  |                 ("Test Using Larger Than Block-Size Key " | 
					
						
							|  |  |  |                  "and Larger Than One Block-Size Data"), | 
					
						
							|  |  |  |                 "6f630fad67cda0ee1fb1f562db3aa53e") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_sha_vectors(self): | 
					
						
							|  |  |  |         def shatest(key, data, digest): | 
					
						
							| 
									
										
										
										
											2007-05-31 19:20:00 +00:00
										 |  |  |             h = hmac.HMAC(key, data, digestmod=sha1) | 
					
						
							| 
									
										
										
										
											2003-05-27 16:16:41 +00:00
										 |  |  |             self.assertEqual(h.hexdigest().upper(), digest.upper()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         shatest(chr(0x0b) * 20, | 
					
						
							|  |  |  |                 "Hi There", | 
					
						
							|  |  |  |                 "b617318655057264e28bc0b6fb378c8ef146be00") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         shatest("Jefe", | 
					
						
							|  |  |  |                 "what do ya want for nothing?", | 
					
						
							|  |  |  |                 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         shatest(chr(0xAA)*20, | 
					
						
							|  |  |  |                 chr(0xDD)*50, | 
					
						
							|  |  |  |                 "125d7342b9ac11cd91a39af48aa17b4f63f175d3") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         shatest("".join([chr(i) for i in range(1, 26)]), | 
					
						
							|  |  |  |                 chr(0xCD) * 50, | 
					
						
							|  |  |  |                 "4c9007f4026250c6bc8414f9bf50c86c2d7235da") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         shatest(chr(0x0C) * 20, | 
					
						
							|  |  |  |                 "Test With Truncation", | 
					
						
							|  |  |  |                 "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         shatest(chr(0xAA) * 80, | 
					
						
							|  |  |  |                 "Test Using Larger Than Block-Size Key - Hash Key First", | 
					
						
							|  |  |  |                 "aa4ae5e15272d00e95705637ce8a3b55ed402112") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         shatest(chr(0xAA) * 80, | 
					
						
							|  |  |  |                 ("Test Using Larger Than Block-Size Key " | 
					
						
							|  |  |  |                  "and Larger Than One Block-Size Data"), | 
					
						
							|  |  |  |                 "e8e99d0f45237d786d6bbaa7965c7808bbff1a91") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  | class ConstructorTestCase(unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2002-08-22 19:38:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  |     def test_normal(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 19:38:14 +00:00
										 |  |  |         # Standard constructor call. | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  |         failed = 0 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             h = hmac.HMAC("key") | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             self.fail("Standard constructor call raised exception.") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_withtext(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 19:38:14 +00:00
										 |  |  |         # Constructor call with text. | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  |         try: | 
					
						
							|  |  |  |             h = hmac.HMAC("key", "hash this!") | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             self.fail("Constructor call with text argument raised exception.") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_withmodule(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 19:38:14 +00:00
										 |  |  |         # Constructor call with text and digest module. | 
					
						
							| 
									
										
										
										
											2007-05-31 19:20:00 +00:00
										 |  |  |         from hashlib import sha1 | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2007-05-31 19:20:00 +00:00
										 |  |  |             h = hmac.HMAC("key", "", sha1) | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  |         except: | 
					
						
							| 
									
										
										
										
											2007-05-31 19:20:00 +00:00
										 |  |  |             self.fail("Constructor call with hashlib.sha1 raised exception.") | 
					
						
							| 
									
										
										
										
											2001-11-13 21:51:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  | class SanityTestCase(unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2002-08-22 19:38:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  |     def test_default_is_md5(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 19:38:14 +00:00
										 |  |  |         # Testing if HMAC defaults to MD5 algorithm. | 
					
						
							| 
									
										
										
										
											2005-08-21 18:45:59 +00:00
										 |  |  |         # NOTE: this whitebox test depends on the hmac class internals | 
					
						
							|  |  |  |         import hashlib | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  |         h = hmac.HMAC("key") | 
					
						
							| 
									
										
										
										
											2005-08-21 18:45:59 +00:00
										 |  |  |         self.failUnless(h.digest_cons == hashlib.md5) | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_exercise_all_methods(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 19:38:14 +00:00
										 |  |  |         # Exercising all methods once. | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  |         # This must not raise any exceptions | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             h = hmac.HMAC("my secret key") | 
					
						
							|  |  |  |             h.update("compute the hash of this text!") | 
					
						
							|  |  |  |             dig = h.digest() | 
					
						
							|  |  |  |             dig = h.hexdigest() | 
					
						
							|  |  |  |             h2 = h.copy() | 
					
						
							|  |  |  |         except: | 
					
						
							| 
									
										
										
										
											2002-04-01 19:00:50 +00:00
										 |  |  |             self.fail("Exception raised during normal usage of HMAC class.") | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class CopyTestCase(unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2002-08-22 19:38:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  |     def test_attributes(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 19:38:14 +00:00
										 |  |  |         # Testing if attributes are of same type. | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  |         h1 = hmac.HMAC("key") | 
					
						
							|  |  |  |         h2 = h1.copy() | 
					
						
							| 
									
										
										
										
											2005-08-21 18:45:59 +00:00
										 |  |  |         self.failUnless(h1.digest_cons == h2.digest_cons, | 
					
						
							|  |  |  |             "digest constructors don't match.") | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  |         self.failUnless(type(h1.inner) == type(h2.inner), | 
					
						
							|  |  |  |             "Types of inner don't match.") | 
					
						
							|  |  |  |         self.failUnless(type(h1.outer) == type(h2.outer), | 
					
						
							|  |  |  |             "Types of outer don't match.") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_realcopy(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 19:38:14 +00:00
										 |  |  |         # Testing if the copy method created a real copy. | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  |         h1 = hmac.HMAC("key") | 
					
						
							|  |  |  |         h2 = h1.copy() | 
					
						
							|  |  |  |         # Using id() in case somebody has overridden __cmp__. | 
					
						
							|  |  |  |         self.failUnless(id(h1) != id(h2), "No real copy of the HMAC instance.") | 
					
						
							|  |  |  |         self.failUnless(id(h1.inner) != id(h2.inner), | 
					
						
							|  |  |  |             "No real copy of the attribute 'inner'.") | 
					
						
							|  |  |  |         self.failUnless(id(h1.outer) != id(h2.outer), | 
					
						
							|  |  |  |             "No real copy of the attribute 'outer'.") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_equality(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 19:38:14 +00:00
										 |  |  |         # Testing if the copy has the same digests. | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  |         h1 = hmac.HMAC("key") | 
					
						
							|  |  |  |         h1.update("some random text") | 
					
						
							|  |  |  |         h2 = h1.copy() | 
					
						
							|  |  |  |         self.failUnless(h1.digest() == h2.digest(), | 
					
						
							|  |  |  |             "Digest of copy doesn't match original digest.") | 
					
						
							|  |  |  |         self.failUnless(h1.hexdigest() == h2.hexdigest(), | 
					
						
							|  |  |  |             "Hexdigest of copy doesn't match original hexdigest.") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_main(): | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |     test_support.run_unittest( | 
					
						
							|  |  |  |         TestVectorsTestCase, | 
					
						
							|  |  |  |         ConstructorTestCase, | 
					
						
							|  |  |  |         SanityTestCase, | 
					
						
							|  |  |  |         CopyTestCase | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2001-11-02 21:49:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     test_main() |