blob: 6b38435548c1ddbceee328fce485d973817f91fc [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
Brett Cannonc2aa09a2007-05-31 19:20:00 +00007import warnings
8warnings.filterwarnings("ignore", "the sha module is deprecated.*",
9 DeprecationWarning)
10
Fred Drakecf992252001-05-22 21:43:17 +000011import sha
Fred Drakecf992252001-05-22 21:43:17 +000012import unittest
Barry Warsaw04f357c2002-07-23 19:04:11 +000013from test import test_support
Guido van Rossuma6386ce1999-03-24 19:04:32 +000014
Guido van Rossuma6386ce1999-03-24 19:04:32 +000015
Fred Drakecf992252001-05-22 21:43:17 +000016class SHATestCase(unittest.TestCase):
17 def check(self, data, digest):
Andrew M. Kuchlingdb4f2552006-11-18 22:17:33 +000018 # Check digest matches the expected value
19 obj = sha.new(data)
20 computed = obj.hexdigest()
Benjamin Peterson5c8da862009-06-30 22:57:08 +000021 self.assertTrue(computed == digest)
Guido van Rossuma6386ce1999-03-24 19:04:32 +000022
Andrew M. Kuchlingdb4f2552006-11-18 22:17:33 +000023 # Verify that the value doesn't change between two consecutive
24 # digest operations.
25 computed_again = obj.hexdigest()
Benjamin Peterson5c8da862009-06-30 22:57:08 +000026 self.assertTrue(computed == computed_again)
Andrew M. Kuchlingdb4f2552006-11-18 22:17:33 +000027
28 # Check hexdigest() output matches digest()'s output
29 digest = obj.digest()
30 hexd = ""
31 for c in digest:
32 hexd += '%02x' % ord(c)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000033 self.assertTrue(computed == hexd)
Andrew M. Kuchlingdb4f2552006-11-18 22:17:33 +000034
Fred Drakecf992252001-05-22 21:43:17 +000035 def test_case_1(self):
36 self.check("abc",
37 "a9993e364706816aba3e25717850c26c9cd0d89d")
Guido van Rossuma6386ce1999-03-24 19:04:32 +000038
Fred Drakecf992252001-05-22 21:43:17 +000039 def test_case_2(self):
40 self.check("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
41 "84983e441c3bd26ebaae4aa1f95129e5e54670f1")
42
43 def test_case_3(self):
44 self.check("a" * 1000000,
45 "34aa973cd4c4daa4f61eeb2bdbad27316534016f")
46
Andrew M. Kuchling9eec51c2006-11-19 18:40:01 +000047 def test_case_4(self):
48 self.check(chr(0xAA) * 80,
49 '4ca0ef38f1794b28a8f8ee110ee79d48ce13be25')
Fred Drakecf992252001-05-22 21:43:17 +000050
Fred Drake2e2be372001-09-20 21:33:42 +000051def test_main():
52 test_support.run_unittest(SHATestCase)
53
54
55if __name__ == "__main__":
56 test_main()