-
Notifications
You must be signed in to change notification settings - Fork 4
/
crypt_functions.js
211 lines (182 loc) · 4.84 KB
/
crypt_functions.js
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
209
210
211
function base64encode(plaintext) {
return btoa(encodeURIComponent(plaintext).replace(/%([0-9A-F]{2})/g,
function toSolidBytes(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
function base64decode(plaintext) {
return decodeURIComponent(atob(plaintext).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
function countChars() {
document.getElementById('count').textContent = "Character count: " + document.getElementById('text').value.length;
return "";
}
function disemowel(plaintext) {
return plaintext.replace(/[AEIOUaeiou]/g, '');
}
function entityEncode(plaintext) {
var entityStr = "";
for (i = 0; i < plaintext.length; i++) {
if (/[^a-zA-Z\d\s]/.test(plaintext[i])) {
entityStr += "&#" + plaintext.charCodeAt(i) + ";";
}
else {
entityStr += plaintext[i];
}
}
return entityStr;
}
function entityDecode(plaintext) {
return plaintext.replace(/&#(\d+);/g, function(match, dec) {
return String.fromCharCode(dec);
});
}
function removespaces(plaintext) {
return plaintext.split(' ').join('');
}
function reverse(plaintext) {
return plaintext.split("").reverse().join("");
}
function rot13(plaintext) {
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var rotated = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm";
var rottxt = "";
for (i=0; i < plaintext.length; i++) {
char = plaintext.substring(i, i+1);
if (char.match(/[a-z]/i)) {
rottxt += rotated.charAt(alphabet.indexOf(char));
}
else {
rottxt += char;
}
}
return rottxt;
}
function timestampEncode(plaintext) {
var date = new Date(plaintext.toString());
var timestamp = date.getTime()/1000.0;
if (!isNaN(timestamp)) {
return timestamp;
}
return plaintext;
}
function timestampDecode(plaintext) {
var date = new Date(plaintext *1000);
return date.toGMTString()+" "+date.toLocaleString();
}
function urlEncode(plaintext) {
return encodeURIComponent(plaintext);
}
function urlDecode(plaintext) {
return decodeURIComponent(plaintext);
}
function xor(plaintext, key) {
var xored = "";
if (key.length > 0) {
while (key.length < plaintext.length) {
key += key;
}
for (i=0; i < plaintext.length; i++) {
xored += String.fromCharCode(plaintext.charCodeAt(i) ^ key.charCodeAt(i));
}
return xored;
}
return plaintext;
}
function txt2bin(plaintext) {
return hex2bin(txt2hex(plaintext));
}
function txt2hex(plaintext) {
var hexStr = "";
for (i = 0; i < plaintext.length; i++) {
hexStr += plaintext.charCodeAt(i).toString(16);
}
return hexStr;
}
function bin2dec(plaintext) {
var num = parseInt(plaintext, 2);
if (!isNaN(num) && num < Number.MAX_SAFE_INTEGER) {
return num;
}
return plaintext;
}
function bin2hex(plaintext) {
while(plaintext.length % 4 != 0) {
plaintext = "0" + plaintext;
}
var hexStr = "";
for (i = 0; i < plaintext.length; i += 4) {
hexStr += parseInt(plaintext.substr(i, 4), 2).toString(16);
}
return hexStr;
}
function bin2txt(plaintext) {
return hex2txt(bin2hex(plaintext));
}
function bin2oct(plaintext) {
return dec2oct(bin2dec(plaintext));
}
function dec2bin(plaintext) {
plaintext = plaintext.replace(/[,]/g, '');
if (!isNaN(plaintext) && parseInt(plaintext) < Number.MAX_SAFE_INTEGER) {
return parseInt(plaintext).toString(2);
}
return plaintext;
}
function dec2hex(plaintext) {
plaintext = plaintext.replace(/[,]/g, '');
if (!isNaN(plaintext) && parseInt(plaintext) < Number.MAX_SAFE_INTEGER) {
return parseInt(plaintext).toString(16);
}
return plaintext;
}
function dec2oct(plaintext) {
plaintext = plaintext.replace(/[,]/g, '');
if (!isNaN(plaintext) && parseInt(plaintext) < Number.MAX_SAFE_INTEGER) {
return parseInt(plaintext).toString(8);
}
return plaintext;
}
function hex2bin(plaintext) {
var binStr = "";
for (i = 0; i < plaintext.length; i ++) {
var bin = parseInt(plaintext.substr(i, 1), 16).toString(2);
while(bin.length < 4) {
bin = "0" + bin;
}
binStr += bin
}
return binStr;
}
function hex2dec(plaintext) {
var num = parseInt(plaintext, 16);
if (!isNaN(num) && num < Number.MAX_SAFE_INTEGER) {
return num;
}
return plaintext;
}
function hex2oct(plaintext) {
return dec2oct(hex2dec(plaintext));
}
function hex2txt(plaintext) {
var asciiStr = "";
for (i = 0; i < plaintext.length; i += 2) {
asciiStr += String.fromCharCode(parseInt(plaintext.substr(i, 2), 16));
}
return asciiStr;
}
function oct2bin(plaintext) {
return dec2bin(oct2dec(plaintext));
}
function oct2dec(plaintext) {
var num = parseInt(plaintext, 8);
if (!isNaN(num) && num < Number.MAX_SAFE_INTEGER) {
return num;
}
return plaintext;
}
function oct2hex(plaintext) {
return dec2hex(oct2dec(plaintext));
}