Skip to content
This repository was archived by the owner on Feb 21, 2023. It is now read-only.

Commit 863037d

Browse files
committed
refactor: enhance code quality
1 parent 09ab24a commit 863037d

File tree

1 file changed

+52
-62
lines changed

1 file changed

+52
-62
lines changed

main.py

Lines changed: 52 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,64 @@
1-
POLY = 0xEDB88320
1+
from itertools import count
2+
from typing import Literal
23

4+
POLY: Literal[0xEDB88320] = 0xEDB88320
35

4-
class crc32Cracker:
5-
def __init__(self):
6-
self.table = self.table()
6+
table: list[int] = []
77

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)
1516

16-
def lastIndex(self, crc: int) -> int:
17-
for i in range(256):
18-
if crc == self.table[i] >> 24:
19-
return i
2017

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
3325

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
4626

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
6331

64-
def __call__(self, crc):
65-
return self.main(crc)
6632

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
6745

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__":
7062
from sys import argv
7163

72-
cracker = crc32Cracker()
73-
t = time()
74-
print(cracker(argv[1]), time() - t)
64+
print(main(argv[1]))

0 commit comments

Comments
 (0)