-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_attack.py
60 lines (42 loc) · 1.7 KB
/
hash_attack.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
52
53
54
55
56
57
58
import hashlib
hash_table = {}
# dummy = "7a94d9a9"
def iterate_nonce(input_value, nonce_range):
nonce = 0
while nonce <= nonce_range:
# Combine the input value and nonce
data = input_value + str(nonce)
# Calculate the hash value, truncate it
hash_value = hashlib.md5(data.encode()).hexdigest()[:8]
collision_found = search_hash_table(hash_value)
if(collision_found):
print("Collision part 1: " + collision_found + ": " + hash_value)
print("Collision part 2: " + data + ": " + hash_value)
# print (data)
if data in hash_table:
print("Input already exists in the hash table.")
else:
hash_table[data] = hash_value
# print("Hash stored successfully.")
# Increment the nonce
nonce += 1
def search_hash_table(value):
for key, stored_value in hash_table.items():
if stored_value == value:
return key # Return the key associated with the value
return None # Value not found in the hash table
# Example usage
iterate_nonce("nakamoto", 2 ** 32)
# print()
# print("nakamoto0:" + " " + hash_table["nakamoto0"])
# print("nakamoto1:" + " " + hash_table["nakamoto1"])
# print("nakamoto2:" + " " + hash_table["nakamoto2"])
# print("nakamoto3:" + " " + hash_table["nakamoto3"])
# print("nakamoto4:" + " " + hash_table["nakamoto4"])
# print("nakamoto5:" + " " + hash_table["nakamoto5"])
# print("nakamoto6:" + " " + hash_table["nakamoto6"])
# print("nakamoto7:" + " " + hash_table["nakamoto7"])
# print("nakamoto8:" + " " + hash_table["nakamoto8"])
# print("nakamoto9:" + " " + hash_table["nakamoto9"])
# print()
# print(search_hash_table(dummy))