[166] | 1 | #!/usr/bin/env python3 |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | from rainbow_config import * |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | def find_match_in_table(table, needle, R): |
---|
| 9 | ret = [] |
---|
| 10 | for i in range(0, len(R)): |
---|
| 11 | end = gen_chain_from(needle, i, R) |
---|
| 12 | for entry in table: |
---|
| 13 | if entry[1] == end and matches(i, table, needle, entry, R): |
---|
| 14 | ret.append([entry[0], i]) |
---|
| 15 | return ret |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | def mk_array(n, val): |
---|
| 19 | a = [] |
---|
| 20 | for i in range(0, n): |
---|
| 21 | a.append(val) |
---|
| 22 | return a |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | def find_match(tables, needle, Rarray): |
---|
| 26 | matches = [] |
---|
| 27 | # get matches from all tables |
---|
| 28 | for i in range(0, len(Rarray)): |
---|
| 29 | m = find_match_in_table(tables[i], needle, Rarray[i]) |
---|
| 30 | if len(m) == 0: |
---|
| 31 | continue |
---|
| 32 | matches.append(m) |
---|
| 33 | # group matches by found secret value |
---|
| 34 | d = dict() |
---|
| 35 | for i in range(0, len(matches)): |
---|
| 36 | for match in matches[i]: |
---|
| 37 | secret = match[0] |
---|
| 38 | index = match[1] |
---|
| 39 | if not(secret in d): |
---|
| 40 | d[secret] = mk_array(len(matches), []) |
---|
| 41 | d[secret][index].append(i) |
---|
| 42 | |
---|
| 43 | # return the secrets with most matches |
---|
| 44 | ret = [] |
---|
| 45 | for secret in d.keys(): |
---|
| 46 | if len(d[secret][index]) == len(matches): |
---|
| 47 | ret.append(secret) |
---|
| 48 | return ret |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | if __name__ == "__main__": |
---|
| 53 | Rarray = [] |
---|
| 54 | tables = [] |
---|
| 55 | lines = [] |
---|
| 56 | for l in sys.stdin: |
---|
| 57 | lines.append(l.strip()) |
---|
| 58 | |
---|
| 59 | for i in range(0, nr_tables): |
---|
| 60 | R = gen_rainbow_reductors(i) |
---|
| 61 | Rarray.append(R) |
---|
| 62 | v = generate_table(lines, R) |
---|
| 63 | tables.append(v) |
---|
| 64 | secret = '1234' |
---|
| 65 | chain = [] |
---|
| 66 | chain.append(secret) |
---|
| 67 | for r in Rarray[0]: |
---|
| 68 | h = hashfn(secret) |
---|
| 69 | secret = r(h) |
---|
| 70 | chain.append(h) |
---|
| 71 | chain.append(secret) |
---|
| 72 | print(chain) |
---|
| 73 | print(chain[1], find_match(tables, chain[1], Rarray)) #1st hash |
---|
| 74 | print(chain[3], find_match(tables, chain[3], Rarray)) #2nd hash |
---|
| 75 | |
---|
| 76 | |
---|