forked from pag-crypto/cs5435-fa19-hw3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collisions.py
60 lines (50 loc) · 1.96 KB
/
collisions.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
59
60
import siphash
import random
import string
import time
def callhash(hashkey, inval):
return siphash.SipHash_2_4(hashkey, inval).hash()
def ht_hash(hashkey, inval, htsize):
return callhash(hashkey, inval) % htsize
# Put your collision-finding code here.
# Your function should output the colliding strings in a list.
def find_collisions(key, ht_size, num_collisions):
s = "test"
h = ht_hash(key, s.encode('utf-8'), ht_size)
# try to find matching hashes for "test"
letters = string.ascii_lowercase
print("Checking matching hashes for string '{}'; hash '{}'".format(s, h))
colliding_strings = {}
colliding_strings[s] = True
count = 0 # For logging
while len(colliding_strings) < num_collisions:
# Build random 8 byte strings
s_random = ''.join(random.choice(letters) for i in range(8))
# Calculate hash
h_test = ht_hash(key, s_random.encode('utf-8'), ht_size)
# Check if the hash matches ours
if h_test == h:
if s_random not in colliding_strings:
count += 1
print("{}. Found two colliding hashes: {} == {}; Hash: {}".format(count, s, s_random, h))
colliding_strings[s_random] = True
return list(colliding_strings.keys())
# Implement this function, which takes the list of
# collisions and verifies they all have the same
# SipHash output under the given key.
def check_collisions(key, ht_size, colls):
h = ht_hash(key, colls[0].encode('utf-8'), ht_size)
for c in colls:
h_test = ht_hash(key, c.encode('utf-8'), ht_size)
if h_test != h:
return False
return True
if __name__=='__main__':
start_time = time.time()
# Look in the source code of the app to
# find the key used for hashing.
ht_size = 2**16
hash_key = b'\x00'*16
colls = find_collisions(hash_key, ht_size, 20)
print("Collisions: {}".format(colls))
print(check_collisions(hash_key, ht_size, colls))