Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1 | # Test hashlib module |
| 2 | # |
| 3 | # $Id$ |
| 4 | # |
Georg Brandl | 8cdc9bc | 2010-01-01 13:07:05 +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 | 2b6e4bc | 2010-01-05 00:04:19 +0000 | [diff] [blame] | 9 | import array |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 10 | import hashlib |
Benjamin Peterson | 2b6e4bc | 2010-01-05 00:04:19 +0000 | [diff] [blame] | 11 | import itertools |
Gregory P. Smith | 6dcdcde | 2010-01-03 00:19:04 +0000 | [diff] [blame] | 12 | import sys |
Gregory P. Smith | d02eeda | 2009-05-04 00:16:49 +0000 | [diff] [blame] | 13 | try: |
| 14 | import threading |
| 15 | except ImportError: |
| 16 | threading = None |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 17 | import unittest |
Gregory P. Smith | 6dcdcde | 2010-01-03 00:19:04 +0000 | [diff] [blame] | 18 | import warnings |
Benjamin Peterson | 48f2e99 | 2014-05-31 13:26:22 -0700 | [diff] [blame] | 19 | from binascii import unhexlify |
| 20 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 21 | from test import test_support |
Benjamin Peterson | 8c2b7dc | 2008-09-18 01:22:16 +0000 | [diff] [blame] | 22 | from test.test_support import _4G, precisionbigmemtest |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 23 | |
Gregory P. Smith | 6dcdcde | 2010-01-03 00:19:04 +0000 | [diff] [blame] | 24 | # Were we compiled --with-pydebug or with #define Py_DEBUG? |
| 25 | COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount') |
| 26 | |
| 27 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 28 | def hexstr(s): |
| 29 | import string |
| 30 | h = string.hexdigits |
| 31 | r = '' |
| 32 | for c in s: |
| 33 | i = ord(c) |
| 34 | r = r + h[(i >> 4) & 0xF] + h[i & 0xF] |
| 35 | return r |
| 36 | |
| 37 | |
| 38 | class HashLibTestCase(unittest.TestCase): |
| 39 | supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1', |
| 40 | 'sha224', 'SHA224', 'sha256', 'SHA256', |
| 41 | 'sha384', 'SHA384', 'sha512', 'SHA512' ) |
| 42 | |
Gregory P. Smith | 6dcdcde | 2010-01-03 00:19:04 +0000 | [diff] [blame] | 43 | _warn_on_extension_import = COMPILED_WITH_PYDEBUG |
| 44 | |
| 45 | def _conditional_import_module(self, module_name): |
| 46 | """Import a module and return a reference to it or None on failure.""" |
| 47 | try: |
| 48 | exec('import '+module_name) |
| 49 | except ImportError, error: |
| 50 | if self._warn_on_extension_import: |
| 51 | warnings.warn('Did a C extension fail to compile? %s' % error) |
| 52 | return locals().get(module_name) |
| 53 | |
| 54 | def __init__(self, *args, **kwargs): |
| 55 | algorithms = set() |
| 56 | for algorithm in self.supported_hash_names: |
| 57 | algorithms.add(algorithm.lower()) |
| 58 | self.constructors_to_test = {} |
| 59 | for algorithm in algorithms: |
| 60 | self.constructors_to_test[algorithm] = set() |
| 61 | |
| 62 | # For each algorithm, test the direct constructor and the use |
| 63 | # of hashlib.new given the algorithm name. |
| 64 | for algorithm, constructors in self.constructors_to_test.items(): |
| 65 | constructors.add(getattr(hashlib, algorithm)) |
| 66 | def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm): |
| 67 | if data is None: |
| 68 | return hashlib.new(_alg) |
| 69 | return hashlib.new(_alg, data) |
| 70 | constructors.add(_test_algorithm_via_hashlib_new) |
| 71 | |
| 72 | _hashlib = self._conditional_import_module('_hashlib') |
| 73 | if _hashlib: |
| 74 | # These two algorithms should always be present when this module |
| 75 | # is compiled. If not, something was compiled wrong. |
| 76 | assert hasattr(_hashlib, 'openssl_md5') |
| 77 | assert hasattr(_hashlib, 'openssl_sha1') |
| 78 | for algorithm, constructors in self.constructors_to_test.items(): |
| 79 | constructor = getattr(_hashlib, 'openssl_'+algorithm, None) |
| 80 | if constructor: |
| 81 | constructors.add(constructor) |
| 82 | |
| 83 | _md5 = self._conditional_import_module('_md5') |
| 84 | if _md5: |
| 85 | self.constructors_to_test['md5'].add(_md5.new) |
| 86 | _sha = self._conditional_import_module('_sha') |
| 87 | if _sha: |
| 88 | self.constructors_to_test['sha1'].add(_sha.new) |
| 89 | _sha256 = self._conditional_import_module('_sha256') |
| 90 | if _sha256: |
| 91 | self.constructors_to_test['sha224'].add(_sha256.sha224) |
| 92 | self.constructors_to_test['sha256'].add(_sha256.sha256) |
| 93 | _sha512 = self._conditional_import_module('_sha512') |
| 94 | if _sha512: |
| 95 | self.constructors_to_test['sha384'].add(_sha512.sha384) |
| 96 | self.constructors_to_test['sha512'].add(_sha512.sha512) |
| 97 | |
| 98 | super(HashLibTestCase, self).__init__(*args, **kwargs) |
| 99 | |
Benjamin Peterson | 2b6e4bc | 2010-01-05 00:04:19 +0000 | [diff] [blame] | 100 | def test_hash_array(self): |
| 101 | a = array.array("b", range(10)) |
| 102 | constructors = self.constructors_to_test.itervalues() |
| 103 | for cons in itertools.chain.from_iterable(constructors): |
| 104 | c = cons(a) |
| 105 | c.hexdigest() |
| 106 | |
Gregory P. Smith | e6390a1 | 2010-03-01 02:01:47 +0000 | [diff] [blame] | 107 | def test_algorithms_attribute(self): |
| 108 | self.assertEqual(hashlib.algorithms, |
| 109 | tuple([_algo for _algo in self.supported_hash_names if |
| 110 | _algo.islower()])) |
| 111 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 112 | def test_unknown_hash(self): |
Amaury Forgeot d'Arc | d958cc9 | 2012-06-29 01:42:46 +0200 | [diff] [blame] | 113 | self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam') |
| 114 | self.assertRaises(TypeError, hashlib.new, 1) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 115 | |
Gregory P. Smith | fb1d60c | 2011-05-14 15:07:53 -0700 | [diff] [blame] | 116 | def test_get_builtin_constructor(self): |
| 117 | get_builtin_constructor = hashlib.__dict__[ |
| 118 | '__get_builtin_constructor'] |
| 119 | self.assertRaises(ValueError, get_builtin_constructor, 'test') |
| 120 | try: |
| 121 | import _md5 |
| 122 | except ImportError: |
| 123 | pass |
| 124 | # This forces an ImportError for "import _md5" statements |
| 125 | sys.modules['_md5'] = None |
| 126 | try: |
| 127 | self.assertRaises(ValueError, get_builtin_constructor, 'md5') |
| 128 | finally: |
| 129 | if '_md5' in locals(): |
| 130 | sys.modules['_md5'] = _md5 |
| 131 | else: |
| 132 | del sys.modules['_md5'] |
Gregory P. Smith | b9e9e0d | 2012-07-21 21:22:16 -0700 | [diff] [blame] | 133 | self.assertRaises(TypeError, get_builtin_constructor, 3) |
Gregory P. Smith | fb1d60c | 2011-05-14 15:07:53 -0700 | [diff] [blame] | 134 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 135 | def test_hexdigest(self): |
| 136 | for name in self.supported_hash_names: |
| 137 | h = hashlib.new(name) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 138 | self.assertTrue(hexstr(h.digest()) == h.hexdigest()) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 139 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 140 | def test_large_update(self): |
| 141 | aas = 'a' * 128 |
| 142 | bees = 'b' * 127 |
| 143 | cees = 'c' * 126 |
Gregory P. Smith | ea38826 | 2009-02-13 03:00:00 +0000 | [diff] [blame] | 144 | abcs = aas + bees + cees |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 145 | |
| 146 | for name in self.supported_hash_names: |
| 147 | m1 = hashlib.new(name) |
| 148 | m1.update(aas) |
| 149 | m1.update(bees) |
| 150 | m1.update(cees) |
| 151 | |
| 152 | m2 = hashlib.new(name) |
Gregory P. Smith | ea38826 | 2009-02-13 03:00:00 +0000 | [diff] [blame] | 153 | m2.update(abcs) |
| 154 | self.assertEqual(m1.digest(), m2.digest(), name+' update problem.') |
| 155 | |
| 156 | m3 = hashlib.new(name, abcs) |
| 157 | self.assertEqual(m1.digest(), m3.digest(), name+' new problem.') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 158 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 159 | def check(self, name, data, digest): |
Gregory P. Smith | 6dcdcde | 2010-01-03 00:19:04 +0000 | [diff] [blame] | 160 | constructors = self.constructors_to_test[name] |
| 161 | # 2 is for hashlib.name(...) and hashlib.new(name, ...) |
| 162 | self.assertGreaterEqual(len(constructors), 2) |
| 163 | for hash_object_constructor in constructors: |
| 164 | computed = hash_object_constructor(data).hexdigest() |
| 165 | self.assertEqual( |
| 166 | computed, digest, |
| 167 | "Hash algorithm %s constructed using %s returned hexdigest" |
| 168 | " %r for %d byte input data that should have hashed to %r." |
| 169 | % (name, hash_object_constructor, |
| 170 | computed, len(data), digest)) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 171 | |
Jesus Cea | 3fb774e | 2012-09-10 21:39:07 +0200 | [diff] [blame] | 172 | def check_update(self, name, data, digest): |
| 173 | constructors = self.constructors_to_test[name] |
| 174 | # 2 is for hashlib.name(...) and hashlib.new(name, ...) |
| 175 | self.assertGreaterEqual(len(constructors), 2) |
| 176 | for hash_object_constructor in constructors: |
| 177 | h = hash_object_constructor() |
| 178 | h.update(data) |
| 179 | computed = h.hexdigest() |
| 180 | self.assertEqual( |
| 181 | computed, digest, |
| 182 | "Hash algorithm %s using %s when updated returned hexdigest" |
| 183 | " %r for %d byte input data that should have hashed to %r." |
| 184 | % (name, hash_object_constructor, |
| 185 | computed, len(data), digest)) |
| 186 | |
Gregory P. Smith | 443ec68 | 2010-01-02 22:28:48 +0000 | [diff] [blame] | 187 | def check_unicode(self, algorithm_name): |
Gregory P. Smith | ea38826 | 2009-02-13 03:00:00 +0000 | [diff] [blame] | 188 | # Unicode objects are not allowed as input. |
Gregory P. Smith | 443ec68 | 2010-01-02 22:28:48 +0000 | [diff] [blame] | 189 | expected = hashlib.new(algorithm_name, str(u'spam')).hexdigest() |
Gregory P. Smith | 6dcdcde | 2010-01-03 00:19:04 +0000 | [diff] [blame] | 190 | self.check(algorithm_name, u'spam', expected) |
Gregory P. Smith | ea38826 | 2009-02-13 03:00:00 +0000 | [diff] [blame] | 191 | |
Gregory P. Smith | 443ec68 | 2010-01-02 22:28:48 +0000 | [diff] [blame] | 192 | def test_unicode(self): |
| 193 | # In python 2.x unicode is auto-encoded to the system default encoding |
| 194 | # when passed to hashlib functions. |
| 195 | self.check_unicode('md5') |
| 196 | self.check_unicode('sha1') |
| 197 | self.check_unicode('sha224') |
| 198 | self.check_unicode('sha256') |
| 199 | self.check_unicode('sha384') |
| 200 | self.check_unicode('sha512') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 201 | |
| 202 | def test_case_md5_0(self): |
| 203 | self.check('md5', '', 'd41d8cd98f00b204e9800998ecf8427e') |
| 204 | |
| 205 | def test_case_md5_1(self): |
| 206 | self.check('md5', 'abc', '900150983cd24fb0d6963f7d28e17f72') |
| 207 | |
| 208 | def test_case_md5_2(self): |
| 209 | self.check('md5', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', |
| 210 | 'd174ab98d277d9f5a5611c2c9f419d9f') |
| 211 | |
Serhiy Storchaka | c9da089 | 2014-01-10 13:36:56 +0200 | [diff] [blame] | 212 | @unittest.skipIf(sys.maxsize < _4G + 5, 'test cannot run on 32-bit systems') |
| 213 | @precisionbigmemtest(size=_4G + 5, memuse=1, dry_run=False) |
Benjamin Peterson | 8c2b7dc | 2008-09-18 01:22:16 +0000 | [diff] [blame] | 214 | def test_case_md5_huge(self, size): |
Serhiy Storchaka | c9da089 | 2014-01-10 13:36:56 +0200 | [diff] [blame] | 215 | self.check('md5', 'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d') |
Benjamin Peterson | 8c2b7dc | 2008-09-18 01:22:16 +0000 | [diff] [blame] | 216 | |
Serhiy Storchaka | c9da089 | 2014-01-10 13:36:56 +0200 | [diff] [blame] | 217 | @unittest.skipIf(sys.maxsize < _4G + 5, 'test cannot run on 32-bit systems') |
| 218 | @precisionbigmemtest(size=_4G + 5, memuse=1, dry_run=False) |
Jesus Cea | 3fb774e | 2012-09-10 21:39:07 +0200 | [diff] [blame] | 219 | def test_case_md5_huge_update(self, size): |
Serhiy Storchaka | c9da089 | 2014-01-10 13:36:56 +0200 | [diff] [blame] | 220 | self.check_update('md5', 'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d') |
Jesus Cea | 3fb774e | 2012-09-10 21:39:07 +0200 | [diff] [blame] | 221 | |
Serhiy Storchaka | c9da089 | 2014-01-10 13:36:56 +0200 | [diff] [blame] | 222 | @unittest.skipIf(sys.maxsize < _4G - 1, 'test cannot run on 32-bit systems') |
| 223 | @precisionbigmemtest(size=_4G - 1, memuse=1, dry_run=False) |
Benjamin Peterson | 8c2b7dc | 2008-09-18 01:22:16 +0000 | [diff] [blame] | 224 | def test_case_md5_uintmax(self, size): |
Serhiy Storchaka | c9da089 | 2014-01-10 13:36:56 +0200 | [diff] [blame] | 225 | self.check('md5', 'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 226 | |
| 227 | # use the three examples from Federal Information Processing Standards |
| 228 | # Publication 180-1, Secure Hash Standard, 1995 April 17 |
| 229 | # http://www.itl.nist.gov/div897/pubs/fip180-1.htm |
| 230 | |
| 231 | def test_case_sha1_0(self): |
| 232 | self.check('sha1', "", |
| 233 | "da39a3ee5e6b4b0d3255bfef95601890afd80709") |
| 234 | |
| 235 | def test_case_sha1_1(self): |
| 236 | self.check('sha1', "abc", |
| 237 | "a9993e364706816aba3e25717850c26c9cd0d89d") |
| 238 | |
| 239 | def test_case_sha1_2(self): |
| 240 | self.check('sha1', "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
| 241 | "84983e441c3bd26ebaae4aa1f95129e5e54670f1") |
| 242 | |
| 243 | def test_case_sha1_3(self): |
| 244 | self.check('sha1', "a" * 1000000, |
| 245 | "34aa973cd4c4daa4f61eeb2bdbad27316534016f") |
| 246 | |
Jesus Cea | 03a9d2a | 2012-09-10 21:04:42 +0200 | [diff] [blame] | 247 | @precisionbigmemtest(size=_4G + 5, memuse=1) |
| 248 | def test_case_sha1_huge(self, size): |
| 249 | if size == _4G + 5: |
| 250 | try: |
| 251 | self.check('sha1', 'A'*size, |
| 252 | '87d745c50e6b2879ffa0fb2c930e9fbfe0dc9a5b') |
| 253 | except OverflowError: |
| 254 | pass # 32-bit arch |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 255 | |
Jesus Cea | 3fb774e | 2012-09-10 21:39:07 +0200 | [diff] [blame] | 256 | @precisionbigmemtest(size=_4G + 5, memuse=1) |
| 257 | def test_case_sha1_huge_update(self, size): |
| 258 | if size == _4G + 5: |
| 259 | try: |
| 260 | self.check_update('sha1', 'A'*size, |
| 261 | '87d745c50e6b2879ffa0fb2c930e9fbfe0dc9a5b') |
| 262 | except OverflowError: |
| 263 | pass # 32-bit arch |
| 264 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 265 | # use the examples from Federal Information Processing Standards |
| 266 | # Publication 180-2, Secure Hash Standard, 2002 August 1 |
| 267 | # http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf |
| 268 | |
| 269 | def test_case_sha224_0(self): |
| 270 | self.check('sha224', "", |
| 271 | "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f") |
| 272 | |
| 273 | def test_case_sha224_1(self): |
| 274 | self.check('sha224', "abc", |
| 275 | "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7") |
| 276 | |
| 277 | def test_case_sha224_2(self): |
| 278 | self.check('sha224', |
| 279 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
| 280 | "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525") |
| 281 | |
| 282 | def test_case_sha224_3(self): |
| 283 | self.check('sha224', "a" * 1000000, |
| 284 | "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67") |
| 285 | |
| 286 | |
| 287 | def test_case_sha256_0(self): |
| 288 | self.check('sha256', "", |
| 289 | "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") |
| 290 | |
| 291 | def test_case_sha256_1(self): |
| 292 | self.check('sha256', "abc", |
| 293 | "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") |
| 294 | |
| 295 | def test_case_sha256_2(self): |
| 296 | self.check('sha256', |
| 297 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
| 298 | "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1") |
| 299 | |
| 300 | def test_case_sha256_3(self): |
| 301 | self.check('sha256', "a" * 1000000, |
| 302 | "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0") |
| 303 | |
| 304 | |
| 305 | def test_case_sha384_0(self): |
| 306 | self.check('sha384', "", |
| 307 | "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da"+ |
| 308 | "274edebfe76f65fbd51ad2f14898b95b") |
| 309 | |
| 310 | def test_case_sha384_1(self): |
| 311 | self.check('sha384', "abc", |
| 312 | "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"+ |
| 313 | "8086072ba1e7cc2358baeca134c825a7") |
| 314 | |
| 315 | def test_case_sha384_2(self): |
| 316 | self.check('sha384', |
| 317 | "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+ |
| 318 | "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", |
| 319 | "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"+ |
| 320 | "fcc7c71a557e2db966c3e9fa91746039") |
| 321 | |
| 322 | def test_case_sha384_3(self): |
| 323 | self.check('sha384', "a" * 1000000, |
| 324 | "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b"+ |
| 325 | "07b8b3dc38ecc4ebae97ddd87f3d8985") |
| 326 | |
| 327 | |
| 328 | def test_case_sha512_0(self): |
| 329 | self.check('sha512', "", |
| 330 | "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"+ |
| 331 | "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e") |
| 332 | |
| 333 | def test_case_sha512_1(self): |
| 334 | self.check('sha512', "abc", |
| 335 | "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"+ |
| 336 | "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f") |
| 337 | |
| 338 | def test_case_sha512_2(self): |
| 339 | self.check('sha512', |
| 340 | "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+ |
| 341 | "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", |
| 342 | "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+ |
| 343 | "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909") |
| 344 | |
| 345 | def test_case_sha512_3(self): |
| 346 | self.check('sha512', "a" * 1000000, |
| 347 | "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+ |
| 348 | "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b") |
| 349 | |
Victor Stinner | fd8ea99 | 2010-04-27 22:59:35 +0000 | [diff] [blame] | 350 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 351 | @test_support.reap_threads |
Gregory P. Smith | d02eeda | 2009-05-04 00:16:49 +0000 | [diff] [blame] | 352 | def test_threaded_hashing(self): |
Gregory P. Smith | d02eeda | 2009-05-04 00:16:49 +0000 | [diff] [blame] | 353 | # Updating the same hash object from several threads at once |
| 354 | # using data chunk sizes containing the same byte sequences. |
| 355 | # |
| 356 | # If the internal locks are working to prevent multiple |
| 357 | # updates on the same object from running at once, the resulting |
| 358 | # hash will be the same as doing it single threaded upfront. |
| 359 | hasher = hashlib.sha1() |
| 360 | num_threads = 5 |
| 361 | smallest_data = 'swineflu' |
| 362 | data = smallest_data*200000 |
| 363 | expected_hash = hashlib.sha1(data*num_threads).hexdigest() |
| 364 | |
| 365 | def hash_in_chunks(chunk_size, event): |
| 366 | index = 0 |
| 367 | while index < len(data): |
| 368 | hasher.update(data[index:index+chunk_size]) |
| 369 | index += chunk_size |
| 370 | event.set() |
| 371 | |
| 372 | events = [] |
| 373 | for threadnum in xrange(num_threads): |
| 374 | chunk_size = len(data) // (10**threadnum) |
| 375 | assert chunk_size > 0 |
| 376 | assert chunk_size % len(smallest_data) == 0 |
| 377 | event = threading.Event() |
| 378 | events.append(event) |
| 379 | threading.Thread(target=hash_in_chunks, |
| 380 | args=(chunk_size, event)).start() |
| 381 | |
| 382 | for event in events: |
| 383 | event.wait() |
| 384 | |
| 385 | self.assertEqual(expected_hash, hasher.hexdigest()) |
| 386 | |
Benjamin Peterson | 48f2e99 | 2014-05-31 13:26:22 -0700 | [diff] [blame] | 387 | |
| 388 | class KDFTests(unittest.TestCase): |
| 389 | pbkdf2_test_vectors = [ |
| 390 | (b'password', b'salt', 1, None), |
| 391 | (b'password', b'salt', 2, None), |
| 392 | (b'password', b'salt', 4096, None), |
| 393 | # too slow, it takes over a minute on a fast CPU. |
| 394 | #(b'password', b'salt', 16777216, None), |
| 395 | (b'passwordPASSWORDpassword', b'saltSALTsaltSALTsaltSALTsaltSALTsalt', |
| 396 | 4096, -1), |
| 397 | (b'pass\0word', b'sa\0lt', 4096, 16), |
| 398 | ] |
| 399 | |
| 400 | pbkdf2_results = { |
| 401 | "sha1": [ |
| 402 | # offical test vectors from RFC 6070 |
| 403 | (unhexlify('0c60c80f961f0e71f3a9b524af6012062fe037a6'), None), |
| 404 | (unhexlify('ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957'), None), |
| 405 | (unhexlify('4b007901b765489abead49d926f721d065a429c1'), None), |
| 406 | #(unhexlify('eefe3d61cd4da4e4e9945b3d6ba2158c2634e984'), None), |
| 407 | (unhexlify('3d2eec4fe41c849b80c8d83662c0e44a8b291a964c' |
| 408 | 'f2f07038'), 25), |
| 409 | (unhexlify('56fa6aa75548099dcc37d7f03425e0c3'), None),], |
| 410 | "sha256": [ |
| 411 | (unhexlify('120fb6cffcf8b32c43e7225256c4f837' |
| 412 | 'a86548c92ccc35480805987cb70be17b'), None), |
| 413 | (unhexlify('ae4d0c95af6b46d32d0adff928f06dd0' |
| 414 | '2a303f8ef3c251dfd6e2d85a95474c43'), None), |
| 415 | (unhexlify('c5e478d59288c841aa530db6845c4c8d' |
| 416 | '962893a001ce4e11a4963873aa98134a'), None), |
| 417 | #(unhexlify('cf81c66fe8cfc04d1f31ecb65dab4089' |
| 418 | # 'f7f179e89b3b0bcb17ad10e3ac6eba46'), None), |
| 419 | (unhexlify('348c89dbcbd32b2f32d814b8116e84cf2b17' |
| 420 | '347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9'), 40), |
| 421 | (unhexlify('89b69d0516f829893c696226650a8687'), None),], |
| 422 | "sha512": [ |
| 423 | (unhexlify('867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5' |
| 424 | 'd513554e1c8cf252c02d470a285a0501bad999bfe943c08f' |
| 425 | '050235d7d68b1da55e63f73b60a57fce'), None), |
| 426 | (unhexlify('e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f004071' |
| 427 | '3f18aefdb866d53cf76cab2868a39b9f7840edce4fef5a82' |
| 428 | 'be67335c77a6068e04112754f27ccf4e'), None), |
| 429 | (unhexlify('d197b1b33db0143e018b12f3d1d1479e6cdebdcc97c5c0f8' |
| 430 | '7f6902e072f457b5143f30602641b3d55cd335988cb36b84' |
| 431 | '376060ecd532e039b742a239434af2d5'), None), |
| 432 | (unhexlify('8c0511f4c6e597c6ac6315d8f0362e225f3c501495ba23b8' |
| 433 | '68c005174dc4ee71115b59f9e60cd9532fa33e0f75aefe30' |
| 434 | '225c583a186cd82bd4daea9724a3d3b8'), 64), |
| 435 | (unhexlify('9d9e9c4cd21fe4be24d5b8244c759665'), None),], |
| 436 | } |
| 437 | |
| 438 | def test_pbkdf2_hmac(self): |
| 439 | for digest_name, results in self.pbkdf2_results.items(): |
| 440 | for i, vector in enumerate(self.pbkdf2_test_vectors): |
| 441 | password, salt, rounds, dklen = vector |
| 442 | expected, overwrite_dklen = results[i] |
| 443 | if overwrite_dklen: |
| 444 | dklen = overwrite_dklen |
| 445 | out = hashlib.pbkdf2_hmac( |
| 446 | digest_name, password, salt, rounds, dklen) |
| 447 | self.assertEqual(out, expected, |
| 448 | (digest_name, password, salt, rounds, dklen)) |
| 449 | |
| 450 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 451 | def test_main(): |
Benjamin Peterson | 48f2e99 | 2014-05-31 13:26:22 -0700 | [diff] [blame] | 452 | test_support.run_unittest(HashLibTestCase, KDFTests) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 453 | |
| 454 | if __name__ == "__main__": |
| 455 | test_main() |