| 1 | import hmac
|
|---|
| 2 | import sha
|
|---|
| 3 | import unittest
|
|---|
| 4 | from test import test_support
|
|---|
| 5 |
|
|---|
| 6 | class TestVectorsTestCase(unittest.TestCase):
|
|---|
| 7 |
|
|---|
| 8 | def test_md5_vectors(self):
|
|---|
| 9 | # Test the HMAC module against test vectors from the RFC.
|
|---|
| 10 |
|
|---|
| 11 | def md5test(key, data, digest):
|
|---|
| 12 | h = hmac.HMAC(key, data)
|
|---|
| 13 | self.assertEqual(h.hexdigest().upper(), digest.upper())
|
|---|
| 14 |
|
|---|
| 15 | md5test(chr(0x0b) * 16,
|
|---|
| 16 | "Hi There",
|
|---|
| 17 | "9294727A3638BB1C13F48EF8158BFC9D")
|
|---|
| 18 |
|
|---|
| 19 | md5test("Jefe",
|
|---|
| 20 | "what do ya want for nothing?",
|
|---|
| 21 | "750c783e6ab0b503eaa86e310a5db738")
|
|---|
| 22 |
|
|---|
| 23 | md5test(chr(0xAA)*16,
|
|---|
| 24 | chr(0xDD)*50,
|
|---|
| 25 | "56be34521d144c88dbb8c733f0e8b3f6")
|
|---|
| 26 |
|
|---|
| 27 | md5test("".join([chr(i) for i in range(1, 26)]),
|
|---|
| 28 | chr(0xCD) * 50,
|
|---|
| 29 | "697eaf0aca3a3aea3a75164746ffaa79")
|
|---|
| 30 |
|
|---|
| 31 | md5test(chr(0x0C) * 16,
|
|---|
| 32 | "Test With Truncation",
|
|---|
| 33 | "56461ef2342edc00f9bab995690efd4c")
|
|---|
| 34 |
|
|---|
| 35 | md5test(chr(0xAA) * 80,
|
|---|
| 36 | "Test Using Larger Than Block-Size Key - Hash Key First",
|
|---|
| 37 | "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")
|
|---|
| 38 |
|
|---|
| 39 | md5test(chr(0xAA) * 80,
|
|---|
| 40 | ("Test Using Larger Than Block-Size Key "
|
|---|
| 41 | "and Larger Than One Block-Size Data"),
|
|---|
| 42 | "6f630fad67cda0ee1fb1f562db3aa53e")
|
|---|
| 43 |
|
|---|
| 44 | def test_sha_vectors(self):
|
|---|
| 45 | def shatest(key, data, digest):
|
|---|
| 46 | h = hmac.HMAC(key, data, digestmod=sha)
|
|---|
| 47 | self.assertEqual(h.hexdigest().upper(), digest.upper())
|
|---|
| 48 |
|
|---|
| 49 | shatest(chr(0x0b) * 20,
|
|---|
| 50 | "Hi There",
|
|---|
| 51 | "b617318655057264e28bc0b6fb378c8ef146be00")
|
|---|
| 52 |
|
|---|
| 53 | shatest("Jefe",
|
|---|
| 54 | "what do ya want for nothing?",
|
|---|
| 55 | "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")
|
|---|
| 56 |
|
|---|
| 57 | shatest(chr(0xAA)*20,
|
|---|
| 58 | chr(0xDD)*50,
|
|---|
| 59 | "125d7342b9ac11cd91a39af48aa17b4f63f175d3")
|
|---|
| 60 |
|
|---|
| 61 | shatest("".join([chr(i) for i in range(1, 26)]),
|
|---|
| 62 | chr(0xCD) * 50,
|
|---|
| 63 | "4c9007f4026250c6bc8414f9bf50c86c2d7235da")
|
|---|
| 64 |
|
|---|
| 65 | shatest(chr(0x0C) * 20,
|
|---|
| 66 | "Test With Truncation",
|
|---|
| 67 | "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")
|
|---|
| 68 |
|
|---|
| 69 | shatest(chr(0xAA) * 80,
|
|---|
| 70 | "Test Using Larger Than Block-Size Key - Hash Key First",
|
|---|
| 71 | "aa4ae5e15272d00e95705637ce8a3b55ed402112")
|
|---|
| 72 |
|
|---|
| 73 | shatest(chr(0xAA) * 80,
|
|---|
| 74 | ("Test Using Larger Than Block-Size Key "
|
|---|
| 75 | "and Larger Than One Block-Size Data"),
|
|---|
| 76 | "e8e99d0f45237d786d6bbaa7965c7808bbff1a91")
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 | class ConstructorTestCase(unittest.TestCase):
|
|---|
| 80 |
|
|---|
| 81 | def test_normal(self):
|
|---|
| 82 | # Standard constructor call.
|
|---|
| 83 | failed = 0
|
|---|
| 84 | try:
|
|---|
| 85 | h = hmac.HMAC("key")
|
|---|
| 86 | except:
|
|---|
| 87 | self.fail("Standard constructor call raised exception.")
|
|---|
| 88 |
|
|---|
| 89 | def test_withtext(self):
|
|---|
| 90 | # Constructor call with text.
|
|---|
| 91 | try:
|
|---|
| 92 | h = hmac.HMAC("key", "hash this!")
|
|---|
| 93 | except:
|
|---|
| 94 | self.fail("Constructor call with text argument raised exception.")
|
|---|
| 95 |
|
|---|
| 96 | def test_withmodule(self):
|
|---|
| 97 | # Constructor call with text and digest module.
|
|---|
| 98 | import sha
|
|---|
| 99 | try:
|
|---|
| 100 | h = hmac.HMAC("key", "", sha)
|
|---|
| 101 | except:
|
|---|
| 102 | self.fail("Constructor call with sha module raised exception.")
|
|---|
| 103 |
|
|---|
| 104 | class SanityTestCase(unittest.TestCase):
|
|---|
| 105 |
|
|---|
| 106 | def test_default_is_md5(self):
|
|---|
| 107 | # Testing if HMAC defaults to MD5 algorithm.
|
|---|
| 108 | # NOTE: this whitebox test depends on the hmac class internals
|
|---|
| 109 | import hashlib
|
|---|
| 110 | h = hmac.HMAC("key")
|
|---|
| 111 | self.failUnless(h.digest_cons == hashlib.md5)
|
|---|
| 112 |
|
|---|
| 113 | def test_exercise_all_methods(self):
|
|---|
| 114 | # Exercising all methods once.
|
|---|
| 115 | # This must not raise any exceptions
|
|---|
| 116 | try:
|
|---|
| 117 | h = hmac.HMAC("my secret key")
|
|---|
| 118 | h.update("compute the hash of this text!")
|
|---|
| 119 | dig = h.digest()
|
|---|
| 120 | dig = h.hexdigest()
|
|---|
| 121 | h2 = h.copy()
|
|---|
| 122 | except:
|
|---|
| 123 | self.fail("Exception raised during normal usage of HMAC class.")
|
|---|
| 124 |
|
|---|
| 125 | class CopyTestCase(unittest.TestCase):
|
|---|
| 126 |
|
|---|
| 127 | def test_attributes(self):
|
|---|
| 128 | # Testing if attributes are of same type.
|
|---|
| 129 | h1 = hmac.HMAC("key")
|
|---|
| 130 | h2 = h1.copy()
|
|---|
| 131 | self.failUnless(h1.digest_cons == h2.digest_cons,
|
|---|
| 132 | "digest constructors don't match.")
|
|---|
| 133 | self.failUnless(type(h1.inner) == type(h2.inner),
|
|---|
| 134 | "Types of inner don't match.")
|
|---|
| 135 | self.failUnless(type(h1.outer) == type(h2.outer),
|
|---|
| 136 | "Types of outer don't match.")
|
|---|
| 137 |
|
|---|
| 138 | def test_realcopy(self):
|
|---|
| 139 | # Testing if the copy method created a real copy.
|
|---|
| 140 | h1 = hmac.HMAC("key")
|
|---|
| 141 | h2 = h1.copy()
|
|---|
| 142 | # Using id() in case somebody has overridden __cmp__.
|
|---|
| 143 | self.failUnless(id(h1) != id(h2), "No real copy of the HMAC instance.")
|
|---|
| 144 | self.failUnless(id(h1.inner) != id(h2.inner),
|
|---|
| 145 | "No real copy of the attribute 'inner'.")
|
|---|
| 146 | self.failUnless(id(h1.outer) != id(h2.outer),
|
|---|
| 147 | "No real copy of the attribute 'outer'.")
|
|---|
| 148 |
|
|---|
| 149 | def test_equality(self):
|
|---|
| 150 | # Testing if the copy has the same digests.
|
|---|
| 151 | h1 = hmac.HMAC("key")
|
|---|
| 152 | h1.update("some random text")
|
|---|
| 153 | h2 = h1.copy()
|
|---|
| 154 | self.failUnless(h1.digest() == h2.digest(),
|
|---|
| 155 | "Digest of copy doesn't match original digest.")
|
|---|
| 156 | self.failUnless(h1.hexdigest() == h2.hexdigest(),
|
|---|
| 157 | "Hexdigest of copy doesn't match original hexdigest.")
|
|---|
| 158 |
|
|---|
| 159 | def test_main():
|
|---|
| 160 | test_support.run_unittest(
|
|---|
| 161 | TestVectorsTestCase,
|
|---|
| 162 | ConstructorTestCase,
|
|---|
| 163 | SanityTestCase,
|
|---|
| 164 | CopyTestCase
|
|---|
| 165 | )
|
|---|
| 166 |
|
|---|
| 167 | if __name__ == "__main__":
|
|---|
| 168 | test_main()
|
|---|