Advertisement
Guest User

Untitled

a guest
May 20th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. import hashlib
  2. import random
  3. import string
  4.  
  5. #direkt iz wikipedije
  6. #https://en.wikipedia.org/wiki/Cycle_detection#Floyd%27s_Tortoise_and_Hare
  7. def floyd(f, x0):
  8.     # Main phase of algorithm: finding a repetition x_i = x_2i.
  9.     # The hare moves twice as quickly as the tortoise and
  10.     # the distance between them increases by 1 at each step.
  11.     # Eventually they will both be inside the cycle and then,
  12.     # at some point, the distance between them will be
  13.     # divisible by the period λ.
  14.     tortoise = f(x0) # f(x0) is the element/node next to x0.
  15.     hare = f(f(x0))
  16.     while tortoise != hare:
  17.         tortoise = f(tortoise)
  18.         hare = f(f(hare))
  19.  
  20.     # At this point the tortoise position, ν, which is also equal
  21.     # to the distance between hare and tortoise, is divisible by
  22.     # the period λ. So hare moving in circle one step at a time,
  23.     # and tortoise (reset to x0) moving towards the circle, will
  24.     # intersect at the beginning of the circle. Because the
  25.     # distance between them is constant at 2ν, a multiple of λ,
  26.     # they will agree as soon as the tortoise reaches index μ.
  27.  
  28.     # Find the position μ of first repetition.    
  29.     mu = 0
  30.     tortoise = x0
  31.     while tortoise != hare:
  32.         tortoise = f(tortoise)
  33.         hare = f(hare)   # Hare and tortoise move at same speed
  34.         mu += 1
  35.  
  36.     # Find the length of the shortest cycle starting from x_μ
  37.     # The hare moves one step at a time while tortoise is still.
  38.     # lam is incremented until λ is found.
  39.     lam = 1
  40.     hare = f(tortoise)
  41.     while tortoise != hare:
  42.         hare = f(hare)
  43.         lam += 1
  44.  
  45.     return lam, mu
  46.  
  47. def sha1_44(x):
  48.     h = hashlib.sha1(x).hexdigest()[:11]
  49.     h += '0'
  50.     return bytes.fromhex(h)
  51.  
  52. b = b"lala"
  53.  
  54. lam, mu = floyd(sha1_44, b)
  55. print("zacetek zanke na iteraciji: ", mu)
  56. print("dolzina zanke: ", lam)
  57. print(lam, mu)
  58.  
  59. #zapeljemo se do zadnjega člena, ki ni v zanki
  60. for _ in range(mu-1):
  61.     b = sha1_44(b)
  62.    
  63. w1 = b
  64. print(bytes.hex(w1), '->', bytes.hex(sha1_44(w1)))
  65.  
  66. #vstopimo v zanko
  67. #in se zapeljemo do "zadnjega" člena v zanki, ki bo imel enak hash kot prvi pred zanko
  68. for _ in range(lam):
  69.     b = sha1_44(b)
  70.    
  71. w2 = b
  72. print(bytes.hex(w2), '->', bytes.hex(sha1_44(w2)))
  73.  
  74. f = open("trk.txt", 'w')
  75. f.write(bytes.hex(w1))
  76. f.write('\n')
  77. f.write(bytes.hex(w2))
  78. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement