Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1 | # Test hashlib module |
| 2 | # |
| 3 | # $Id$ |
| 4 | # |
Gregory P. Smith | c411918 | 2007-09-09 20:17:22 +0000 | [diff] [blame] | 5 | # Copyright (C) 2005 Gregory P. Smith (greg@krypto.org) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 6 | # Licensed to PSF under a Contributor Agreement. |
| 7 | # |
| 8 | |
| 9 | import hashlib |
| 10 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 11 | from test import support |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | def hexstr(s): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 15 | assert isinstance(s, bytes), repr(s) |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 16 | h = "0123456789abcdef" |
| 17 | r = '' |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 18 | for i in s: |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 19 | r += h[(i >> 4) & 0xF] + h[i & 0xF] |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 20 | return r |
| 21 | |
| 22 | |
| 23 | class HashLibTestCase(unittest.TestCase): |
| 24 | supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1', |
| 25 | 'sha224', 'SHA224', 'sha256', 'SHA256', |
| 26 | 'sha384', 'SHA384', 'sha512', 'SHA512' ) |
| 27 | |
| 28 | def test_unknown_hash(self): |
| 29 | try: |
| 30 | hashlib.new('spam spam spam spam spam') |
| 31 | except ValueError: |
| 32 | pass |
| 33 | else: |
| 34 | self.assert_(0 == "hashlib didn't reject bogus hash name") |
| 35 | |
| 36 | def test_hexdigest(self): |
| 37 | for name in self.supported_hash_names: |
| 38 | h = hashlib.new(name) |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 39 | assert isinstance(h.digest(), bytes), name |
| 40 | self.assertEqual(hexstr(h.digest()), h.hexdigest()) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 41 | |
| 42 | |
| 43 | def test_large_update(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 44 | aas = b'a' * 128 |
| 45 | bees = b'b' * 127 |
| 46 | cees = b'c' * 126 |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 47 | |
| 48 | for name in self.supported_hash_names: |
| 49 | m1 = hashlib.new(name) |
| 50 | m1.update(aas) |
| 51 | m1.update(bees) |
| 52 | m1.update(cees) |
| 53 | |
| 54 | m2 = hashlib.new(name) |
| 55 | m2.update(aas + bees + cees) |
| 56 | self.assertEqual(m1.digest(), m2.digest()) |
| 57 | |
| 58 | |
| 59 | def check(self, name, data, digest): |
| 60 | # test the direct constructors |
| 61 | computed = getattr(hashlib, name)(data).hexdigest() |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 62 | self.assertEqual(computed, digest) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 63 | # test the general new() interface |
| 64 | computed = hashlib.new(name, data).hexdigest() |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 65 | self.assertEqual(computed, digest) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 66 | |
| 67 | |
| 68 | def test_case_md5_0(self): |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 69 | self.check('md5', b'', 'd41d8cd98f00b204e9800998ecf8427e') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 70 | |
| 71 | def test_case_md5_1(self): |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 72 | self.check('md5', b'abc', '900150983cd24fb0d6963f7d28e17f72') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 73 | |
| 74 | def test_case_md5_2(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 75 | self.check('md5', |
| 76 | b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 77 | 'd174ab98d277d9f5a5611c2c9f419d9f') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | # use the three examples from Federal Information Processing Standards |
| 81 | # Publication 180-1, Secure Hash Standard, 1995 April 17 |
| 82 | # http://www.itl.nist.gov/div897/pubs/fip180-1.htm |
| 83 | |
| 84 | def test_case_sha1_0(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 85 | self.check('sha1', b"", |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 86 | "da39a3ee5e6b4b0d3255bfef95601890afd80709") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 87 | |
| 88 | def test_case_sha1_1(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 89 | self.check('sha1', b"abc", |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 90 | "a9993e364706816aba3e25717850c26c9cd0d89d") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 91 | |
| 92 | def test_case_sha1_2(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 93 | self.check('sha1', |
| 94 | b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 95 | "84983e441c3bd26ebaae4aa1f95129e5e54670f1") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 96 | |
| 97 | def test_case_sha1_3(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 98 | self.check('sha1', b"a" * 1000000, |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 99 | "34aa973cd4c4daa4f61eeb2bdbad27316534016f") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 100 | |
| 101 | |
| 102 | # use the examples from Federal Information Processing Standards |
| 103 | # Publication 180-2, Secure Hash Standard, 2002 August 1 |
| 104 | # http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf |
| 105 | |
| 106 | def test_case_sha224_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 107 | self.check('sha224', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 108 | "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f") |
| 109 | |
| 110 | def test_case_sha224_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 111 | self.check('sha224', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 112 | "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7") |
| 113 | |
| 114 | def test_case_sha224_2(self): |
| 115 | self.check('sha224', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 116 | b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 117 | "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525") |
| 118 | |
| 119 | def test_case_sha224_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 120 | self.check('sha224', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 121 | "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67") |
| 122 | |
| 123 | |
| 124 | def test_case_sha256_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 125 | self.check('sha256', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 126 | "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") |
| 127 | |
| 128 | def test_case_sha256_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 129 | self.check('sha256', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 130 | "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") |
| 131 | |
| 132 | def test_case_sha256_2(self): |
| 133 | self.check('sha256', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 134 | b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 135 | "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1") |
| 136 | |
| 137 | def test_case_sha256_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 138 | self.check('sha256', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 139 | "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0") |
| 140 | |
| 141 | |
| 142 | def test_case_sha384_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 143 | self.check('sha384', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 144 | "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da"+ |
| 145 | "274edebfe76f65fbd51ad2f14898b95b") |
| 146 | |
| 147 | def test_case_sha384_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 148 | self.check('sha384', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 149 | "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"+ |
| 150 | "8086072ba1e7cc2358baeca134c825a7") |
| 151 | |
| 152 | def test_case_sha384_2(self): |
| 153 | self.check('sha384', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 154 | b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+ |
| 155 | b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 156 | "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"+ |
| 157 | "fcc7c71a557e2db966c3e9fa91746039") |
| 158 | |
| 159 | def test_case_sha384_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 160 | self.check('sha384', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 161 | "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b"+ |
| 162 | "07b8b3dc38ecc4ebae97ddd87f3d8985") |
| 163 | |
| 164 | |
| 165 | def test_case_sha512_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 166 | self.check('sha512', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 167 | "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"+ |
| 168 | "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e") |
| 169 | |
| 170 | def test_case_sha512_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 171 | self.check('sha512', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 172 | "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"+ |
| 173 | "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f") |
| 174 | |
| 175 | def test_case_sha512_2(self): |
| 176 | self.check('sha512', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 177 | b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+ |
| 178 | b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 179 | "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+ |
| 180 | "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909") |
| 181 | |
| 182 | def test_case_sha512_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 183 | self.check('sha512', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 184 | "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+ |
| 185 | "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b") |
| 186 | |
| 187 | |
| 188 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 189 | support.run_unittest(HashLibTestCase) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 190 | |
| 191 | |
| 192 | if __name__ == "__main__": |
| 193 | test_main() |