This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
hasher.py
208 lines (176 loc) · 6.54 KB
/
hasher.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import sublime_plugin
import binascii
import hashlib
try:
from urllib import quote as urlquote
from urllib import unquote as urlunquote
except ImportError:
from urllib.parse import quote as urlquote
from urllib.parse import unquote as urlunquote
import time
import base64
try:
from HTMLParser import HTMLParser
except ImportError:
from html.parser import HTMLParser
try:
from htmlentitydefs import codepoint2name as cp2n
except ImportError:
from html.entities import codepoint2name as cp2n
class Md5Command(sublime_plugin.TextCommand):
def run(self, edit):
for s in self.view.sel():
if s.empty():
s = self.view.word(s)
selected = self.view.substr(s).encode('utf-8')
m = hashlib.md5()
m.update(selected)
txt = m.hexdigest()
self.view.replace(edit, s, txt)
class Sha1Command(sublime_plugin.TextCommand):
def run(self, edit):
for s in self.view.sel():
if s.empty():
s = self.view.word(s)
selected = self.view.substr(s).encode('utf-8')
m = hashlib.sha1()
m.update(selected)
txt = m.hexdigest()
self.view.replace(edit, s, txt)
class Sha224Command(sublime_plugin.TextCommand):
def run(self, edit):
for s in self.view.sel():
if s.empty():
s = self.view.word(s)
selected = self.view.substr(s).encode('utf-8')
m = hashlib.sha224()
m.update(selected)
txt = m.hexdigest()
self.view.replace(edit, s, txt)
class Sha256Command(sublime_plugin.TextCommand):
def run(self, edit):
for s in self.view.sel():
if s.empty():
s = self.view.word(s)
selected = self.view.substr(s).encode('utf-8')
m = hashlib.sha256()
m.update(selected)
txt = m.hexdigest()
self.view.replace(edit, s, txt)
class Sha384Command(sublime_plugin.TextCommand):
def run(self, edit):
for s in self.view.sel():
if s.empty():
s = self.view.word(s)
selected = self.view.substr(s).encode('utf-8')
m = hashlib.sha384()
m.update(selected)
txt = m.hexdigest()
self.view.replace(edit, s, txt)
class Sha512Command(sublime_plugin.TextCommand):
def run(self, edit):
for s in self.view.sel():
if s.empty():
s = self.view.word(s)
selected = self.view.substr(s).encode('utf-8')
m = hashlib.sha512()
m.update(selected)
txt = m.hexdigest()
self.view.replace(edit, s, txt)
class NtlmCommand(sublime_plugin.TextCommand):
def run(self, edit):
for s in self.view.sel():
if s.empty():
s = self.view.word(s)
selected = str(self.view.substr(s))
m = hashlib.new('md4', selected.encode('utf-16le')).digest()
if(type(m) is str):
txt = str(binascii.hexlify(m)).upper()
elif(type(m) is bytes):
txt = str(binascii.hexlify(m)).split('\'')[1].upper()
else:
txt = s
self.view.replace(edit, s, txt)
class Base64EncodeCommand(sublime_plugin.TextCommand):
def run(self, edit):
for s in self.view.sel():
if s.empty():
s = self.view.word(s)
selected = self.view.substr(s).encode('utf-8')
txt = base64.b64encode(selected).decode('utf-8')
self.view.replace(edit, s, txt)
class Base64DecodeCommand(sublime_plugin.TextCommand):
def run(self, edit):
for s in self.view.sel():
if s.empty():
s = self.view.word(s)
selected = self.view.substr(s)
# pad to 4 characters
if len(selected) % 4 != 0:
selected += "=" * (4 - len(selected) % 4)
txt = base64.b64decode(selected).decode('utf8')
self.view.replace(edit, s, txt)
class UriComponentEncodeCommand(sublime_plugin.TextCommand):
def run(self, edit):
for s in self.view.sel():
if s.empty():
s = self.view.word(s)
selected = self.view.substr(s)
txt = urlquote(selected.encode('utf8'), '')
self.view.replace(edit, s, txt)
class UriComponentDecodeCommand(sublime_plugin.TextCommand):
def run(self, edit):
for s in self.view.sel():
if s.empty():
s = self.view.word(s)
selected = self.view.substr(s)
try:
txt = urlunquote(selected.encode('utf8')).decode('utf8')
except TypeError:
txt = urlunquote(selected)
self.view.replace(edit, s, txt)
class EntityEncodeCommand(sublime_plugin.TextCommand):
def run(self, edit):
for s in self.view.sel():
if s.empty():
s = self.view.word(s)
buf = []
for pt in range(s.begin(), s.end()):
ch = self.view.substr(pt)
ch_ord = ord(ch)
try:
ch = '&%s;' % cp2n[ch_ord]
except:
pass
buf.append(ch)
self.view.replace(edit, s, ''.join(buf))
class EntityDecodeCommand(sublime_plugin.TextCommand):
def run(self, edit):
for s in self.view.sel():
if s.empty():
s = self.view.word(s)
selected = self.view.substr(s)
parser = HTMLParser()
selected = parser.unescape(selected)
self.view.replace(edit, s, selected)
class UnicodeEscapeCommand(sublime_plugin.TextCommand):
def run(self, edit):
for s in self.view.sel():
if s.empty():
s = self.view.word(s)
selected = self.view.substr(s)
self.view.replace(edit, s, selected.encode('ascii', 'backslashreplace').decode('utf-8'))
class UnicodeUnescapeCommand(sublime_plugin.TextCommand):
def run(self, edit):
for s in self.view.sel():
if s.empty():
s = self.view.word(s)
selected = self.view.substr(s)
self.view.replace(edit, s, bytes(selected, 'utf8').decode('unicode_escape'))
class CurrentUnixTimestamp(sublime_plugin.TextCommand):
def run(self, edit):
for s in self.view.sel():
txt = time.asctime(time.gmtime())
txt = time.ctime()
txt = "%.0f" % round(time.time(), 3)
self.view.replace(edit, s, txt)