test_sha.py 1.7 KB

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