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