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

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

Issue 26798: add BLAKE2 to hashlib
Left Patch Set: Created 3 years, 3 months ago
Right Patch Set: Created 2 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
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') | Show Comments 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
46 def read_vectors(hash_name): 48 def read_vectors(hash_name):
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 63 'blake2b', 'blake2s')
64 often_supported = {'blake2b', 'blake2s'}
65 64
66 # Issue #14693: fallback modules are always compiled under POSIX 65 # Issue #14693: fallback modules are always compiled under POSIX
67 _warn_on_extension_import = os.name == 'posix' or COMPILED_WITH_PYDEBUG 66 _warn_on_extension_import = os.name == 'posix' or COMPILED_WITH_PYDEBUG
68 67
69 def _conditional_import_module(self, module_name): 68 def _conditional_import_module(self, module_name):
70 """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."""
71 try: 70 try:
72 exec('import '+module_name) 71 exec('import '+module_name)
73 except ImportError as error: 72 except ImportError as error:
74 if self._warn_on_extension_import: 73 if self._warn_on_extension_import:
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 def test_hexdigest(self): 185 def test_hexdigest(self):
187 for cons in self.hash_constructors: 186 for cons in self.hash_constructors:
188 h = cons() 187 h = cons()
189 self.assertIsInstance(h.digest(), bytes) 188 self.assertIsInstance(h.digest(), bytes)
190 self.assertEqual(hexstr(h.digest()), h.hexdigest()) 189 self.assertEqual(hexstr(h.digest()), h.hexdigest())
191 190
192 def test_name_attribute(self): 191 def test_name_attribute(self):
193 for cons in self.hash_constructors: 192 for cons in self.hash_constructors:
194 h = cons() 193 h = cons()
195 self.assertIsInstance(h.name, str) 194 self.assertIsInstance(h.name, str)
196 self.assertIn( 195 self.assertIn(h.name, self.supported_hash_names)
197 h.name,
198 self.often_supported.union(self.supported_hash_names))
199 self.assertEqual(h.name, hashlib.new(h.name).name) 196 self.assertEqual(h.name, hashlib.new(h.name).name)
200 197
201 def test_large_update(self): 198 def test_large_update(self):
202 aas = b'a' * 128 199 aas = b'a' * 128
203 bees = b'b' * 127 200 bees = b'b' * 127
204 cees = b'c' * 126 201 cees = b'c' * 126
205 dees = b'd' * 2048 # HASHLIB_GIL_MINSIZE 202 dees = b'd' * 2048 # HASHLIB_GIL_MINSIZE
206 203
207 for cons in self.hash_constructors: 204 for cons in self.hash_constructors:
208 m1 = cons() 205 m1 = cons()
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 (b'password', b'salt', 4096, None), 631 (b'password', b'salt', 4096, None),
635 # too slow, it takes over a minute on a fast CPU. 632 # too slow, it takes over a minute on a fast CPU.
636 #(b'password', b'salt', 16777216, None), 633 #(b'password', b'salt', 16777216, None),
637 (b'passwordPASSWORDpassword', b'saltSALTsaltSALTsaltSALTsaltSALTsalt', 634 (b'passwordPASSWORDpassword', b'saltSALTsaltSALTsaltSALTsaltSALTsalt',
638 4096, -1), 635 4096, -1),
639 (b'pass\0word', b'sa\0lt', 4096, 16), 636 (b'pass\0word', b'sa\0lt', 4096, 16),
640 ] 637 ]
641 638
642 pbkdf2_results = { 639 pbkdf2_results = {
643 "sha1": [ 640 "sha1": [
644 # offical test vectors from RFC 6070 641 # official test vectors from RFC 6070
645 (bytes.fromhex('0c60c80f961f0e71f3a9b524af6012062fe037a6'), None), 642 (bytes.fromhex('0c60c80f961f0e71f3a9b524af6012062fe037a6'), None),
646 (bytes.fromhex('ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957'), None), 643 (bytes.fromhex('ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957'), None),
647 (bytes.fromhex('4b007901b765489abead49d926f721d065a429c1'), None), 644 (bytes.fromhex('4b007901b765489abead49d926f721d065a429c1'), None),
648 #(bytes.fromhex('eefe3d61cd4da4e4e9945b3d6ba2158c2634e984'), None), 645 #(bytes.fromhex('eefe3d61cd4da4e4e9945b3d6ba2158c2634e984'), None),
649 (bytes.fromhex('3d2eec4fe41c849b80c8d83662c0e44a8b291a964c' 646 (bytes.fromhex('3d2eec4fe41c849b80c8d83662c0e44a8b291a964c'
650 'f2f07038'), 25), 647 'f2f07038'), 25),
651 (bytes.fromhex('56fa6aa75548099dcc37d7f03425e0c3'), None),], 648 (bytes.fromhex('56fa6aa75548099dcc37d7f03425e0c3'), None),],
652 "sha256": [ 649 "sha256": [
653 (bytes.fromhex('120fb6cffcf8b32c43e7225256c4f837' 650 (bytes.fromhex('120fb6cffcf8b32c43e7225256c4f837'
654 'a86548c92ccc35480805987cb70be17b'), None), 651 'a86548c92ccc35480805987cb70be17b'), None),
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 self._test_pbkdf2_hmac(py_hashlib.pbkdf2_hmac) 710 self._test_pbkdf2_hmac(py_hashlib.pbkdf2_hmac)
714 711
715 @unittest.skipUnless(hasattr(c_hashlib, 'pbkdf2_hmac'), 712 @unittest.skipUnless(hasattr(c_hashlib, 'pbkdf2_hmac'),
716 ' test requires OpenSSL > 1.0') 713 ' test requires OpenSSL > 1.0')
717 def test_pbkdf2_hmac_c(self): 714 def test_pbkdf2_hmac_c(self):
718 self._test_pbkdf2_hmac(c_hashlib.pbkdf2_hmac) 715 self._test_pbkdf2_hmac(c_hashlib.pbkdf2_hmac)
719 716
720 717
721 if __name__ == "__main__": 718 if __name__ == "__main__":
722 unittest.main() 719 unittest.main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+