|
| 1 | +POLY = 0xEDB88320 |
| 2 | + |
| 3 | + |
| 4 | +class crc32Cracker: |
| 5 | + def __init__(self): |
| 6 | + self.table = self.table() |
| 7 | + |
| 8 | + def crc32(self, string: str) -> (int, int): |
| 9 | + INIT = 0xFFFFFFFF |
| 10 | + index = None |
| 11 | + for i in string: |
| 12 | + index = (INIT ^ ord(i)) & 0xFF |
| 13 | + INIT = INIT >> 8 ^ self.table[index] |
| 14 | + return INIT, index |
| 15 | + |
| 16 | + def lastIndex(self, crc: int) -> int: |
| 17 | + for i in range(256): |
| 18 | + if crc == self.table[i] >> 24: |
| 19 | + return i |
| 20 | + |
| 21 | + def check(self, high: int, indexes: list) -> (bool, str): |
| 22 | + crc, index = self.crc32(str(high)) |
| 23 | + if index != indexes[3]: |
| 24 | + return False |
| 25 | + low = '' |
| 26 | + for i in range(2, -1, -1): |
| 27 | + num = crc & 0xFF ^ indexes[i] |
| 28 | + if num not in range(48, 58): |
| 29 | + return False |
| 30 | + low += str(num - 48) |
| 31 | + crc = self.table[indexes[i]] ^ crc >> 8 |
| 32 | + return low |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def table(): |
| 36 | + table = [] |
| 37 | + for i in range(256): |
| 38 | + value = i |
| 39 | + for _ in range(8): |
| 40 | + if value & 1: |
| 41 | + value = value >> 1 ^ POLY |
| 42 | + else: |
| 43 | + value >>= 1 |
| 44 | + table.append(value) |
| 45 | + return table |
| 46 | + |
| 47 | + def main(self, crc): |
| 48 | + indexes = [0 for _ in range(4)] |
| 49 | + crc = int(crc, 16) ^ 0xFFFFFFFF |
| 50 | + for i in range(1, 1000): |
| 51 | + if crc == self.crc32(str(i))[0]: |
| 52 | + return i |
| 53 | + for i in range(3, -1, -1): |
| 54 | + index = indexes[3 - i] = self.lastIndex(crc >> (i << 3)) |
| 55 | + value = self.table[index] |
| 56 | + crc ^= value >> ((3 - i) << 3) |
| 57 | + i = 0 |
| 58 | + while True: |
| 59 | + i += 1 |
| 60 | + low = self.check(i, indexes) |
| 61 | + if low: |
| 62 | + return int(str(i) + low) |
| 63 | + |
| 64 | + def __call__(self, crc): |
| 65 | + return self.main(crc) |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == '__main__': |
| 69 | + from time import time |
| 70 | + from sys import argv |
| 71 | + |
| 72 | + cracker = crc32Cracker() |
| 73 | + t = time() |
| 74 | + print(cracker(argv[1]), time() - t) |
0 commit comments