 # Number Base bruteforce checker

Oct 25th, 2021
871
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from enum import Enum
2.
3. class NumberBase(Enum):
4.     DECIMAL = 'DECIMAL'
5.     HEX = 'HEX'
6.     OCTAL = 'OCTAL'
7.     BINARY = 'BINARY'
8.
9. def parse_number_base(str):
10.     curr_state = 'START'
11.     init_DEC_OCT_HEX = [chr(i) for i in range(49,56)]
12.     l_alph = [chr(i) for i in range(97,103)]
13.     u_alph = [chr(i) for i in range(65,71)]
14.     OCT = [chr(i) for i in range(48,56)]
15.     DEC_HEX = [chr(i) for i in range(48,58)]
16.     for char in str:
17.         if curr_state == 'START':
18.             if len(str) == 0:
19.                 return None
20.             elif char == '0':
21.                 curr_state = 'ZERO'
22.             elif char in init_DEC_OCT_HEX:
23.                 curr_state = 'DEC_OCT_HEX'
24.             elif char == '8' or char == '9':
25.                 curr_state = 'DEC_HEX'
26.             elif char in l_alph or char in u_alph:
27.                 curr_state = 'HEX'
28.             else:
29.                 return None
30.         elif curr_state == 'ZERO':
31.             if char == 'b' or char == 'B':
32.                 curr_state = 'BIN_START'
33.             elif char in OCT:
34.                 curr_state = 'OCT'
35.             elif char == 'x' or char == 'X':
36.                 curr_state = 'HEX_START'
37.             else:
38.                 return None
39.         elif curr_state == 'DEC_OCT_HEX':
40.             if char in OCT:
41.                 curr_state = 'DEC_OCT_HEX'
42.             elif char in l_alph or char in u_alph:
43.                 curr_state = 'HEX'
44.             elif char == '8' or char == '9':
45.                 curr_state = 'DEC_HEX'
46.         elif curr_state == 'BIN_START':
47.             if char == '0' or char == '1':
48.                 curr_state = 'BIN'
49.             else:
50.                 return None
51.         elif curr_state == 'HEX_START':
52.             if char in DEC_HEX or char in l_alph or char in u_alph:
53.                 curr_state = 'HEX'
54.             else:
55.                 return None
56.         elif curr_state == 'DEC_HEX':
57.             if char in DEC_HEX:
58.                 curr_state = 'DEC_HEX'
59.             elif char in l_alph or char in u_alph:
60.                 curr_state = 'HEX'
61.             else:
62.                 return None
63.         elif curr_state == 'HEX':
64.             if char in DEC_HEX or char in l_alph or char in u_alph:
65.                 curr_state = 'HEX'
66.             else:
67.                 return None
68.         elif curr_state == 'BIN':
69.             if char == '0' or char == '1':
70.                 curr_state = 'BIN'
71.             else:
72.                 return None
73.         elif curr_state == 'OCT':
74.             if char in OCT:
75.                 curr_state = 'OCT'
76.             else:
77.                 return None
78.     if curr_state == 'ZERO' or curr_state == 'DEC_OCT_HEX' or curr_state == 'DEC_HEX':
79.         return NumberBase.DECIMAL
80.     elif curr_state == 'BIN':
81.         return NumberBase.BINARY
82.     elif curr_state == 'OCT':
83.         return NumberBase.OCTAL
84.     elif curr_state == 'HEX':
85.         return NumberBase.HEX
86.     else:
87.         return None
88.
RAW Paste Data