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