73 lines
1.9 KiB
Python
73 lines
1.9 KiB
Python
#-*-encoding:utf-8-*-
|
|
|
|
from hashlib import md5
|
|
from binascii import hexlify
|
|
import datetime
|
|
|
|
renmich_1 = 'renmich1123376350383621786626'.encode();
|
|
renmich_2 = 'renmich3700873222361195459231'.encode();
|
|
|
|
my_input = 'rabanbar'.encode();
|
|
|
|
def rmhash(input,n=1):
|
|
tmp = input;
|
|
for i in range(n):
|
|
x = md5();
|
|
x.update(tmp);
|
|
y = md5();
|
|
y.update(x.digest())
|
|
tmp = y.digest()[:7]
|
|
return tmp;
|
|
|
|
def to_string(b):
|
|
return hexlify(b).decode();
|
|
|
|
def find_colision(input):
|
|
begin_time = datetime.datetime.now()
|
|
|
|
tortoise=input;
|
|
hare=input;
|
|
i=0;
|
|
|
|
dlugosc_cyklu = 1;
|
|
pierwsze_wystapienie_cyklu = 0;
|
|
|
|
while i==0 or tortoise!=hare:
|
|
i+=1;
|
|
|
|
tortoise=rmhash(tortoise);
|
|
|
|
hare=rmhash(hare);
|
|
hare=rmhash(hare);
|
|
|
|
print("{}: zolw={} zajac={} ".format(i,to_string(tortoise), to_string(hare)))
|
|
|
|
# Zolw na poczatek
|
|
tortoise = input;
|
|
|
|
while tortoise != hare:
|
|
previous_tortoise = tortoise;
|
|
tortoise = rmhash(tortoise);
|
|
hare = rmhash(hare)
|
|
pierwsze_wystapienie_cyklu += 1;
|
|
|
|
#Przesuwam zajaca o jedno pole do przodu
|
|
hare=rmhash(tortoise);
|
|
|
|
while tortoise!=hare:
|
|
previous_hare = hare;
|
|
hare=rmhash(hare);
|
|
dlugosc_cyklu+=1;
|
|
|
|
print("Zajac jest na polu {} a byl na {}".format(to_string(hare),to_string(previous_hare)))
|
|
print("Zolw jest na polu {} a byl na {}".format(to_string(tortoise),to_string(previous_tortoise)))
|
|
print("Dlugosc cyklu: {}; Pierwsze wystapienie cyklu {}".format(dlugosc_cyklu,pierwsze_wystapienie_cyklu))
|
|
|
|
collision_a = rmhash(input,pierwsze_wystapienie_cyklu-1);
|
|
collision_b = rmhash(input, pierwsze_wystapienie_cyklu + dlugosc_cyklu-1);
|
|
|
|
print("Hash ciagow: {}, {} to {}".format(to_string(collision_a),to_string(collision_b),to_string(rmhash(collision_a))))
|
|
|
|
print("Poczatek: {} Koniec: {}".format(begin_time,datetime.datetime.now()))
|
|
|
|
return(collision_a,collision_b); |