-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexcon.py
169 lines (141 loc) · 4.33 KB
/
hexcon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/python3
# by AlissonFaoli
# Github: https://github.com/AlissonFaoli/
# Instagram: https://instagram.com/AlissonFaoli
import os, sys, re
if sys.platform == 'linux':
pro_tip = f'''\n\033[36mPro tip:
It works smoother if you add \033[35mhexcon\033[39m \033[36mto your aliases file.
Command: \033[33msudo echo "alias hexcon='python3 {os.path.abspath(__file__)}'" >> {os.getenv('HOME')}/.{os.getenv('SHELL').split(os.sep)[-1].lower()}_aliases\033[39m\n'''
else:
pro_tip = ''
HELP = f'''
\033[35mhexcon\033[39m \033[32mby AlissonFaoli\033[39m
\033[34mGithub\033[39m: \033[32mhttps://github.com/AlissonFaoli/utils/blob/main/hexcon.py\033[39m
Usage:
python3 {sys.argv[0]} [options]
-h, --help Show this help message and exit
-e, --encode Text to hex (encode mode)
-d, --decode Hex to text (decode mode)
-i, --intelligent Intelligent mode (decode/encode) - Default if no option is provided
-ue, --url-encode URL encode
-ud, --url-decode URL decode
If you run the script without any content, it will run in interactive mode.
Examples:
Encoding:
python3 {sys.argv[0]} -e "Hello World"
Decoding:
python3 {sys.argv[0]} -d "48656c6c6f20576f726c64"
Intelligent mode:
python3 {sys.argv[0]} -i "Hello World"
python3 {sys.argv[0]} -i '48656c6c6f20576f726c64'
URL encoding:
python3 {sys.argv[0]} -ue "Hello World"
URL decoding:
python3 {sys.argv[0]} -ud "Hello%20World"
Interactive mode:
python3 {sys.argv[0]}
Note:
If you want to echo a string into the script, you can use the following syntax:
echo "Hello World" | python3 {sys.argv[0]} -e
{pro_tip}
'''
def intelligence(text):
if re.match(r'^[0-9a-fA-F]+$', text):
return fh(text)
else:
return th(text)
def url_encode(text):
return ''.join(map(lambda x: x if (x.isalnum() or x in '-._~') else '%' + format(ord(x), 'x').upper(), text))
def url_decode(text):
d = ''
i = 0
while i < len(text):
if text[i] == '%':
d += chr(int(text[i + 1:i + 3], 16))
i += 3
else:
d += text[i]
i += 1
return d
def th(text):
return ''.join(list(map(lambda x: format(ord(x), 'x'), text)))
def fh(hexa):
results = []
for text in hexa.split():
result = []
for i in range(0, len(text), 2):
result.append(chr(int(text[i:i+2], 16)))
results.append(''.join(result))
return '\n'.join(results)
def interactive():
while True:
try:
match input('[1] Text to hex\n[2] Hex to text\n[3] URL encode\n[4] URL decode\n[5] Exit\n> '):
case '1':
print('\n\033[32m[+]\033[39m Result: '+th(input('Text to hex: '))+'\n')
case '2':
print('\n\033[32m[+]\033[39m Result: '+fh(input('Hex to text: '))+'\n')
case '3':
print('\n\033[32m[+]\033[39m Result: '+url_encode(input('Text to URL encode: '))+'\n')
case '4':
print('\n\033[32m[+]\033[39m Result: '+url_decode(input('URL encoded text: '))+'\n')
case '5':
break
case _:
print('\n\033[31m[!]\033[39m Invalid option!\n')
except KeyboardInterrupt:
break
except ValueError:
print('\n\033[31m[!]\033[39m Bad format!\n')
except Exception:
print('\n\033[31m[!]\033[39m Fatal error...\n')
sys.exit(0)
def get_input():
if not os.isatty(0):
return sys.stdin.read().strip()
elif len(sys.argv) > 1:
return sys.argv[-1]
else:
return interactive()
def get_mode():
if len(sys.argv) > 1:
mode = sys.argv[1]
mode = re.sub(r'^--help', '-h', mode)
mode = re.sub(r'^--encode', '-e', mode)
mode = re.sub(r'^--decode', '-d', mode)
mode = re.sub(r'^--url.?encode', '-ue', mode)
mode = re.sub(r'^--url.?decode', '-ud', mode)
mode = re.sub(r'^--intelligent', '-i', mode)
return mode
else:
return '-i'
def main():
try:
text = get_input()
match get_mode():
case '-e':
print(th(text))
case '-d':
print(fh(text))
case '-i':
print(intelligence(text))
case '-ue':
print(url_encode(text))
case '-ud':
print(url_decode(text))
case '-h':
print(HELP)
case _:
if len(sys.argv) > 2:
print('\033[31m[!]\033[39m Invalid option!')
else:
print(intelligence(text))
except KeyboardInterrupt:
sys.exit(0)
except ValueError:
print('\033[31m[!]\033[39m Bad format!')
except Exception:
print('\033[31m[!]\033[39m Fatal error...')
if __name__ == '__main__':
main()