annotate Lib/test/test_hashlib.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 4d36e56b56d9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1 # Test hashlib module
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
2 #
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
3 # $Id$
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
4 #
59834
9eabba6ded09 Merged revisions 77120,77151,77155,77209,77229,77256,77317,77331,77333,77359-77360,77382,77561,77570 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49217
diff changeset
5 # Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
6 # Licensed to PSF under a Contributor Agreement.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
7 #
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
8
58162
b05743ce5e94 add a test about hashing array.array
Benjamin Peterson <benjamin@python.org>
parents: 58106
diff changeset
9 import array
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
10 import hashlib
58162
b05743ce5e94 add a test about hashing array.array
Benjamin Peterson <benjamin@python.org>
parents: 58106
diff changeset
11 import itertools
58106
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
12 import sys
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
13 try:
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
14 import threading
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
15 except ImportError:
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
16 threading = None
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
17 import unittest
58106
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
18 import warnings
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
19 from test import test_support
49218
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 35375
diff changeset
20 from test.test_support import _4G, precisionbigmemtest
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
21
58106
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
22 # Were we compiled --with-pydebug or with #define Py_DEBUG?
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
23 COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount')
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
24
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
25
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
26 def hexstr(s):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
27 import string
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
28 h = string.hexdigits
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
29 r = ''
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
30 for c in s:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
31 i = ord(c)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
32 r = r + h[(i >> 4) & 0xF] + h[i & 0xF]
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
33 return r
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
34
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
35
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
36 class HashLibTestCase(unittest.TestCase):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
37 supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1',
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
38 'sha224', 'SHA224', 'sha256', 'SHA256',
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
39 'sha384', 'SHA384', 'sha512', 'SHA512' )
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
40
58106
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
41 _warn_on_extension_import = COMPILED_WITH_PYDEBUG
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
42
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
43 def _conditional_import_module(self, module_name):
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
44 """Import a module and return a reference to it or None on failure."""
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
45 try:
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
46 exec('import '+module_name)
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
47 except ImportError, error:
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
48 if self._warn_on_extension_import:
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
49 warnings.warn('Did a C extension fail to compile? %s' % error)
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
50 return locals().get(module_name)
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
51
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
52 def __init__(self, *args, **kwargs):
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
53 algorithms = set()
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
54 for algorithm in self.supported_hash_names:
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
55 algorithms.add(algorithm.lower())
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
56 self.constructors_to_test = {}
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
57 for algorithm in algorithms:
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
58 self.constructors_to_test[algorithm] = set()
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
59
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
60 # For each algorithm, test the direct constructor and the use
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
61 # of hashlib.new given the algorithm name.
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
62 for algorithm, constructors in self.constructors_to_test.items():
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
63 constructors.add(getattr(hashlib, algorithm))
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
64 def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm):
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
65 if data is None:
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
66 return hashlib.new(_alg)
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
67 return hashlib.new(_alg, data)
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
68 constructors.add(_test_algorithm_via_hashlib_new)
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
69
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
70 _hashlib = self._conditional_import_module('_hashlib')
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
71 if _hashlib:
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
72 # These two algorithms should always be present when this module
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
73 # is compiled. If not, something was compiled wrong.
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
74 assert hasattr(_hashlib, 'openssl_md5')
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
75 assert hasattr(_hashlib, 'openssl_sha1')
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
76 for algorithm, constructors in self.constructors_to_test.items():
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
77 constructor = getattr(_hashlib, 'openssl_'+algorithm, None)
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
78 if constructor:
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
79 constructors.add(constructor)
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
80
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
81 _md5 = self._conditional_import_module('_md5')
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
82 if _md5:
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
83 self.constructors_to_test['md5'].add(_md5.new)
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
84 _sha = self._conditional_import_module('_sha')
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
85 if _sha:
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
86 self.constructors_to_test['sha1'].add(_sha.new)
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
87 _sha256 = self._conditional_import_module('_sha256')
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
88 if _sha256:
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
89 self.constructors_to_test['sha224'].add(_sha256.sha224)
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
90 self.constructors_to_test['sha256'].add(_sha256.sha256)
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
91 _sha512 = self._conditional_import_module('_sha512')
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
92 if _sha512:
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
93 self.constructors_to_test['sha384'].add(_sha512.sha384)
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
94 self.constructors_to_test['sha512'].add(_sha512.sha512)
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
95
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
96 super(HashLibTestCase, self).__init__(*args, **kwargs)
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
97
58162
b05743ce5e94 add a test about hashing array.array
Benjamin Peterson <benjamin@python.org>
parents: 58106
diff changeset
98 def test_hash_array(self):
b05743ce5e94 add a test about hashing array.array
Benjamin Peterson <benjamin@python.org>
parents: 58106
diff changeset
99 a = array.array("b", range(10))
b05743ce5e94 add a test about hashing array.array
Benjamin Peterson <benjamin@python.org>
parents: 58106
diff changeset
100 constructors = self.constructors_to_test.itervalues()
b05743ce5e94 add a test about hashing array.array
Benjamin Peterson <benjamin@python.org>
parents: 58106
diff changeset
101 for cons in itertools.chain.from_iterable(constructors):
b05743ce5e94 add a test about hashing array.array
Benjamin Peterson <benjamin@python.org>
parents: 58106
diff changeset
102 c = cons(a)
b05743ce5e94 add a test about hashing array.array
Benjamin Peterson <benjamin@python.org>
parents: 58106
diff changeset
103 c.hexdigest()
b05743ce5e94 add a test about hashing array.array
Benjamin Peterson <benjamin@python.org>
parents: 58106
diff changeset
104
59211
e1b70c6df9e7 Adds the hashlib.algorithms attribute. See issue7418.
Gregory P. Smith <greg@mad-scientist.com>
parents: 58817
diff changeset
105 def test_algorithms_attribute(self):
e1b70c6df9e7 Adds the hashlib.algorithms attribute. See issue7418.
Gregory P. Smith <greg@mad-scientist.com>
parents: 58817
diff changeset
106 self.assertEqual(hashlib.algorithms,
e1b70c6df9e7 Adds the hashlib.algorithms attribute. See issue7418.
Gregory P. Smith <greg@mad-scientist.com>
parents: 58817
diff changeset
107 tuple([_algo for _algo in self.supported_hash_names if
e1b70c6df9e7 Adds the hashlib.algorithms attribute. See issue7418.
Gregory P. Smith <greg@mad-scientist.com>
parents: 58817
diff changeset
108 _algo.islower()]))
e1b70c6df9e7 Adds the hashlib.algorithms attribute. See issue7418.
Gregory P. Smith <greg@mad-scientist.com>
parents: 58817
diff changeset
109
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
110 def test_unknown_hash(self):
77838
49dee01d72f9 Issue #15219: Fix a reference leak when hashlib.new() is called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 70121
diff changeset
111 self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam')
49dee01d72f9 Issue #15219: Fix a reference leak when hashlib.new() is called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 70121
diff changeset
112 self.assertRaises(TypeError, hashlib.new, 1)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
113
70121
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
114 def test_get_builtin_constructor(self):
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
115 get_builtin_constructor = hashlib.__dict__[
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
116 '__get_builtin_constructor']
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
117 self.assertRaises(ValueError, get_builtin_constructor, 'test')
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
118 try:
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
119 import _md5
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
120 except ImportError:
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
121 pass
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
122 # This forces an ImportError for "import _md5" statements
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
123 sys.modules['_md5'] = None
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
124 try:
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
125 self.assertRaises(ValueError, get_builtin_constructor, 'md5')
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
126 finally:
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
127 if '_md5' in locals():
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
128 sys.modules['_md5'] = _md5
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
129 else:
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
130 del sys.modules['_md5']
78237
21691763ab3b Consistently raise a TypeError when a non str is passed to hashlib.new
Gregory P. Smith <greg@krypto.org>
parents: 77838
diff changeset
131 self.assertRaises(TypeError, get_builtin_constructor, 3)
70121
572fbd9ca28f Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
132
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
133 def test_hexdigest(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
134 for name in self.supported_hash_names:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
135 h = hashlib.new(name)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54085
diff changeset
136 self.assertTrue(hexstr(h.digest()) == h.hexdigest())
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
137
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
138 def test_large_update(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
139 aas = 'a' * 128
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
140 bees = 'b' * 127
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
141 cees = 'c' * 126
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
142 abcs = aas + bees + cees
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
143
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
144 for name in self.supported_hash_names:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
145 m1 = hashlib.new(name)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
146 m1.update(aas)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
147 m1.update(bees)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
148 m1.update(cees)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
149
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
150 m2 = hashlib.new(name)
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
151 m2.update(abcs)
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
152 self.assertEqual(m1.digest(), m2.digest(), name+' update problem.')
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
153
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
154 m3 = hashlib.new(name, abcs)
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
155 self.assertEqual(m1.digest(), m3.digest(), name+' new problem.')
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
156
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
157 def check(self, name, data, digest):
58106
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
158 constructors = self.constructors_to_test[name]
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
159 # 2 is for hashlib.name(...) and hashlib.new(name, ...)
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
160 self.assertGreaterEqual(len(constructors), 2)
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
161 for hash_object_constructor in constructors:
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
162 computed = hash_object_constructor(data).hexdigest()
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
163 self.assertEqual(
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
164 computed, digest,
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
165 "Hash algorithm %s constructed using %s returned hexdigest"
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
166 " %r for %d byte input data that should have hashed to %r."
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
167 % (name, hash_object_constructor,
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
168 computed, len(data), digest))
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
169
78975
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
170 def check_update(self, name, data, digest):
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
171 constructors = self.constructors_to_test[name]
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
172 # 2 is for hashlib.name(...) and hashlib.new(name, ...)
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
173 self.assertGreaterEqual(len(constructors), 2)
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
174 for hash_object_constructor in constructors:
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
175 h = hash_object_constructor()
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
176 h.update(data)
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
177 computed = h.hexdigest()
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
178 self.assertEqual(
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
179 computed, digest,
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
180 "Hash algorithm %s using %s when updated returned hexdigest"
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
181 " %r for %d byte input data that should have hashed to %r."
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
182 % (name, hash_object_constructor,
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
183 computed, len(data), digest))
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
184
58101
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
185 def check_unicode(self, algorithm_name):
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
186 # Unicode objects are not allowed as input.
58101
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
187 expected = hashlib.new(algorithm_name, str(u'spam')).hexdigest()
58106
91a57cc0020e Import all implementations of the hash algorithms (OpenSSL & builtin) and run
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
188 self.check(algorithm_name, u'spam', expected)
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
189
58101
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
190 def test_unicode(self):
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
191 # In python 2.x unicode is auto-encoded to the system default encoding
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
192 # when passed to hashlib functions.
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
193 self.check_unicode('md5')
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
194 self.check_unicode('sha1')
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
195 self.check_unicode('sha224')
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
196 self.check_unicode('sha256')
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
197 self.check_unicode('sha384')
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
198 self.check_unicode('sha512')
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
199
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
200 def test_case_md5_0(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
201 self.check('md5', '', 'd41d8cd98f00b204e9800998ecf8427e')
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
202
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
203 def test_case_md5_1(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
204 self.check('md5', 'abc', '900150983cd24fb0d6963f7d28e17f72')
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
205
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
206 def test_case_md5_2(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
207 self.check('md5', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
208 'd174ab98d277d9f5a5611c2c9f419d9f')
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
209
49218
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 35375
diff changeset
210 @precisionbigmemtest(size=_4G + 5, memuse=1)
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 35375
diff changeset
211 def test_case_md5_huge(self, size):
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 35375
diff changeset
212 if size == _4G + 5:
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 35375
diff changeset
213 try:
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 35375
diff changeset
214 self.check('md5', 'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d')
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 35375
diff changeset
215 except OverflowError:
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 35375
diff changeset
216 pass # 32-bit arch
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 35375
diff changeset
217
78975
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
218 @precisionbigmemtest(size=_4G + 5, memuse=1)
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
219 def test_case_md5_huge_update(self, size):
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
220 if size == _4G + 5:
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
221 try:
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
222 self.check_update('md5', 'A'*size,
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
223 'c9af2dff37468ce5dfee8f2cfc0a9c6d')
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
224 except OverflowError:
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
225 pass # 32-bit arch
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
226
49218
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 35375
diff changeset
227 @precisionbigmemtest(size=_4G - 1, memuse=1)
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 35375
diff changeset
228 def test_case_md5_uintmax(self, size):
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 35375
diff changeset
229 if size == _4G - 1:
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 35375
diff changeset
230 try:
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 35375
diff changeset
231 self.check('md5', 'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3')
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 35375
diff changeset
232 except OverflowError:
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 35375
diff changeset
233 pass # 32-bit arch
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
234
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
235 # use the three examples from Federal Information Processing Standards
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
236 # Publication 180-1, Secure Hash Standard, 1995 April 17
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
237 # http://www.itl.nist.gov/div897/pubs/fip180-1.htm
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
238
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
239 def test_case_sha1_0(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
240 self.check('sha1', "",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
241 "da39a3ee5e6b4b0d3255bfef95601890afd80709")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
242
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
243 def test_case_sha1_1(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
244 self.check('sha1', "abc",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
245 "a9993e364706816aba3e25717850c26c9cd0d89d")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
246
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
247 def test_case_sha1_2(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
248 self.check('sha1', "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
249 "84983e441c3bd26ebaae4aa1f95129e5e54670f1")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
250
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
251 def test_case_sha1_3(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
252 self.check('sha1', "a" * 1000000,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
253 "34aa973cd4c4daa4f61eeb2bdbad27316534016f")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
254
78974
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78237
diff changeset
255 @precisionbigmemtest(size=_4G + 5, memuse=1)
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78237
diff changeset
256 def test_case_sha1_huge(self, size):
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78237
diff changeset
257 if size == _4G + 5:
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78237
diff changeset
258 try:
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78237
diff changeset
259 self.check('sha1', 'A'*size,
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78237
diff changeset
260 '87d745c50e6b2879ffa0fb2c930e9fbfe0dc9a5b')
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78237
diff changeset
261 except OverflowError:
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78237
diff changeset
262 pass # 32-bit arch
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
263
78975
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
264 @precisionbigmemtest(size=_4G + 5, memuse=1)
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
265 def test_case_sha1_huge_update(self, size):
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
266 if size == _4G + 5:
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
267 try:
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
268 self.check_update('sha1', 'A'*size,
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
269 '87d745c50e6b2879ffa0fb2c930e9fbfe0dc9a5b')
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
270 except OverflowError:
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
271 pass # 32-bit arch
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
272
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
273 # use the examples from Federal Information Processing Standards
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
274 # Publication 180-2, Secure Hash Standard, 2002 August 1
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
275 # http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
276
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
277 def test_case_sha224_0(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
278 self.check('sha224', "",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
279 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
280
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
281 def test_case_sha224_1(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
282 self.check('sha224', "abc",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
283 "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
284
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
285 def test_case_sha224_2(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
286 self.check('sha224',
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
287 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
288 "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
289
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
290 def test_case_sha224_3(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
291 self.check('sha224', "a" * 1000000,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
292 "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
293
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
294
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
295 def test_case_sha256_0(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
296 self.check('sha256', "",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
297 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
298
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
299 def test_case_sha256_1(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
300 self.check('sha256', "abc",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
301 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
302
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
303 def test_case_sha256_2(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
304 self.check('sha256',
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
305 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
306 "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
307
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
308 def test_case_sha256_3(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
309 self.check('sha256', "a" * 1000000,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
310 "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
311
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
312
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
313 def test_case_sha384_0(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
314 self.check('sha384', "",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
315 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
316 "274edebfe76f65fbd51ad2f14898b95b")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
317
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
318 def test_case_sha384_1(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
319 self.check('sha384', "abc",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
320 "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
321 "8086072ba1e7cc2358baeca134c825a7")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
322
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
323 def test_case_sha384_2(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
324 self.check('sha384',
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
325 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
326 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
327 "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
328 "fcc7c71a557e2db966c3e9fa91746039")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
329
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
330 def test_case_sha384_3(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
331 self.check('sha384', "a" * 1000000,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
332 "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
333 "07b8b3dc38ecc4ebae97ddd87f3d8985")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
334
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
335
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
336 def test_case_sha512_0(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
337 self.check('sha512', "",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
338 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
339 "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
340
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
341 def test_case_sha512_1(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
342 self.check('sha512', "abc",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
343 "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
344 "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
345
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
346 def test_case_sha512_2(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
347 self.check('sha512',
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
348 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
349 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
350 "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
351 "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
352
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
353 def test_case_sha512_3(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
354 self.check('sha512', "a" * 1000000,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
355 "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
356 "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
357
61039
b88aa13ceaf2 Issue #7449, part 6: fix test_hashlib for missing threading module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59211
diff changeset
358 @unittest.skipUnless(threading, 'Threading required for this test.')
b88aa13ceaf2 Issue #7449, part 6: fix test_hashlib for missing threading module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59211
diff changeset
359 @test_support.reap_threads
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
360 def test_threaded_hashing(self):
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
361 # Updating the same hash object from several threads at once
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
362 # using data chunk sizes containing the same byte sequences.
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
363 #
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
364 # If the internal locks are working to prevent multiple
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
365 # updates on the same object from running at once, the resulting
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
366 # hash will be the same as doing it single threaded upfront.
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
367 hasher = hashlib.sha1()
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
368 num_threads = 5
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
369 smallest_data = 'swineflu'
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
370 data = smallest_data*200000
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
371 expected_hash = hashlib.sha1(data*num_threads).hexdigest()
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
372
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
373 def hash_in_chunks(chunk_size, event):
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
374 index = 0
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
375 while index < len(data):
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
376 hasher.update(data[index:index+chunk_size])
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
377 index += chunk_size
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
378 event.set()
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
379
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
380 events = []
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
381 for threadnum in xrange(num_threads):
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
382 chunk_size = len(data) // (10**threadnum)
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
383 assert chunk_size > 0
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
384 assert chunk_size % len(smallest_data) == 0
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
385 event = threading.Event()
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
386 events.append(event)
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
387 threading.Thread(target=hash_in_chunks,
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
388 args=(chunk_size, event)).start()
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
389
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
390 for event in events:
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
391 event.wait()
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
392
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
393 self.assertEqual(expected_hash, hasher.hexdigest())
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
394
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
395 def test_main():
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
396 test_support.run_unittest(HashLibTestCase)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
397
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
398 if __name__ == "__main__":
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
399 test_main()