Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1 | # Test hashlib module |
| 2 | # |
| 3 | # $Id$ |
| 4 | # |
Benjamin Peterson | 46a9900 | 2010-01-09 18:45:30 +0000 | [diff] [blame] | 5 | # Copyright (C) 2005-2010 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 | |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 9 | import array |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 10 | import hashlib |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 11 | import itertools |
Antoine Pitrou | 019ff19 | 2012-05-16 16:41:26 +0200 | [diff] [blame] | 12 | import os |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 13 | import sys |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 14 | try: |
| 15 | import threading |
| 16 | except ImportError: |
| 17 | threading = None |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 18 | import unittest |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 19 | import warnings |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 20 | from test import support |
Antoine Pitrou | 94190bb | 2011-10-04 10:22:36 +0200 | [diff] [blame] | 21 | from test.support import _4G, bigmemtest |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 22 | |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 23 | # Were we compiled --with-pydebug or with #define Py_DEBUG? |
| 24 | COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount') |
| 25 | |
| 26 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 27 | def hexstr(s): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 28 | assert isinstance(s, bytes), repr(s) |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 29 | h = "0123456789abcdef" |
| 30 | r = '' |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 31 | for i in s: |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 32 | r += h[(i >> 4) & 0xF] + h[i & 0xF] |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 33 | return r |
| 34 | |
| 35 | |
| 36 | class HashLibTestCase(unittest.TestCase): |
| 37 | supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1', |
| 38 | 'sha224', 'SHA224', 'sha256', 'SHA256', |
Christian Heimes | 4a0270d | 2012-10-06 02:23:36 +0200 | [diff] [blame] | 39 | 'sha384', 'SHA384', 'sha512', 'SHA512', |
| 40 | 'sha3_224', 'sha3_256', 'sha3_384', |
| 41 | 'sha3_512', 'SHA3_224', 'SHA3_256', |
| 42 | 'SHA3_384', 'SHA3_512' ) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 43 | |
Antoine Pitrou | 019ff19 | 2012-05-16 16:41:26 +0200 | [diff] [blame] | 44 | # Issue #14693: fallback modules are always compiled under POSIX |
| 45 | _warn_on_extension_import = os.name == 'posix' or COMPILED_WITH_PYDEBUG |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 46 | |
| 47 | def _conditional_import_module(self, module_name): |
| 48 | """Import a module and return a reference to it or None on failure.""" |
| 49 | try: |
| 50 | exec('import '+module_name) |
| 51 | except ImportError as error: |
| 52 | if self._warn_on_extension_import: |
| 53 | warnings.warn('Did a C extension fail to compile? %s' % error) |
| 54 | return locals().get(module_name) |
| 55 | |
| 56 | def __init__(self, *args, **kwargs): |
| 57 | algorithms = set() |
| 58 | for algorithm in self.supported_hash_names: |
| 59 | algorithms.add(algorithm.lower()) |
| 60 | self.constructors_to_test = {} |
| 61 | for algorithm in algorithms: |
| 62 | self.constructors_to_test[algorithm] = set() |
| 63 | |
| 64 | # For each algorithm, test the direct constructor and the use |
| 65 | # of hashlib.new given the algorithm name. |
| 66 | for algorithm, constructors in self.constructors_to_test.items(): |
| 67 | constructors.add(getattr(hashlib, algorithm)) |
| 68 | def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm): |
| 69 | if data is None: |
| 70 | return hashlib.new(_alg) |
| 71 | return hashlib.new(_alg, data) |
| 72 | constructors.add(_test_algorithm_via_hashlib_new) |
| 73 | |
| 74 | _hashlib = self._conditional_import_module('_hashlib') |
| 75 | if _hashlib: |
| 76 | # These two algorithms should always be present when this module |
| 77 | # is compiled. If not, something was compiled wrong. |
| 78 | assert hasattr(_hashlib, 'openssl_md5') |
| 79 | assert hasattr(_hashlib, 'openssl_sha1') |
| 80 | for algorithm, constructors in self.constructors_to_test.items(): |
| 81 | constructor = getattr(_hashlib, 'openssl_'+algorithm, None) |
| 82 | if constructor: |
| 83 | constructors.add(constructor) |
| 84 | |
| 85 | _md5 = self._conditional_import_module('_md5') |
| 86 | if _md5: |
Gregory P. Smith | b04ded4 | 2010-01-03 00:38:10 +0000 | [diff] [blame] | 87 | self.constructors_to_test['md5'].add(_md5.md5) |
| 88 | _sha1 = self._conditional_import_module('_sha1') |
| 89 | if _sha1: |
| 90 | self.constructors_to_test['sha1'].add(_sha1.sha1) |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 91 | _sha256 = self._conditional_import_module('_sha256') |
| 92 | if _sha256: |
| 93 | self.constructors_to_test['sha224'].add(_sha256.sha224) |
| 94 | self.constructors_to_test['sha256'].add(_sha256.sha256) |
| 95 | _sha512 = self._conditional_import_module('_sha512') |
| 96 | if _sha512: |
| 97 | self.constructors_to_test['sha384'].add(_sha512.sha384) |
| 98 | self.constructors_to_test['sha512'].add(_sha512.sha512) |
Christian Heimes | 4a0270d | 2012-10-06 02:23:36 +0200 | [diff] [blame] | 99 | _sha3 = self._conditional_import_module('_sha3') |
| 100 | if _sha3: |
| 101 | self.constructors_to_test['sha3_224'].add(_sha3.sha3_224) |
| 102 | self.constructors_to_test['sha3_256'].add(_sha3.sha3_256) |
| 103 | self.constructors_to_test['sha3_384'].add(_sha3.sha3_384) |
| 104 | self.constructors_to_test['sha3_512'].add(_sha3.sha3_512) |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 105 | |
| 106 | super(HashLibTestCase, self).__init__(*args, **kwargs) |
| 107 | |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 108 | @property |
| 109 | def hash_constructors(self): |
| 110 | constructors = self.constructors_to_test.values() |
| 111 | return itertools.chain.from_iterable(constructors) |
| 112 | |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 113 | def test_hash_array(self): |
| 114 | a = array.array("b", range(10)) |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 115 | for cons in self.hash_constructors: |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 116 | c = cons(a) |
| 117 | c.hexdigest() |
| 118 | |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 119 | def test_algorithms_guaranteed(self): |
| 120 | self.assertEqual(hashlib.algorithms_guaranteed, |
Raymond Hettinger | bf1d2bc | 2011-01-24 04:52:27 +0000 | [diff] [blame] | 121 | set(_algo for _algo in self.supported_hash_names |
Gregory P. Smith | 86508cc | 2010-03-01 02:05:26 +0000 | [diff] [blame] | 122 | if _algo.islower())) |
| 123 | |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 124 | def test_algorithms_available(self): |
| 125 | self.assertTrue(set(hashlib.algorithms_guaranteed). |
| 126 | issubset(hashlib.algorithms_available)) |
| 127 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 128 | def test_unknown_hash(self): |
Amaury Forgeot d'Arc | 3a3dc17 | 2012-06-29 01:53:13 +0200 | [diff] [blame] | 129 | self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam') |
| 130 | self.assertRaises(TypeError, hashlib.new, 1) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 131 | |
Gregory P. Smith | 12c9d02 | 2011-05-14 15:15:49 -0700 | [diff] [blame] | 132 | def test_get_builtin_constructor(self): |
| 133 | get_builtin_constructor = hashlib.__dict__[ |
| 134 | '__get_builtin_constructor'] |
| 135 | self.assertRaises(ValueError, get_builtin_constructor, 'test') |
| 136 | try: |
| 137 | import _md5 |
| 138 | except ImportError: |
| 139 | pass |
| 140 | # This forces an ImportError for "import _md5" statements |
| 141 | sys.modules['_md5'] = None |
| 142 | try: |
| 143 | self.assertRaises(ValueError, get_builtin_constructor, 'md5') |
| 144 | finally: |
| 145 | if '_md5' in locals(): |
| 146 | sys.modules['_md5'] = _md5 |
| 147 | else: |
| 148 | del sys.modules['_md5'] |
Gregory P. Smith | 76c28f7 | 2012-07-21 21:19:53 -0700 | [diff] [blame] | 149 | self.assertRaises(TypeError, get_builtin_constructor, 3) |
Gregory P. Smith | 12c9d02 | 2011-05-14 15:15:49 -0700 | [diff] [blame] | 150 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 151 | def test_hexdigest(self): |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 152 | for cons in self.hash_constructors: |
| 153 | h = cons() |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 154 | assert isinstance(h.digest(), bytes), name |
| 155 | self.assertEqual(hexstr(h.digest()), h.hexdigest()) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 156 | |
| 157 | |
| 158 | def test_large_update(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 159 | aas = b'a' * 128 |
| 160 | bees = b'b' * 127 |
| 161 | cees = b'c' * 126 |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 162 | dees = b'd' * 2048 # HASHLIB_GIL_MINSIZE |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 163 | |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 164 | for cons in self.hash_constructors: |
| 165 | m1 = cons() |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 166 | m1.update(aas) |
| 167 | m1.update(bees) |
| 168 | m1.update(cees) |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 169 | m1.update(dees) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 170 | |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 171 | m2 = cons() |
| 172 | m2.update(aas + bees + cees + dees) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 173 | self.assertEqual(m1.digest(), m2.digest()) |
| 174 | |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 175 | m3 = cons(aas + bees + cees + dees) |
| 176 | self.assertEqual(m1.digest(), m3.digest()) |
| 177 | |
| 178 | # verify copy() doesn't touch original |
| 179 | m4 = cons(aas + bees + cees) |
| 180 | m4_digest = m4.digest() |
| 181 | m4_copy = m4.copy() |
| 182 | m4_copy.update(dees) |
| 183 | self.assertEqual(m1.digest(), m4_copy.digest()) |
| 184 | self.assertEqual(m4.digest(), m4_digest) |
| 185 | |
| 186 | def check(self, name, data, hexdigest): |
| 187 | hexdigest = hexdigest.lower() |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 188 | constructors = self.constructors_to_test[name] |
| 189 | # 2 is for hashlib.name(...) and hashlib.new(name, ...) |
| 190 | self.assertGreaterEqual(len(constructors), 2) |
| 191 | for hash_object_constructor in constructors: |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 192 | m = hash_object_constructor(data) |
| 193 | computed = m.hexdigest() |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 194 | self.assertEqual( |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 195 | computed, hexdigest, |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 196 | "Hash algorithm %s constructed using %s returned hexdigest" |
| 197 | " %r for %d byte input data that should have hashed to %r." |
| 198 | % (name, hash_object_constructor, |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 199 | computed, len(data), hexdigest)) |
| 200 | computed = m.digest() |
| 201 | digest = bytes.fromhex(hexdigest) |
| 202 | self.assertEqual(computed, digest) |
| 203 | self.assertEqual(len(digest), m.digest_size) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 204 | |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 205 | def check_no_unicode(self, algorithm_name): |
| 206 | # Unicode objects are not allowed as input. |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 207 | constructors = self.constructors_to_test[algorithm_name] |
| 208 | for hash_object_constructor in constructors: |
| 209 | self.assertRaises(TypeError, hash_object_constructor, 'spam') |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 210 | |
| 211 | def test_no_unicode(self): |
| 212 | self.check_no_unicode('md5') |
| 213 | self.check_no_unicode('sha1') |
| 214 | self.check_no_unicode('sha224') |
| 215 | self.check_no_unicode('sha256') |
| 216 | self.check_no_unicode('sha384') |
| 217 | self.check_no_unicode('sha512') |
Christian Heimes | 4a0270d | 2012-10-06 02:23:36 +0200 | [diff] [blame] | 218 | self.check_no_unicode('sha3_224') |
| 219 | self.check_no_unicode('sha3_256') |
| 220 | self.check_no_unicode('sha3_384') |
| 221 | self.check_no_unicode('sha3_512') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 222 | |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 223 | def check_blocksize_name(self, name, block_size=0, digest_size=0): |
| 224 | constructors = self.constructors_to_test[name] |
| 225 | for hash_object_constructor in constructors: |
| 226 | m = hash_object_constructor() |
| 227 | self.assertEqual(m.block_size, block_size) |
| 228 | self.assertEqual(m.digest_size, digest_size) |
| 229 | self.assertEqual(len(m.digest()), digest_size) |
| 230 | self.assertEqual(m.name.lower(), name.lower()) |
Christian Heimes | d49a371 | 2013-07-30 15:35:54 +0200 | [diff] [blame^] | 231 | # split for sha3_512 / _sha3.sha3 object |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 232 | self.assertIn(name.split("_")[0], repr(m).lower()) |
| 233 | |
| 234 | def test_blocksize_name(self): |
| 235 | self.check_blocksize_name('md5', 64, 16) |
| 236 | self.check_blocksize_name('sha1', 64, 20) |
| 237 | self.check_blocksize_name('sha224', 64, 28) |
| 238 | self.check_blocksize_name('sha256', 64, 32) |
| 239 | self.check_blocksize_name('sha384', 128, 48) |
| 240 | self.check_blocksize_name('sha512', 128, 64) |
Christian Heimes | d49a371 | 2013-07-30 15:35:54 +0200 | [diff] [blame^] | 241 | self.check_blocksize_name('sha3_224', NotImplemented, 28) |
| 242 | self.check_blocksize_name('sha3_256', NotImplemented, 32) |
| 243 | self.check_blocksize_name('sha3_384', NotImplemented, 48) |
| 244 | self.check_blocksize_name('sha3_512', NotImplemented, 64) |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 245 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 246 | def test_case_md5_0(self): |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 247 | self.check('md5', b'', 'd41d8cd98f00b204e9800998ecf8427e') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 248 | |
| 249 | def test_case_md5_1(self): |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 250 | self.check('md5', b'abc', '900150983cd24fb0d6963f7d28e17f72') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 251 | |
| 252 | def test_case_md5_2(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 253 | self.check('md5', |
| 254 | b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 255 | 'd174ab98d277d9f5a5611c2c9f419d9f') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 256 | |
Antoine Pitrou | 94190bb | 2011-10-04 10:22:36 +0200 | [diff] [blame] | 257 | @bigmemtest(size=_4G + 5, memuse=1) |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 258 | def test_case_md5_huge(self, size): |
| 259 | if size == _4G + 5: |
| 260 | try: |
Antoine Pitrou | d7ae299 | 2010-11-08 20:36:57 +0000 | [diff] [blame] | 261 | self.check('md5', b'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d') |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 262 | except OverflowError: |
| 263 | pass # 32-bit arch |
| 264 | |
Antoine Pitrou | 94190bb | 2011-10-04 10:22:36 +0200 | [diff] [blame] | 265 | @bigmemtest(size=_4G - 1, memuse=1) |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 266 | def test_case_md5_uintmax(self, size): |
| 267 | if size == _4G - 1: |
| 268 | try: |
Antoine Pitrou | d7ae299 | 2010-11-08 20:36:57 +0000 | [diff] [blame] | 269 | self.check('md5', b'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3') |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 270 | except OverflowError: |
| 271 | pass # 32-bit arch |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 272 | |
| 273 | # use the three examples from Federal Information Processing Standards |
| 274 | # Publication 180-1, Secure Hash Standard, 1995 April 17 |
| 275 | # http://www.itl.nist.gov/div897/pubs/fip180-1.htm |
| 276 | |
| 277 | def test_case_sha1_0(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 278 | self.check('sha1', b"", |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 279 | "da39a3ee5e6b4b0d3255bfef95601890afd80709") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 280 | |
| 281 | def test_case_sha1_1(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 282 | self.check('sha1', b"abc", |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 283 | "a9993e364706816aba3e25717850c26c9cd0d89d") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 284 | |
| 285 | def test_case_sha1_2(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 286 | self.check('sha1', |
| 287 | b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 288 | "84983e441c3bd26ebaae4aa1f95129e5e54670f1") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 289 | |
| 290 | def test_case_sha1_3(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 291 | self.check('sha1', b"a" * 1000000, |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 292 | "34aa973cd4c4daa4f61eeb2bdbad27316534016f") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 293 | |
| 294 | |
| 295 | # use the examples from Federal Information Processing Standards |
| 296 | # Publication 180-2, Secure Hash Standard, 2002 August 1 |
| 297 | # http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf |
| 298 | |
| 299 | def test_case_sha224_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 300 | self.check('sha224', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 301 | "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f") |
| 302 | |
| 303 | def test_case_sha224_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 304 | self.check('sha224', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 305 | "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7") |
| 306 | |
| 307 | def test_case_sha224_2(self): |
| 308 | self.check('sha224', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 309 | b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 310 | "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525") |
| 311 | |
| 312 | def test_case_sha224_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 313 | self.check('sha224', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 314 | "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67") |
| 315 | |
| 316 | |
| 317 | def test_case_sha256_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 318 | self.check('sha256', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 319 | "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") |
| 320 | |
| 321 | def test_case_sha256_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 322 | self.check('sha256', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 323 | "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") |
| 324 | |
| 325 | def test_case_sha256_2(self): |
| 326 | self.check('sha256', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 327 | b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 328 | "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1") |
| 329 | |
| 330 | def test_case_sha256_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 331 | self.check('sha256', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 332 | "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0") |
| 333 | |
| 334 | |
| 335 | def test_case_sha384_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 336 | self.check('sha384', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 337 | "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da"+ |
| 338 | "274edebfe76f65fbd51ad2f14898b95b") |
| 339 | |
| 340 | def test_case_sha384_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 341 | self.check('sha384', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 342 | "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"+ |
| 343 | "8086072ba1e7cc2358baeca134c825a7") |
| 344 | |
| 345 | def test_case_sha384_2(self): |
| 346 | self.check('sha384', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 347 | b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+ |
| 348 | b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 349 | "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"+ |
| 350 | "fcc7c71a557e2db966c3e9fa91746039") |
| 351 | |
| 352 | def test_case_sha384_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 353 | self.check('sha384', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 354 | "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b"+ |
| 355 | "07b8b3dc38ecc4ebae97ddd87f3d8985") |
| 356 | |
| 357 | |
| 358 | def test_case_sha512_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 359 | self.check('sha512', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 360 | "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"+ |
| 361 | "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e") |
| 362 | |
| 363 | def test_case_sha512_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 364 | self.check('sha512', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 365 | "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"+ |
| 366 | "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f") |
| 367 | |
| 368 | def test_case_sha512_2(self): |
| 369 | self.check('sha512', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 370 | b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+ |
| 371 | b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 372 | "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+ |
| 373 | "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909") |
| 374 | |
| 375 | def test_case_sha512_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 376 | self.check('sha512', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 377 | "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+ |
| 378 | "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b") |
| 379 | |
Christian Heimes | 4a0270d | 2012-10-06 02:23:36 +0200 | [diff] [blame] | 380 | # SHA-3 family |
| 381 | def test_case_sha3_224_0(self): |
| 382 | self.check('sha3_224', b"", |
| 383 | "F71837502BA8E10837BDD8D365ADB85591895602FC552B48B7390ABD") |
| 384 | |
| 385 | def test_case_sha3_224_1(self): |
| 386 | self.check('sha3_224', bytes.fromhex("CC"), |
| 387 | "A9CAB59EB40A10B246290F2D6086E32E3689FAF1D26B470C899F2802") |
| 388 | |
| 389 | def test_case_sha3_224_2(self): |
| 390 | self.check('sha3_224', bytes.fromhex("41FB"), |
| 391 | "615BA367AFDC35AAC397BC7EB5D58D106A734B24986D5D978FEFD62C") |
| 392 | |
| 393 | def test_case_sha3_224_3(self): |
| 394 | self.check('sha3_224', bytes.fromhex( |
| 395 | "433C5303131624C0021D868A30825475E8D0BD3052A022180398F4CA4423B9"+ |
| 396 | "8214B6BEAAC21C8807A2C33F8C93BD42B092CC1B06CEDF3224D5ED1EC29784"+ |
| 397 | "444F22E08A55AA58542B524B02CD3D5D5F6907AFE71C5D7462224A3F9D9E53"+ |
| 398 | "E7E0846DCBB4CE"), |
| 399 | "62B10F1B6236EBC2DA72957742A8D4E48E213B5F8934604BFD4D2C3A") |
| 400 | |
| 401 | @bigmemtest(size=_4G + 5, memuse=1) |
| 402 | def test_case_sha3_224_huge(self, size): |
| 403 | if size == _4G + 5: |
| 404 | try: |
| 405 | self.check('sha3_224', b'A'*size, |
| 406 | '58ef60057c9dddb6a87477e9ace5a26f0d9db01881cf9b10a9f8c224') |
| 407 | except OverflowError: |
| 408 | pass # 32-bit arch |
| 409 | |
| 410 | |
| 411 | def test_case_sha3_256_0(self): |
| 412 | self.check('sha3_256', b"", |
| 413 | "C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470") |
| 414 | |
| 415 | def test_case_sha3_256_1(self): |
| 416 | self.check('sha3_256', bytes.fromhex("CC"), |
| 417 | "EEAD6DBFC7340A56CAEDC044696A168870549A6A7F6F56961E84A54BD9970B8A") |
| 418 | |
| 419 | def test_case_sha3_256_2(self): |
| 420 | self.check('sha3_256', bytes.fromhex("41FB"), |
| 421 | "A8EACEDA4D47B3281A795AD9E1EA2122B407BAF9AABCB9E18B5717B7873537D2") |
| 422 | |
| 423 | def test_case_sha3_256_3(self): |
| 424 | self.check('sha3_256', bytes.fromhex( |
| 425 | "433C5303131624C0021D868A30825475E8D0BD3052A022180398F4CA4423B9"+ |
| 426 | "8214B6BEAAC21C8807A2C33F8C93BD42B092CC1B06CEDF3224D5ED1EC29784"+ |
| 427 | "444F22E08A55AA58542B524B02CD3D5D5F6907AFE71C5D7462224A3F9D9E53"+ |
| 428 | "E7E0846DCBB4CE"), |
| 429 | "CE87A5173BFFD92399221658F801D45C294D9006EE9F3F9D419C8D427748DC41") |
| 430 | |
| 431 | |
| 432 | def test_case_sha3_384_0(self): |
| 433 | self.check('sha3_384', b"", |
| 434 | "2C23146A63A29ACF99E73B88F8C24EAA7DC60AA771780CCC006AFBFA8FE2479B"+ |
| 435 | "2DD2B21362337441AC12B515911957FF") |
| 436 | |
| 437 | def test_case_sha3_384_1(self): |
| 438 | self.check('sha3_384', bytes.fromhex("CC"), |
| 439 | "1B84E62A46E5A201861754AF5DC95C4A1A69CAF4A796AE405680161E29572641"+ |
| 440 | "F5FA1E8641D7958336EE7B11C58F73E9") |
| 441 | |
| 442 | def test_case_sha3_384_2(self): |
| 443 | self.check('sha3_384', bytes.fromhex("41FB"), |
| 444 | "495CCE2714CD72C8C53C3363D22C58B55960FE26BE0BF3BBC7A3316DD563AD1D"+ |
| 445 | "B8410E75EEFEA655E39D4670EC0B1792") |
| 446 | |
| 447 | def test_case_sha3_384_3(self): |
| 448 | self.check('sha3_384', bytes.fromhex( |
| 449 | "433C5303131624C0021D868A30825475E8D0BD3052A022180398F4CA4423B9"+ |
| 450 | "8214B6BEAAC21C8807A2C33F8C93BD42B092CC1B06CEDF3224D5ED1EC29784"+ |
| 451 | "444F22E08A55AA58542B524B02CD3D5D5F6907AFE71C5D7462224A3F9D9E53"+ |
| 452 | "E7E0846DCBB4CE"), |
| 453 | "135114508DD63E279E709C26F7817C0482766CDE49132E3EDF2EEDD8996F4E35"+ |
| 454 | "96D184100B384868249F1D8B8FDAA2C9") |
| 455 | |
| 456 | |
| 457 | def test_case_sha3_512_0(self): |
| 458 | self.check('sha3_512', b"", |
| 459 | "0EAB42DE4C3CEB9235FC91ACFFE746B29C29A8C366B7C60E4E67C466F36A4304"+ |
| 460 | "C00FA9CAF9D87976BA469BCBE06713B435F091EF2769FB160CDAB33D3670680E") |
| 461 | |
| 462 | def test_case_sha3_512_1(self): |
| 463 | self.check('sha3_512', bytes.fromhex("CC"), |
| 464 | "8630C13CBD066EA74BBE7FE468FEC1DEE10EDC1254FB4C1B7C5FD69B646E4416"+ |
| 465 | "0B8CE01D05A0908CA790DFB080F4B513BC3B6225ECE7A810371441A5AC666EB9") |
| 466 | |
| 467 | def test_case_sha3_512_2(self): |
| 468 | self.check('sha3_512', bytes.fromhex("41FB"), |
| 469 | "551DA6236F8B96FCE9F97F1190E901324F0B45E06DBBB5CDB8355D6ED1DC34B3"+ |
| 470 | "F0EAE7DCB68622FF232FA3CECE0D4616CDEB3931F93803662A28DF1CD535B731") |
| 471 | |
| 472 | def test_case_sha3_512_3(self): |
| 473 | self.check('sha3_512', bytes.fromhex( |
| 474 | "433C5303131624C0021D868A30825475E8D0BD3052A022180398F4CA4423B9"+ |
| 475 | "8214B6BEAAC21C8807A2C33F8C93BD42B092CC1B06CEDF3224D5ED1EC29784"+ |
| 476 | "444F22E08A55AA58542B524B02CD3D5D5F6907AFE71C5D7462224A3F9D9E53"+ |
| 477 | "E7E0846DCBB4CE"), |
| 478 | "527D28E341E6B14F4684ADB4B824C496C6482E51149565D3D17226828884306B"+ |
| 479 | "51D6148A72622C2B75F5D3510B799D8BDC03EAEDE453676A6EC8FE03A1AD0EAB") |
| 480 | |
| 481 | |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 482 | def test_gil(self): |
| 483 | # Check things work fine with an input larger than the size required |
| 484 | # for multithreaded operation (which is hardwired to 2048). |
| 485 | gil_minsize = 2048 |
| 486 | |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 487 | for cons in self.hash_constructors: |
| 488 | m = cons() |
Christian Heimes | 4a0270d | 2012-10-06 02:23:36 +0200 | [diff] [blame] | 489 | m.update(b'1') |
| 490 | m.update(b'#' * gil_minsize) |
| 491 | m.update(b'1') |
| 492 | |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 493 | m = cons(b'x' * gil_minsize) |
Christian Heimes | 4a0270d | 2012-10-06 02:23:36 +0200 | [diff] [blame] | 494 | m.update(b'1') |
| 495 | |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 496 | m = hashlib.md5() |
| 497 | m.update(b'1') |
| 498 | m.update(b'#' * gil_minsize) |
| 499 | m.update(b'1') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 500 | self.assertEqual(m.hexdigest(), 'cb1e1a2cbc80be75e19935d621fb9b21') |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 501 | |
| 502 | m = hashlib.md5(b'x' * gil_minsize) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 503 | self.assertEqual(m.hexdigest(), 'cfb767f225d58469c5de3632a8803958') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 504 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 505 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 506 | @support.reap_threads |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 507 | def test_threaded_hashing(self): |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 508 | # Updating the same hash object from several threads at once |
| 509 | # using data chunk sizes containing the same byte sequences. |
| 510 | # |
| 511 | # If the internal locks are working to prevent multiple |
| 512 | # updates on the same object from running at once, the resulting |
| 513 | # hash will be the same as doing it single threaded upfront. |
| 514 | hasher = hashlib.sha1() |
| 515 | num_threads = 5 |
| 516 | smallest_data = b'swineflu' |
| 517 | data = smallest_data*200000 |
| 518 | expected_hash = hashlib.sha1(data*num_threads).hexdigest() |
| 519 | |
| 520 | def hash_in_chunks(chunk_size, event): |
| 521 | index = 0 |
| 522 | while index < len(data): |
| 523 | hasher.update(data[index:index+chunk_size]) |
| 524 | index += chunk_size |
| 525 | event.set() |
| 526 | |
| 527 | events = [] |
| 528 | for threadnum in range(num_threads): |
| 529 | chunk_size = len(data) // (10**threadnum) |
| 530 | assert chunk_size > 0 |
| 531 | assert chunk_size % len(smallest_data) == 0 |
| 532 | event = threading.Event() |
| 533 | events.append(event) |
| 534 | threading.Thread(target=hash_in_chunks, |
| 535 | args=(chunk_size, event)).start() |
| 536 | |
| 537 | for event in events: |
| 538 | event.wait() |
| 539 | |
| 540 | self.assertEqual(expected_hash, hasher.hexdigest()) |
| 541 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 542 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 543 | if __name__ == "__main__": |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 544 | unittest.main() |