home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 July / maximum-cd-2011-07.iso / DiscContents / LibO_3.3.2_Win_x86_install_multi.exe / libreoffice1.cab / test_hmac.py < prev    next >
Encoding:
Python Source  |  2011-03-15  |  12.8 KB  |  321 lines

  1. import hmac
  2. import hashlib
  3. import unittest
  4. import warnings
  5. from test import test_support
  6.  
  7. class TestVectorsTestCase(unittest.TestCase):
  8.  
  9.     def test_md5_vectors(self):
  10.         # Test the HMAC module against test vectors from the RFC.
  11.  
  12.         def md5test(key, data, digest):
  13.             h = hmac.HMAC(key, data)
  14.             self.assertEqual(h.hexdigest().upper(), digest.upper())
  15.  
  16.         md5test(chr(0x0b) * 16,
  17.                 "Hi There",
  18.                 "9294727A3638BB1C13F48EF8158BFC9D")
  19.  
  20.         md5test("Jefe",
  21.                 "what do ya want for nothing?",
  22.                 "750c783e6ab0b503eaa86e310a5db738")
  23.  
  24.         md5test(chr(0xAA)*16,
  25.                 chr(0xDD)*50,
  26.                 "56be34521d144c88dbb8c733f0e8b3f6")
  27.  
  28.         md5test("".join([chr(i) for i in range(1, 26)]),
  29.                 chr(0xCD) * 50,
  30.                 "697eaf0aca3a3aea3a75164746ffaa79")
  31.  
  32.         md5test(chr(0x0C) * 16,
  33.                 "Test With Truncation",
  34.                 "56461ef2342edc00f9bab995690efd4c")
  35.  
  36.         md5test(chr(0xAA) * 80,
  37.                 "Test Using Larger Than Block-Size Key - Hash Key First",
  38.                 "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")
  39.  
  40.         md5test(chr(0xAA) * 80,
  41.                 ("Test Using Larger Than Block-Size Key "
  42.                  "and Larger Than One Block-Size Data"),
  43.                 "6f630fad67cda0ee1fb1f562db3aa53e")
  44.  
  45.     def test_sha_vectors(self):
  46.         def shatest(key, data, digest):
  47.             h = hmac.HMAC(key, data, digestmod=hashlib.sha1)
  48.             self.assertEqual(h.hexdigest().upper(), digest.upper())
  49.  
  50.         shatest(chr(0x0b) * 20,
  51.                 "Hi There",
  52.                 "b617318655057264e28bc0b6fb378c8ef146be00")
  53.  
  54.         shatest("Jefe",
  55.                 "what do ya want for nothing?",
  56.                 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")
  57.  
  58.         shatest(chr(0xAA)*20,
  59.                 chr(0xDD)*50,
  60.                 "125d7342b9ac11cd91a39af48aa17b4f63f175d3")
  61.  
  62.         shatest("".join([chr(i) for i in range(1, 26)]),
  63.                 chr(0xCD) * 50,
  64.                 "4c9007f4026250c6bc8414f9bf50c86c2d7235da")
  65.  
  66.         shatest(chr(0x0C) * 20,
  67.                 "Test With Truncation",
  68.                 "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")
  69.  
  70.         shatest(chr(0xAA) * 80,
  71.                 "Test Using Larger Than Block-Size Key - Hash Key First",
  72.                 "aa4ae5e15272d00e95705637ce8a3b55ed402112")
  73.  
  74.         shatest(chr(0xAA) * 80,
  75.                 ("Test Using Larger Than Block-Size Key "
  76.                  "and Larger Than One Block-Size Data"),
  77.                 "e8e99d0f45237d786d6bbaa7965c7808bbff1a91")
  78.  
  79.     def _rfc4231_test_cases(self, hashfunc):
  80.         def hmactest(key, data, hexdigests):
  81.             h = hmac.HMAC(key, data, digestmod=hashfunc)
  82.             self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc])
  83.  
  84.         # 4.2.  Test Case 1
  85.         hmactest(key = '\x0b'*20,
  86.                  data = 'Hi There',
  87.                  hexdigests = {
  88.                    hashlib.sha224: '896fb1128abbdf196832107cd49df33f'
  89.                                    '47b4b1169912ba4f53684b22',
  90.                    hashlib.sha256: 'b0344c61d8db38535ca8afceaf0bf12b'
  91.                                    '881dc200c9833da726e9376c2e32cff7',
  92.                    hashlib.sha384: 'afd03944d84895626b0825f4ab46907f'
  93.                                    '15f9dadbe4101ec682aa034c7cebc59c'
  94.                                    'faea9ea9076ede7f4af152e8b2fa9cb6',
  95.                    hashlib.sha512: '87aa7cdea5ef619d4ff0b4241a1d6cb0'
  96.                                    '2379f4e2ce4ec2787ad0b30545e17cde'
  97.                                    'daa833b7d6b8a702038b274eaea3f4e4'
  98.                                    'be9d914eeb61f1702e696c203a126854',
  99.                  })
  100.  
  101.         # 4.3.  Test Case 2
  102.         hmactest(key = 'Jefe',
  103.                  data = 'what do ya want for nothing?',
  104.                  hexdigests = {
  105.                    hashlib.sha224: 'a30e01098bc6dbbf45690f3a7e9e6d0f'
  106.                                    '8bbea2a39e6148008fd05e44',
  107.                    hashlib.sha256: '5bdcc146bf60754e6a042426089575c7'
  108.                                    '5a003f089d2739839dec58b964ec3843',
  109.                    hashlib.sha384: 'af45d2e376484031617f78d2b58a6b1b'
  110.                                    '9c7ef464f5a01b47e42ec3736322445e'
  111.                                    '8e2240ca5e69e2c78b3239ecfab21649',
  112.                    hashlib.sha512: '164b7a7bfcf819e2e395fbe73b56e0a3'
  113.                                    '87bd64222e831fd610270cd7ea250554'
  114.                                    '9758bf75c05a994a6d034f65f8f0e6fd'
  115.                                    'caeab1a34d4a6b4b636e070a38bce737',
  116.                  })
  117.  
  118.         # 4.4.  Test Case 3
  119.         hmactest(key = '\xaa'*20,
  120.                  data = '\xdd'*50,
  121.                  hexdigests = {
  122.                    hashlib.sha224: '7fb3cb3588c6c1f6ffa9694d7d6ad264'
  123.                                    '9365b0c1f65d69d1ec8333ea',
  124.                    hashlib.sha256: '773ea91e36800e46854db8ebd09181a7'
  125.                                    '2959098b3ef8c122d9635514ced565fe',
  126.                    hashlib.sha384: '88062608d3e6ad8a0aa2ace014c8a86f'
  127.                                    '0aa635d947ac9febe83ef4e55966144b'
  128.                                    '2a5ab39dc13814b94e3ab6e101a34f27',
  129.                    hashlib.sha512: 'fa73b0089d56a284efb0f0756c890be9'
  130.                                    'b1b5dbdd8ee81a3655f83e33b2279d39'
  131.                                    'bf3e848279a722c806b485a47e67c807'
  132.                                    'b946a337bee8942674278859e13292fb',
  133.                  })
  134.  
  135.         # 4.5.  Test Case 4
  136.         hmactest(key = ''.join([chr(x) for x in xrange(0x01, 0x19+1)]),
  137.                  data = '\xcd'*50,
  138.                  hexdigests = {
  139.                    hashlib.sha224: '6c11506874013cac6a2abc1bb382627c'
  140.                                    'ec6a90d86efc012de7afec5a',
  141.                    hashlib.sha256: '82558a389a443c0ea4cc819899f2083a'
  142.                                    '85f0faa3e578f8077a2e3ff46729665b',
  143.                    hashlib.sha384: '3e8a69b7783c25851933ab6290af6ca7'
  144.                                    '7a9981480850009cc5577c6e1f573b4e'
  145.                                    '6801dd23c4a7d679ccf8a386c674cffb',
  146.                    hashlib.sha512: 'b0ba465637458c6990e5a8c5f61d4af7'
  147.                                    'e576d97ff94b872de76f8050361ee3db'
  148.                                    'a91ca5c11aa25eb4d679275cc5788063'
  149.                                    'a5f19741120c4f2de2adebeb10a298dd',
  150.                  })
  151.  
  152.         # 4.7.  Test Case 6
  153.         hmactest(key = '\xaa'*131,
  154.                  data = 'Test Using Larger Than Block-Siz'
  155.                         'e Key - Hash Key First',
  156.                  hexdigests = {
  157.                    hashlib.sha224: '95e9a0db962095adaebe9b2d6f0dbce2'
  158.                                    'd499f112f2d2b7273fa6870e',
  159.                    hashlib.sha256: '60e431591ee0b67f0d8a26aacbf5b77f'
  160.                                    '8e0bc6213728c5140546040f0ee37f54',
  161.                    hashlib.sha384: '4ece084485813e9088d2c63a041bc5b4'
  162.                                    '4f9ef1012a2b588f3cd11f05033ac4c6'
  163.                                    '0c2ef6ab4030fe8296248df163f44952',
  164.                    hashlib.sha512: '80b24263c7c1a3ebb71493c1dd7be8b4'
  165.                                    '9b46d1f41b4aeec1121b013783f8f352'
  166.                                    '6b56d037e05f2598bd0fd2215d6a1e52'
  167.                                    '95e64f73f63f0aec8b915a985d786598',
  168.                  })
  169.  
  170.         # 4.8.  Test Case 7
  171.         hmactest(key = '\xaa'*131,
  172.                  data = 'This is a test using a larger th'
  173.                         'an block-size key and a larger t'
  174.                         'han block-size data. The key nee'
  175.                         'ds to be hashed before being use'
  176.                         'd by the HMAC algorithm.',
  177.                  hexdigests = {
  178.                    hashlib.sha224: '3a854166ac5d9f023f54d517d0b39dbd'
  179.                                    '946770db9c2b95c9f6f565d1',
  180.                    hashlib.sha256: '9b09ffa71b942fcb27635fbcd5b0e944'
  181.                                    'bfdc63644f0713938a7f51535c3a35e2',
  182.                    hashlib.sha384: '6617178e941f020d351e2f254e8fd32c'
  183.                                    '602420feb0b8fb9adccebb82461e99c5'
  184.                                    'a678cc31e799176d3860e6110c46523e',
  185.                    hashlib.sha512: 'e37b6a775dc87dbaa4dfa9f96e5e3ffd'
  186.                                    'debd71f8867289865df5a32d20cdc944'
  187.                                    'b6022cac3c4982b10d5eeb55c3e4de15'
  188.                                    '134676fb6de0446065c97440fa8c6a58',
  189.                  })
  190.  
  191.     def test_sha224_rfc4231(self):
  192.         self._rfc4231_test_cases(hashlib.sha224)
  193.  
  194.     def test_sha256_rfc4231(self):
  195.         self._rfc4231_test_cases(hashlib.sha256)
  196.  
  197.     def test_sha384_rfc4231(self):
  198.         self._rfc4231_test_cases(hashlib.sha384)
  199.  
  200.     def test_sha512_rfc4231(self):
  201.         self._rfc4231_test_cases(hashlib.sha512)
  202.  
  203.     def test_legacy_block_size_warnings(self):
  204.         class MockCrazyHash(object):
  205.             """Ain't no block_size attribute here."""
  206.             def __init__(self, *args):
  207.                 self._x = hashlib.sha1(*args)
  208.                 self.digest_size = self._x.digest_size
  209.             def update(self, v):
  210.                 self._x.update(v)
  211.             def digest(self):
  212.                 return self._x.digest()
  213.  
  214.         with warnings.catch_warnings():
  215.             warnings.simplefilter('error', RuntimeWarning)
  216.             try:
  217.                 hmac.HMAC('a', 'b', digestmod=MockCrazyHash)
  218.             except RuntimeWarning:
  219.                 pass
  220.             else:
  221.                 self.fail('Expected warning about missing block_size')
  222.  
  223.             MockCrazyHash.block_size = 1
  224.             try:
  225.                 hmac.HMAC('a', 'b', digestmod=MockCrazyHash)
  226.             except RuntimeWarning:
  227.                 pass
  228.             else:
  229.                 self.fail('Expected warning about small block_size')
  230.  
  231.  
  232.  
  233. class ConstructorTestCase(unittest.TestCase):
  234.  
  235.     def test_normal(self):
  236.         # Standard constructor call.
  237.         failed = 0
  238.         try:
  239.             h = hmac.HMAC("key")
  240.         except:
  241.             self.fail("Standard constructor call raised exception.")
  242.  
  243.     def test_withtext(self):
  244.         # Constructor call with text.
  245.         try:
  246.             h = hmac.HMAC("key", "hash this!")
  247.         except:
  248.             self.fail("Constructor call with text argument raised exception.")
  249.  
  250.     def test_withmodule(self):
  251.         # Constructor call with text and digest module.
  252.         try:
  253.             h = hmac.HMAC("key", "", hashlib.sha1)
  254.         except:
  255.             self.fail("Constructor call with hashlib.sha1 raised exception.")
  256.  
  257. class SanityTestCase(unittest.TestCase):
  258.  
  259.     def test_default_is_md5(self):
  260.         # Testing if HMAC defaults to MD5 algorithm.
  261.         # NOTE: this whitebox test depends on the hmac class internals
  262.         h = hmac.HMAC("key")
  263.         self.failUnless(h.digest_cons == hashlib.md5)
  264.  
  265.     def test_exercise_all_methods(self):
  266.         # Exercising all methods once.
  267.         # This must not raise any exceptions
  268.         try:
  269.             h = hmac.HMAC("my secret key")
  270.             h.update("compute the hash of this text!")
  271.             dig = h.digest()
  272.             dig = h.hexdigest()
  273.             h2 = h.copy()
  274.         except:
  275.             self.fail("Exception raised during normal usage of HMAC class.")
  276.  
  277. class CopyTestCase(unittest.TestCase):
  278.  
  279.     def test_attributes(self):
  280.         # Testing if attributes are of same type.
  281.         h1 = hmac.HMAC("key")
  282.         h2 = h1.copy()
  283.         self.failUnless(h1.digest_cons == h2.digest_cons,
  284.             "digest constructors don't match.")
  285.         self.failUnless(type(h1.inner) == type(h2.inner),
  286.             "Types of inner don't match.")
  287.         self.failUnless(type(h1.outer) == type(h2.outer),
  288.             "Types of outer don't match.")
  289.  
  290.     def test_realcopy(self):
  291.         # Testing if the copy method created a real copy.
  292.         h1 = hmac.HMAC("key")
  293.         h2 = h1.copy()
  294.         # Using id() in case somebody has overridden __cmp__.
  295.         self.failUnless(id(h1) != id(h2), "No real copy of the HMAC instance.")
  296.         self.failUnless(id(h1.inner) != id(h2.inner),
  297.             "No real copy of the attribute 'inner'.")
  298.         self.failUnless(id(h1.outer) != id(h2.outer),
  299.             "No real copy of the attribute 'outer'.")
  300.  
  301.     def test_equality(self):
  302.         # Testing if the copy has the same digests.
  303.         h1 = hmac.HMAC("key")
  304.         h1.update("some random text")
  305.         h2 = h1.copy()
  306.         self.failUnless(h1.digest() == h2.digest(),
  307.             "Digest of copy doesn't match original digest.")
  308.         self.failUnless(h1.hexdigest() == h2.hexdigest(),
  309.             "Hexdigest of copy doesn't match original hexdigest.")
  310.  
  311. def test_main():
  312.     test_support.run_unittest(
  313.         TestVectorsTestCase,
  314.         ConstructorTestCase,
  315.         SanityTestCase,
  316.         CopyTestCase
  317.     )
  318.  
  319. if __name__ == "__main__":
  320.     test_main()
  321.