forked from dangelov/hasher
/
hasher.py
116 lines (96 loc) · 2.97 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
import sublime_plugin
import hashlib
import urllib
import time
import base64
import re
# test
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('utf8')
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('utf8')
m = hashlib.sha1()
m.update(selected)
txt = m.hexdigest()
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('utf8')
txt = base64.b64encode(selected)
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 = urllib.quote(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)
txt = urllib.unquote(selected.encode('utf8'))
txt = unicode(txt.decode('utf8'));
self.view.replace(edit, s, txt);
class EntityEncodeCommand(sublime_plugin.TextCommand):
def run(self, edit):
from htmlentitydefs import codepoint2name as cp2n
for s in self.view.sel():
if s.empty():
s = self.view.word(s)
buf = []
for pt in xrange(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 = unicode(self.view.substr(s))
import HTMLParser
HTMLParser = HTMLParser.HTMLParser()
selected = HTMLParser.unescape(selected)
self.view.replace(edit, s, selected);
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)