-
Notifications
You must be signed in to change notification settings - Fork 5
/
gen_test_data.py
51 lines (39 loc) · 1.22 KB
/
gen_test_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import hashlib
import os
def leading_zeros(num):
if num == 0:
return 8
leading_zeros = 0
while num & 0b10000000 == 0:
leading_zeros += 1
num = num << 1
num = num & 0b11111111
return leading_zeros
def total_leading_zeros(hash):
to_return = 0
for byte in hash:
l_zeros = leading_zeros(byte)
to_return += l_zeros
if l_zeros < 8:
break
return to_return
def to_hex_list(bt: bytes) -> list:
r = []
for b in bt:
r.append(f'0x{hex(b)[2:].upper()}')
return r
def gen(hash, difficulty):
difficulty = total_leading_zeros(difficulty)
for i in range(1000):
pow = b'' + os.urandom(10)
hasher = hashlib.sha256()
hasher.update(hash)
hasher.update(pow)
generated_hash = hasher.digest()
ghash_leadin_zeros = total_leading_zeros(generated_hash)
if ghash_leadin_zeros >= difficulty:
print(', '.join(to_hex_list(pow)), True)
else:
print(', '.join(to_hex_list(pow)), False)
gen(hashlib.sha256(b'text').digest(),
b'\x0F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF')