MCPcopy Index your code
hub / github.com/python/cpython / do_test_threaded_hashing

Method do_test_threaded_hashing

Lib/test/test_hashlib.py:1121–1161  ·  view source on GitHub ↗
(self, constructor, is_shake)

Source from the content-addressed store, hash-verified

1119 self.do_test_threaded_hashing(constructor, is_shake)
1120
1121 def do_test_threaded_hashing(self, constructor, is_shake):
1122 # Updating the same hash object from several threads at once
1123 # using data chunk sizes containing the same byte sequences.
1124 #
1125 # If the internal locks are working to prevent multiple
1126 # updates on the same object from running at once, the resulting
1127 # hash will be the same as doing it single threaded upfront.
1128
1129 # The data to hash has length s|M|q^N and the chunk size for the i-th
1130 # thread is s|M|q^(N-i), where N is the number of threads, M is a fixed
1131 # message of small length, and s >= 1 and q >= 2 are small integers.
1132 smallest_size, num_threads, s, q = 8, 5, 2, 10
1133
1134 smallest_data = os.urandom(smallest_size)
1135 data = s * smallest_data * (q ** num_threads)
1136
1137 h1 = constructor(usedforsecurity=False)
1138 h2 = constructor(data * num_threads, usedforsecurity=False)
1139
1140 def update(chunk_size):
1141 for index in range(0, len(data), chunk_size):
1142 h1.update(data[index:index + chunk_size])
1143
1144 threads = []
1145 for thread_num in range(num_threads):
1146 # chunk_size = len(data) // (q ** thread_num)
1147 chunk_size = s * smallest_size * q ** (num_threads - thread_num)
1148 self.assertGreater(chunk_size, 0)
1149 self.assertEqual(chunk_size % smallest_size, 0)
1150 thread = threading.Thread(target=update, args=(chunk_size,))
1151 threads.append(thread)
1152
1153 for thread in threads:
1154 thread.start()
1155 for thread in threads:
1156 thread.join()
1157
1158 if is_shake:
1159 self.assertEqual(h1.hexdigest(16), h2.hexdigest(16))
1160 else:
1161 self.assertEqual(h1.hexdigest(), h2.hexdigest())
1162
1163 def test_get_fips_mode(self):
1164 fips_mode = self.is_fips_mode

Callers 2

Calls 7

startMethod · 0.95
joinMethod · 0.95
constructorFunction · 0.85
assertGreaterMethod · 0.80
hexdigestMethod · 0.80
assertEqualMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected