Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4442)

## Delta Between Two Patch Sets: Lib/test/test_hashlib.py

Issue 26798: add BLAKE2 to hashlib
Left Patch Set: Created 3 years, 5 months ago
Right Patch Set: Created 3 years, 1 month ago
 Left: Base Patch Set 1: None Patch Set 2: None Patch Set 3: None Patch Set 4: None Patch Set 5: None Right: Patch Set 1: None Patch Set 2: None Patch Set 3: None Patch Set 4: None Patch Set 5: None
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/hashlib.py ('k') | Makefile.pre.in » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Hide Comments ('s')
LEFTRIGHT
1 # Test hashlib module 1 # Test hashlib module
2 # 2 #
3 # \$Id\$ 3 # \$Id\$
4 # 4 #
5 # Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org) 5 # Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
6 # Licensed to PSF under a Contributor Agreement. 6 # Licensed to PSF under a Contributor Agreement.
7 # 7 #
8 8
9 import array 9 import array
10 import hashlib 10 import hashlib
(...skipping 25 matching lines...) Expand all
36 36
37 def hexstr(s): 37 def hexstr(s):
38 assert isinstance(s, bytes), repr(s) 38 assert isinstance(s, bytes), repr(s)
39 h = "0123456789abcdef" 39 h = "0123456789abcdef"
40 r = '' 40 r = ''
41 for i in s: 41 for i in s:
42 r += h[(i >> 4) & 0xF] + h[i & 0xF] 42 r += h[(i >> 4) & 0xF] + h[i & 0xF]
43 return r 43 return r
44 44
45 45
46 URL = "https://raw.githubusercontent.com/tiran/python_vectors/master/{}.txt"
47
47 vector = os.path.join(support.TEST_HOME_DIR, 'vectors', 49 with support.open_urlresource(URL.format(hash_name)) as f:
48 hash_name + '.txt')
49 with open(vector) as f:
50 for line in f: 50 for line in f:
51 line = line.strip() 51 line = line.strip()
52 if line.startswith('#') or not line: 52 if line.startswith('#') or not line:
53 continue 53 continue
54 parts = line.split(',') 54 parts = line.split(',')
55 parts[0] = bytes.fromhex(parts[0]) 55 parts[0] = bytes.fromhex(parts[0])
56 yield parts 56 yield parts
57 57
58 58
59 class HashLibTestCase(unittest.TestCase): 59 class HashLibTestCase(unittest.TestCase):
60 supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1', 60 supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1',
61 'sha224', 'SHA224', 'sha256', 'SHA256', 61 'sha224', 'SHA224', 'sha256', 'SHA256',
62 'sha384', 'SHA384', 'sha512', 'SHA512') 62 'sha384', 'SHA384', 'sha512', 'SHA512',
63 'blake2b', 'blake2s')
63 64
64 # Issue #14693: fallback modules are always compiled under POSIX 65 # Issue #14693: fallback modules are always compiled under POSIX
65 _warn_on_extension_import = os.name == 'posix' or COMPILED_WITH_PYDEBUG 66 _warn_on_extension_import = os.name == 'posix' or COMPILED_WITH_PYDEBUG
66 67
67 def _conditional_import_module(self, module_name): 68 def _conditional_import_module(self, module_name):
68 """Import a module and return a reference to it or None on failure.""" 69 """Import a module and return a reference to it or None on failure."""
69 try: 70 try:
70 exec('import '+module_name) 71 exec('import '+module_name)
71 except ImportError as error: 72 except ImportError as error:
72 if self._warn_on_extension_import: 73 if self._warn_on_extension_import:
73 warnings.warn('Did a C extension fail to compile? %s' % error) 74 warnings.warn('Did a C extension fail to compile? %s' % error)
74 return locals().get(module_name) 75 return locals().get(module_name)
75 76
76 def __init__(self, *args, **kwargs): 77 def __init__(self, *args, **kwargs):
77 algorithms = set() 78 algorithms = set()
78 for algorithm in self.supported_hash_names: 79 for algorithm in self.supported_hash_names:
80 81
81 _blake2 = self._conditional_import_module('_blake2') 82 _blake2 = self._conditional_import_module('_blake2')
82 if _blake2: 83 if _blake2:
83 algorithms.add('blake2b') 84 algorithms.update({'blake2b', 'blake2s'})
85 85
86 self.constructors_to_test = {} 86 self.constructors_to_test = {}
87 for algorithm in algorithms: 87 for algorithm in algorithms:
88 self.constructors_to_test[algorithm] = set() 88 self.constructors_to_test[algorithm] = set()
89 89
90 # For each algorithm, test the direct constructor and the use 90 # For each algorithm, test the direct constructor and the use
91 # of hashlib.new given the algorithm name. 91 # of hashlib.new given the algorithm name.
92 for algorithm, constructors in self.constructors_to_test.items(): 92 for algorithm, constructors in self.constructors_to_test.items():
94 def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm, **kwa rgs): 94 def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm, **kwa rgs):
(...skipping 90 matching lines...)
185 def test_hexdigest(self): 185 def test_hexdigest(self):
186 for cons in self.hash_constructors: 186 for cons in self.hash_constructors:
187 h = cons() 187 h = cons()
188 self.assertIsInstance(h.digest(), bytes) 188 self.assertIsInstance(h.digest(), bytes)
189 self.assertEqual(hexstr(h.digest()), h.hexdigest()) 189 self.assertEqual(hexstr(h.digest()), h.hexdigest())
190 190
191 def test_name_attribute(self): 191 def test_name_attribute(self):
192 for cons in self.hash_constructors: 192 for cons in self.hash_constructors:
193 h = cons() 193 h = cons()
194 self.assertIsInstance(h.name, str) 194 self.assertIsInstance(h.name, str)
195 #self.assertIn(h.name, self.supported_hash_names) 195 self.assertIn(h.name, self.supported_hash_names)
196 self.assertEqual(h.name, hashlib.new(h.name).name) 196 self.assertEqual(h.name, hashlib.new(h.name).name)
197 197
198 def test_large_update(self): 198 def test_large_update(self):
199 aas = b'a' * 128 199 aas = b'a' * 128
200 bees = b'b' * 127 200 bees = b'b' * 127
201 cees = b'c' * 126 201 cees = b'c' * 126
202 dees = b'd' * 2048 # HASHLIB_GIL_MINSIZE 202 dees = b'd' * 2048 # HASHLIB_GIL_MINSIZE
203 203
204 for cons in self.hash_constructors: 204 for cons in self.hash_constructors:
205 m1 = cons() 205 m1 = cons()
(...skipping 425 matching lines...)
631 (b'password', b'salt', 4096, None), 631 (b'password', b'salt', 4096, None),
632 # too slow, it takes over a minute on a fast CPU. 632 # too slow, it takes over a minute on a fast CPU.
633 #(b'password', b'salt', 16777216, None), 633 #(b'password', b'salt', 16777216, None),
635 4096, -1), 635 4096, -1),
636 (b'pass\0word', b'sa\0lt', 4096, 16), 636 (b'pass\0word', b'sa\0lt', 4096, 16),
637 ] 637 ]
638 638
639 pbkdf2_results = { 639 pbkdf2_results = {
640 "sha1": [ 640 "sha1": [
641 # offical test vectors from RFC 6070 641 # official test vectors from RFC 6070
642 (bytes.fromhex('0c60c80f961f0e71f3a9b524af6012062fe037a6'), None), 642 (bytes.fromhex('0c60c80f961f0e71f3a9b524af6012062fe037a6'), None),
643 (bytes.fromhex('ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957'), None), 643 (bytes.fromhex('ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957'), None),
645 #(bytes.fromhex('eefe3d61cd4da4e4e9945b3d6ba2158c2634e984'), None), 645 #(bytes.fromhex('eefe3d61cd4da4e4e9945b3d6ba2158c2634e984'), None),
646 (bytes.fromhex('3d2eec4fe41c849b80c8d83662c0e44a8b291a964c' 646 (bytes.fromhex('3d2eec4fe41c849b80c8d83662c0e44a8b291a964c'
647 'f2f07038'), 25), 647 'f2f07038'), 25),
648 (bytes.fromhex('56fa6aa75548099dcc37d7f03425e0c3'), None),], 648 (bytes.fromhex('56fa6aa75548099dcc37d7f03425e0c3'), None),],
649 "sha256": [ 649 "sha256": [
650 (bytes.fromhex('120fb6cffcf8b32c43e7225256c4f837' 650 (bytes.fromhex('120fb6cffcf8b32c43e7225256c4f837'
651 'a86548c92ccc35480805987cb70be17b'), None), 651 'a86548c92ccc35480805987cb70be17b'), None),
(...skipping 58 matching lines...)
710 self._test_pbkdf2_hmac(py_hashlib.pbkdf2_hmac) 710 self._test_pbkdf2_hmac(py_hashlib.pbkdf2_hmac)
711 711
712 @unittest.skipUnless(hasattr(c_hashlib, 'pbkdf2_hmac'), 712 @unittest.skipUnless(hasattr(c_hashlib, 'pbkdf2_hmac'),
713 ' test requires OpenSSL > 1.0') 713 ' test requires OpenSSL > 1.0')
714 def test_pbkdf2_hmac_c(self): 714 def test_pbkdf2_hmac_c(self):
715 self._test_pbkdf2_hmac(c_hashlib.pbkdf2_hmac) 715 self._test_pbkdf2_hmac(c_hashlib.pbkdf2_hmac)
716 716
717 717
718 if __name__ == "__main__": 718 if __name__ == "__main__":
719 unittest.main() 719 unittest.main()
LEFTRIGHT

This is Rietveld 894c83f36cb7+