python-2.5.2/win32/Lib/test/test_sha.py
changeset 0 ae805ac0140d
equal deleted inserted replaced
-1:000000000000 0:ae805ac0140d
       
     1 # Testing sha module (NIST's Secure Hash Algorithm)
       
     2 
       
     3 # use the three examples from Federal Information Processing Standards
       
     4 # Publication 180-1, Secure Hash Standard,  1995 April 17
       
     5 # http://www.itl.nist.gov/div897/pubs/fip180-1.htm
       
     6 
       
     7 import sha
       
     8 import unittest
       
     9 from test import test_support
       
    10 
       
    11 
       
    12 class SHATestCase(unittest.TestCase):
       
    13     def check(self, data, digest):
       
    14         # Check digest matches the expected value
       
    15         obj = sha.new(data)
       
    16         computed = obj.hexdigest()
       
    17         self.assert_(computed == digest)
       
    18 
       
    19         # Verify that the value doesn't change between two consecutive
       
    20         # digest operations.
       
    21         computed_again = obj.hexdigest()
       
    22         self.assert_(computed == computed_again)
       
    23 
       
    24         # Check hexdigest() output matches digest()'s output
       
    25         digest = obj.digest()
       
    26         hexd = ""
       
    27         for c in digest:
       
    28             hexd += '%02x' % ord(c)
       
    29         self.assert_(computed == hexd)
       
    30 
       
    31     def test_case_1(self):
       
    32         self.check("abc",
       
    33                    "a9993e364706816aba3e25717850c26c9cd0d89d")
       
    34 
       
    35     def test_case_2(self):
       
    36         self.check("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
       
    37                    "84983e441c3bd26ebaae4aa1f95129e5e54670f1")
       
    38 
       
    39     def test_case_3(self):
       
    40         self.check("a" * 1000000,
       
    41                    "34aa973cd4c4daa4f61eeb2bdbad27316534016f")
       
    42 
       
    43     def test_case_4(self):
       
    44         self.check(chr(0xAA) * 80,
       
    45                    '4ca0ef38f1794b28a8f8ee110ee79d48ce13be25')
       
    46 
       
    47 def test_main():
       
    48     test_support.run_unittest(SHATestCase)
       
    49 
       
    50 
       
    51 if __name__ == "__main__":
       
    52     test_main()