#!/usr/bin/env python3 import sys from rainbow_config import * def find_match_in_table(table, needle, R): ret = [] for i in range(0, len(R)): end = gen_chain_from(needle, i, R) for entry in table: if entry[1] == end and matches(i, table, needle, entry, R): ret.append([entry[0], i]) return ret def mk_array(n, val): a = [] for i in range(0, n): a.append(val) return a def find_match(tables, needle, Rarray): matches = [] # get matches from all tables for i in range(0, len(Rarray)): m = find_match_in_table(tables[i], needle, Rarray[i]) if len(m) == 0: continue matches.append(m) # group matches by found secret value d = dict() for i in range(0, len(matches)): for match in matches[i]: secret = match[0] index = match[1] if not(secret in d): d[secret] = mk_array(len(matches), []) d[secret][index].append(i) # return the secrets with most matches ret = [] for secret in d.keys(): if len(d[secret][index]) == len(matches): ret.append(secret) return ret if __name__ == "__main__": Rarray = [] tables = [] lines = [] for l in sys.stdin: lines.append(l.strip()) for i in range(0, nr_tables): R = gen_rainbow_reductors(i) Rarray.append(R) v = generate_table(lines, R) tables.append(v) secret = '1234' chain = [] chain.append(secret) for r in Rarray[0]: h = hashfn(secret) secret = r(h) chain.append(h) chain.append(secret) print(chain) print(chain[1], find_match(tables, chain[1], Rarray)) #1st hash print(chain[3], find_match(tables, chain[3], Rarray)) #2nd hash