Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.94 KB | None | 0 0
  1. from hashlib import sha1, new
  2. import os
  3. from secrets import randbelow
  4. from concurrent.futures import ProcessPoolExecutor
  5. from itertools import takewhile
  6. from functools import partial
  7. from operator import ne
  8. from random import shuffle, choices
  9.  
  10.  
  11. RAND_SIZE = 128
  12. N = 4 * 10 ** 7
  13.  
  14. def my_hash(s, k=-1):
  15.     return int(sha1(s).hexdigest(), base=16)
  16.     #return int(sha1(s).hexdigest(), base=16) >> (160 - k - 1)
  17.  
  18.  
  19. template_message = "Прошу перевести 8000 руб 00 коп на счет №0101010{}. Клиент {}.RAND="
  20. salt = b'=' + os.urandom(128)
  21. case = 13
  22.  
  23. message = template_message.format(1, case).encode() + str(randbelow(10**6)).encode() + salt
  24. new_message_template = template_message.format(1, case).encode()
  25. message_hash = my_hash(message)
  26.  
  27. message_template = template_message.format(2, case).encode()
  28. new_hashes = [my_hash(new_message_template + str(i).encode() + salt)
  29.               for i in range(N)]
  30.  
  31. def calculate_collision1_for(k):
  32.     message_hash_k = message_hash >> (160 - k - 1)
  33.     #shuffle(new_hashes)
  34.     return len(list(takewhile(partial(ne, message_hash_k),
  35.                               (h >> (160 - k - 1) for h in choices(new_hashes, k=N))))) + 1
  36.  
  37. with ProcessPoolExecutor(max_workers=3) as pool:
  38.     print(*zip(range(1, 25), pool.map(calculate_collision1_for, range(1, 25))), sep='\n')
  39.  
  40.  
  41. hashes = [my_hash(message_template + str(i).encode() + salt)
  42.               for i in range(N)]
  43.  
  44. def calculate_collision2_for(k):
  45.     s1 = set()
  46.     s2 = set()
  47.     shuffle(hashes)
  48.     shuffle(new_hashes)
  49.     for h1, h2, i in zip(hashes, new_hashes, range(1, N+1)):
  50.         h1 = h1 >> (160 - k - 1)
  51.         h2 = h2 >> (160 - k - 1)
  52.         if h1 in s2 or h2 in s1:
  53.             print(k, i)
  54.             return i
  55.         s1.add(h1)
  56.         s2.add(h2)
  57.     return -1
  58.  
  59. with ProcessPoolExecutor(max_workers=3) as pool:
  60.     print(*zip(range(1, 49), pool.map(calculate_collision2_for, range(1, 49))), sep='\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement