test_SHA1.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. #
  3. # SelfTest/Hash/SHA1.py: Self-test for the SHA-1 hash function
  4. #
  5. # Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
  6. #
  7. # ===================================================================
  8. # The contents of this file are dedicated to the public domain. To
  9. # the extent that dedication to the public domain is not available,
  10. # everyone is granted a worldwide, perpetual, royalty-free,
  11. # non-exclusive license to exercise all rights associated with the
  12. # contents of this file for any purpose whatsoever.
  13. # No rights are reserved.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. # ===================================================================
  24. """Self-test suite for Crypto.Hash.SHA"""
  25. from binascii import hexlify
  26. from tls.Crypto.SelfTest.loader import load_tests
  27. # Test vectors from various sources
  28. # This is a list of (expected_result, input[, description]) tuples.
  29. test_data_various = [
  30. # FIPS PUB 180-2, A.1 - "One-Block Message"
  31. ('a9993e364706816aba3e25717850c26c9cd0d89d', 'abc'),
  32. # FIPS PUB 180-2, A.2 - "Multi-Block Message"
  33. ('84983e441c3bd26ebaae4aa1f95129e5e54670f1',
  34. 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'),
  35. # FIPS PUB 180-2, A.3 - "Long Message"
  36. # ('34aa973cd4c4daa4f61eeb2bdbad27316534016f',
  37. # 'a' * 10**6,
  38. # '"a" * 10**6'),
  39. # RFC 3174: Section 7.3, "TEST4" (multiple of 512 bits)
  40. ('dea356a2cddd90c7a7ecedc5ebb563934f460452',
  41. '01234567' * 80,
  42. '"01234567" * 80'),
  43. ]
  44. def get_tests(config={}):
  45. from tls.Crypto.Hash import SHA1
  46. from .common import make_hash_tests
  47. tests = []
  48. test_vectors = load_tests(("Crypto", "SelfTest", "Hash", "test_vectors", "SHA1"),
  49. "SHA1ShortMsg.rsp",
  50. "KAT SHA-1",
  51. { "len" : lambda x: int(x) } )
  52. test_data = test_data_various[:]
  53. for tv in test_vectors:
  54. try:
  55. if tv.startswith('['):
  56. continue
  57. except AttributeError:
  58. pass
  59. if tv.len == 0:
  60. tv.msg = b""
  61. test_data.append((hexlify(tv.md), tv.msg, tv.desc))
  62. tests = make_hash_tests(SHA1, "SHA1", test_data,
  63. digest_size=20,
  64. oid="1.3.14.3.2.26")
  65. return tests
  66. if __name__ == '__main__':
  67. import unittest
  68. suite = lambda: unittest.TestSuite(get_tests())
  69. unittest.main(defaultTest='suite')
  70. # vim:set ts=4 sw=4 sts=4 expandtab: