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