Skip to content

Hashes

Hashes

Breaking a Toy Hash Function

Reversing functions

Reverse shift_left xor:

import os

def reverse_right_xor_shift(data, inital_shift, max_size):
	prev = max_size + 1
	current = data

	while prev != current:
		prev = current
		current ^= (prev >> inital_shift) & max_size
		inital_shift *= 2

	return current

def reverse_left_xor_shift(data, inital_shift, max_size):
	prev = max_size + 1
	current = data

	while prev != current:
		prev = current
		current ^= (prev << inital_shift) & max_size
		inital_shift *= 2

	return current

def asint32(i):
	return i & 0xFFFFFFFF

def asint64(i):
	return i & 0xFFFFFFFFFFFFFFFF

test = int.from_bytes(os.urandom(8), 'big')
print(f"test: {hex(test)}")
test1 = test
#test: 0x1923c230dee65408

#Origional Left Shift
test1 ^= asint64(test << 13)
print(f"Left test1[13]: {hex(test1)}")
#Left test1[13]: 0x6165d9ec14675408

#Use the Doubel shift 
test1 = reverse_left_xor_shift(test1, 13, 0xFFFFFFFFFFFFFFFF)
print(f"Left test1: {hex(test1)}")
#Left test1: 0x1923c230dee65408


#Origional Right Shift
test1 ^= asint64(test >> 13)
print(f"Right test1[13]: {hex(test1)}")
#Right test1[13]: 0x19230b2ecf60a33a

#Use the Doubel shift 
test1 = reverse_right_xor_shift(test1, 13, 0xFFFFFFFFFFFFFFFF)
print(f"Right test1: {hex(test1)}")
#Right test1: 0x1923c230dee65408