blob: ea224e439c3d5c60b415bb73f846c1f41ba2c20b [file] [log] [blame]
Guido van Rossuma6386ce1999-03-24 19:04:32 +00001# Testing sha module (NIST's Secure Hash Algorithm)
2
Guido van Rossuma6386ce1999-03-24 19:04:32 +00003# 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
Fred Drakecf992252001-05-22 21:43:17 +00007import sha
Fred Drakecf992252001-05-22 21:43:17 +00008import unittest
Barry Warsaw04f357c2002-07-23 19:04:11 +00009from test import test_support
Guido van Rossuma6386ce1999-03-24 19:04:32 +000010
Guido van Rossuma6386ce1999-03-24 19:04:32 +000011
Fred Drakecf992252001-05-22 21:43:17 +000012class SHATestCase(unittest.TestCase):
13 def check(self, data, digest):
Andrew M. Kuchlingdb4f2552006-11-18 22:17:33 +000014 # Check digest matches the expected value
15 obj = sha.new(data)
16 computed = obj.hexdigest()
Fred Drakecf992252001-05-22 21:43:17 +000017 self.assert_(computed == digest)
Guido van Rossuma6386ce1999-03-24 19:04:32 +000018
Andrew M. Kuchlingdb4f2552006-11-18 22:17:33 +000019 # 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
Fred Drakecf992252001-05-22 21:43:17 +000031 def test_case_1(self):
32 self.check("abc",
33 "a9993e364706816aba3e25717850c26c9cd0d89d")
Guido van Rossuma6386ce1999-03-24 19:04:32 +000034
Fred Drakecf992252001-05-22 21:43:17 +000035 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
Andrew M. Kuchling9eec51c2006-11-19 18:40:01 +000043 def test_case_4(self):
44 self.check(chr(0xAA) * 80,
45 '4ca0ef38f1794b28a8f8ee110ee79d48ce13be25')
Fred Drakecf992252001-05-22 21:43:17 +000046
Fred Drake2e2be372001-09-20 21:33:42 +000047def test_main():
48 test_support.run_unittest(SHATestCase)
49
50
51if __name__ == "__main__":
52 test_main()