test_SHA256.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. #
  3. # SelfTest/Hash/test_SHA256.py: Self-test for the SHA-256 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.SHA256"""
  25. __revision__ = "$Id$"
  26. import unittest
  27. from Crypto.Util.py3compat import *
  28. class LargeSHA256Test(unittest.TestCase):
  29. def runTest(self):
  30. """SHA256: 512/520 MiB test"""
  31. from Crypto.Hash import SHA256
  32. zeros = bchr(0x00) * (1024*1024)
  33. h = SHA256.new(zeros)
  34. for i in xrange(511):
  35. h.update(zeros)
  36. # This test vector is from PyCrypto's old testdata.py file.
  37. self.assertEqual('9acca8e8c22201155389f65abbf6bc9723edc7384ead80503839f49dcc56d767', h.hexdigest()) # 512 MiB
  38. for i in xrange(8):
  39. h.update(zeros)
  40. # This test vector is from PyCrypto's old testdata.py file.
  41. self.assertEqual('abf51ad954b246009dfe5a50ecd582fd5b8f1b8b27f30393853c3ef721e7fa6e', h.hexdigest()) # 520 MiB
  42. def get_tests(config={}):
  43. # Test vectors from FIPS PUB 180-2
  44. # This is a list of (expected_result, input[, description]) tuples.
  45. test_data = [
  46. # FIPS PUB 180-2, B.1 - "One-Block Message"
  47. ('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad',
  48. 'abc'),
  49. # FIPS PUB 180-2, B.2 - "Multi-Block Message"
  50. ('248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1',
  51. 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'),
  52. # FIPS PUB 180-2, B.3 - "Long Message"
  53. ('cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0',
  54. 'a' * 10**6,
  55. '"a" * 10**6'),
  56. # Test for an old PyCrypto bug.
  57. ('f7fd017a3c721ce7ff03f3552c0813adcc48b7f33f07e5e2ba71e23ea393d103',
  58. 'This message is precisely 55 bytes long, to test a bug.',
  59. 'Length = 55 (mod 64)'),
  60. # Example from http://de.wikipedia.org/wiki/Secure_Hash_Algorithm
  61. ('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', ''),
  62. ('d32b568cd1b96d459e7291ebf4b25d007f275c9f13149beeb782fac0716613f8',
  63. 'Franz jagt im komplett verwahrlosten Taxi quer durch Bayern'),
  64. ]
  65. from Crypto.Hash import SHA256
  66. from common import make_hash_tests
  67. tests = make_hash_tests(SHA256, "SHA256", test_data,
  68. digest_size=32,
  69. oid="\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01")
  70. if config.get('slow_tests'):
  71. tests += [LargeSHA256Test()]
  72. return tests
  73. if __name__ == '__main__':
  74. import unittest
  75. suite = lambda: unittest.TestSuite(get_tests())
  76. unittest.main(defaultTest='suite')
  77. # vim:set ts=4 sw=4 sts=4 expandtab: