Guido van Rossum | a6386ce | 1999-03-24 19:04:32 +0000 | [diff] [blame] | 1 | # Testing sha module (NIST's Secure Hash Algorithm) |
| 2 | |
Guido van Rossum | a6386ce | 1999-03-24 19:04:32 +0000 | [diff] [blame] | 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 | |
Fred Drake | cf99225 | 2001-05-22 21:43:17 +0000 | [diff] [blame] | 7 | import sha |
Fred Drake | cf99225 | 2001-05-22 21:43:17 +0000 | [diff] [blame] | 8 | import unittest |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 9 | from test import test_support |
Guido van Rossum | a6386ce | 1999-03-24 19:04:32 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | a6386ce | 1999-03-24 19:04:32 +0000 | [diff] [blame] | 11 | |
Fred Drake | cf99225 | 2001-05-22 21:43:17 +0000 | [diff] [blame] | 12 | class SHATestCase(unittest.TestCase): |
| 13 | def check(self, data, digest): |
Andrew M. Kuchling | db4f255 | 2006-11-18 22:17:33 +0000 | [diff] [blame] | 14 | # Check digest matches the expected value |
| 15 | obj = sha.new(data) |
| 16 | computed = obj.hexdigest() |
Fred Drake | cf99225 | 2001-05-22 21:43:17 +0000 | [diff] [blame] | 17 | self.assert_(computed == digest) |
Guido van Rossum | a6386ce | 1999-03-24 19:04:32 +0000 | [diff] [blame] | 18 | |
Andrew M. Kuchling | db4f255 | 2006-11-18 22:17:33 +0000 | [diff] [blame] | 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 | |
Fred Drake | cf99225 | 2001-05-22 21:43:17 +0000 | [diff] [blame] | 31 | def test_case_1(self): |
| 32 | self.check("abc", |
| 33 | "a9993e364706816aba3e25717850c26c9cd0d89d") |
Guido van Rossum | a6386ce | 1999-03-24 19:04:32 +0000 | [diff] [blame] | 34 | |
Fred Drake | cf99225 | 2001-05-22 21:43:17 +0000 | [diff] [blame] | 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 | |
Andrew M. Kuchling | 9eec51c | 2006-11-19 18:40:01 +0000 | [diff] [blame^] | 43 | def test_case_4(self): |
| 44 | self.check(chr(0xAA) * 80, |
| 45 | '4ca0ef38f1794b28a8f8ee110ee79d48ce13be25') |
Fred Drake | cf99225 | 2001-05-22 21:43:17 +0000 | [diff] [blame] | 46 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 47 | def test_main(): |
| 48 | test_support.run_unittest(SHATestCase) |
| 49 | |
| 50 | |
| 51 | if __name__ == "__main__": |
| 52 | test_main() |