Last active
July 2, 2020 13:06
-
-
Save olivierlemoal/7236ebfe19d289dbc433a5bca867477a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hashlib | |
from multiprocessing import Pool, cpu_count | |
from binascii import unhexlify | |
# Retrieves DATALOCK values with AT^NVRDEX=50502,0,128 | |
# Worst case scenario takes 44s on i7-7700K | |
def test_code(params): | |
text1, text2, min_code, max_code = params | |
for i in range(min_code, max_code): | |
tested_code = bytearray("{:08d}".format(i).encode('ascii')) | |
m = hashlib.sha256() | |
m.update(tested_code) | |
tested_code_256 = m.digest() | |
final_array = tested_code_256 + text2 | |
m = hashlib.sha256() | |
m.update(final_array) | |
final_256 = m.digest() | |
if final_256 == text1: | |
return i | |
def find_v4_code(text1, text2): | |
text1 = unhexlify(text1.replace(' ', '')) | |
text2 = unhexlify(text2.replace(' ', '')) | |
if len(text1) != 32 or len(text2) != 32: | |
print("bad length") | |
return -1 | |
CODE_MAX = 100000000 | |
STEP = int(CODE_MAX / cpu_count()) | |
params = [] | |
for i in range(0, cpu_count()): | |
min_code = STEP * i | |
max_code = STEP * (i + 1) | |
params.append((text1, text2, min_code, max_code)) | |
with Pool(processes=cpu_count()) as pool: | |
for i in pool.imap_unordered(test_code, params, chunksize=1): | |
if i is not None: | |
pool.terminate() | |
return i | |
def test(): | |
import time | |
start = time.time() | |
test1_text1 = "8F 29 FF 8E A8 CA 34 89 78 73 18 BA 9E F5 9C 64 0B A4 DB 81 DC 03 45 6E 72 DA EC 6A 0C 7C 90 65" | |
test1_text2 = "8B 8C F4 B5 AF 0C F2 2C FE E0 F4 46 9C CF 47 95 36 71 1F 1C BF 05 7F 84 AB A9 F2 92 89 33 3C 12" | |
end = time.time() | |
assert find_v4_code(test1_text1, test1_text2) == 0 | |
print(f"test1 OK in {end - start} seconds") | |
start = time.time() | |
test2_text1 = "C3 39 93 98 47 F7 75 08 1E 65 EC 04 38 3A 45 A6 FA AF 46 4B 78 FF 53 FF 79 73 33 90 9D FF F1 6C" | |
test2_text2 = "B8 16 47 98 42 21 26 0E 86 1D A8 C8 E0 C2 0C FC 8F 2E A9 01 9D 58 05 3C 15 43 37 BC 95 66 E4 CF" | |
assert find_v4_code(test2_text1, test2_text2) == 99999999 | |
end = time.time() | |
print(f"test2 OK in {end - start} seconds") | |
# test() | |
text1 = "" | |
text2 = "" | |
print(find_v4_code(text1, text2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment