Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from hashlib import sha1, new
- import os
- from secrets import randbelow
- from concurrent.futures import ProcessPoolExecutor
- from itertools import takewhile
- from functools import partial
- from operator import ne
- from random import shuffle, choices
- RAND_SIZE = 128
- N = 4 * 10 ** 7
- def my_hash(s, k=-1):
- return int(sha1(s).hexdigest(), base=16)
- #return int(sha1(s).hexdigest(), base=16) >> (160 - k - 1)
- template_message = "Прошу перевести 8000 руб 00 коп на счет №0101010{}. Клиент {}.RAND="
- salt = b'=' + os.urandom(128)
- case = 13
- message = template_message.format(1, case).encode() + str(randbelow(10**6)).encode() + salt
- new_message_template = template_message.format(1, case).encode()
- message_hash = my_hash(message)
- message_template = template_message.format(2, case).encode()
- new_hashes = [my_hash(new_message_template + str(i).encode() + salt)
- for i in range(N)]
- def calculate_collision1_for(k):
- message_hash_k = message_hash >> (160 - k - 1)
- #shuffle(new_hashes)
- return len(list(takewhile(partial(ne, message_hash_k),
- (h >> (160 - k - 1) for h in choices(new_hashes, k=N))))) + 1
- with ProcessPoolExecutor(max_workers=3) as pool:
- print(*zip(range(1, 25), pool.map(calculate_collision1_for, range(1, 25))), sep='\n')
- hashes = [my_hash(message_template + str(i).encode() + salt)
- for i in range(N)]
- def calculate_collision2_for(k):
- s1 = set()
- s2 = set()
- shuffle(hashes)
- shuffle(new_hashes)
- for h1, h2, i in zip(hashes, new_hashes, range(1, N+1)):
- h1 = h1 >> (160 - k - 1)
- h2 = h2 >> (160 - k - 1)
- if h1 in s2 or h2 in s1:
- print(k, i)
- return i
- s1.add(h1)
- s2.add(h2)
- return -1
- with ProcessPoolExecutor(max_workers=3) as pool:
- 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