-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.js
304 lines (275 loc) · 9.61 KB
/
util.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// GPG4Browsers - An OpenPGP implementation in javascript
// Copyright (C) 2011 Recurity Labs GmbH
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
var Util = function() {
this.emailRegEx = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
this.hexdump = function(str) {
var r=[];
var e=str.length;
var c=0;
var h;
var i = 0;
while(c<e){
h=str.charCodeAt(c++).toString(16);
while(h.length<2) h="0"+h;
r.push(" "+h);
i++;
if (i % 32 == 0)
r.push("\n ");
}
return r.join('');
};
/**
* Create hexstring from a binary
* @param {String} str String to convert
* @return {String} String containing the hexadecimal values
*/
this.hexstrdump = function(str) {
if (str == null)
return "";
var r=[];
var e=str.length;
var c=0;
var h;
while(c<e){
h=str[c++].charCodeAt().toString(16);
while(h.length<2) h="0"+h;
r.push(""+h);
}
return r.join('');
};
/**
* Create binary string from a hex encoded string
* @param {String} str Hex string to convert
* @return {String} String containing the binary values
*/
this.hex2bin = function(hex) {
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
};
/**
* Creating a hex string from an binary array of integers (0..255)
* @param {String} str Array of bytes to convert
* @return {String} Hexadecimal representation of the array
*/
this.hexidump = function(str) {
var r=[];
var e=str.length;
var c=0;
var h;
while(c<e){
h=str[c++].toString(16);
while(h.length<2) h="0"+h;
r.push(""+h);
}
return r.join('');
};
/**
* Convert a native javascript string to a string of utf8 bytes
* @param {String} str The string to convert
* @return {String} A valid squence of utf8 bytes
*/
this.encode_utf8 = function(str) {
return unescape(encodeURIComponent(str));
};
/**
* Convert a string of utf8 bytes to a native javascript string
* @param {String} utf8 A valid squence of utf8 bytes
* @return {String} A native javascript string
*/
this.decode_utf8 = function(utf8) {
return decodeURIComponent(escape(utf8));
};
var str2bin = function(str, result) {
for (var i = 0; i < str.length; i++) {
result[i] = str.charCodeAt(i);
}
return result;
};
var bin2str = function(bin) {
var result = [];
for (var i = 0; i < bin.length; i++) {
result.push(String.fromCharCode(bin[i]));
}
return result.join('');
};
/**
* Convert a string to an array of integers(0.255)
* @param {String} str String to convert
* @return {Integer[]} An array of (binary) integers
*/
this.str2bin = function(str) {
return str2bin(str, new Array(str.length));
};
/**
* Convert an array of integers(0.255) to a string
* @param {Integer[]} bin An array of (binary) integers to convert
* @return {String} The string representation of the array
*/
this.bin2str = bin2str;
/**
* Convert a string to a Uint8Array
* @param {String} str String to convert
* @return {Uint8Array} The array of (binary) integers
*/
this.str2Uint8Array = function(str) {
return str2bin(str, new Uint8Array(new ArrayBuffer(str.length)));
};
/**
* Convert a Uint8Array to a string. This currently functions
* the same as bin2str.
* @param {Uint8Array} bin An array of (binary) integers to convert
* @return {String} String representation of the array
*/
this.Uint8Array2str = bin2str;
/**
* Calculates a 16bit sum of a string by adding each character
* codes modulus 65535
* @param {String} text String to create a sum of
* @return {Integer} An integer containing the sum of all character
* codes % 65535
*/
this.calc_checksum = function(text) {
var checksum = { s: 0, add: function (sadd) { this.s = (this.s + sadd) % 65536; }};
for (var i = 0; i < text.length; i++) {
checksum.add(text.charCodeAt(i));
}
return checksum.s;
};
/**
* Helper function to print a debug message. Debug
* messages are only printed if
* openpgp.config.debug is set to true. The calling
* Javascript context MUST define
* a "showMessages(text)" function. Line feeds ('\n')
* are automatically converted to HTML line feeds '<br/>'
* @param {String} str String of the debug message
* @return {String} An HTML tt entity containing a paragraph with a
* style attribute where the debug message is HTMLencoded in.
*/
this.print_debug = function(str) {
if (openpgp.config.debug) {
str = openpgp_encoding_html_encode(str);
showMessages("<tt><p style=\"background-color: #ffffff; width: 652px; word-break: break-word; padding: 5px; border-bottom: 1px solid black;\">"+str.replace(/\n/g,"<br>")+"</p></tt>");
}
};
/**
* Helper function to print a debug message. Debug
* messages are only printed if
* openpgp.config.debug is set to true. The calling
* Javascript context MUST define
* a "showMessages(text)" function. Line feeds ('\n')
* are automatically converted to HTML line feeds '<br/>'
* Different than print_debug because will call hexstrdump iff necessary.
* @param {String} str String of the debug message
* @return {String} An HTML tt entity containing a paragraph with a
* style attribute where the debug message is HTMLencoded in.
*/
this.print_debug_hexstr_dump = function(str,strToHex) {
if (openpgp.config.debug) {
str = str + this.hexstrdump(strToHex);
str = openpgp_encoding_html_encode(str);
showMessages("<tt><p style=\"background-color: #ffffff; width: 652px; word-break: break-word; padding: 5px; border-bottom: 1px solid black;\">"+str.replace(/\n/g,"<br>")+"</p></tt>");
}
};
/**
* Helper function to print an error message.
* The calling Javascript context MUST define
* a "showMessages(text)" function. Line feeds ('\n')
* are automatically converted to HTML line feeds '<br/>'
* @param {String} str String of the error message
* @return {String} A HTML paragraph entity with a style attribute
* containing the HTML encoded error message
*/
this.print_error = function(str) {
str = openpgp_encoding_html_encode(str);
showMessages("<p style=\"font-size: 80%; background-color: #FF8888; margin:0; width: 652px; word-break: break-word; padding: 5px; border-bottom: 1px solid black;\"><span style=\"color: #888;\"><b>ERROR:</b></span> "+str.replace(/\n/g,"<br>")+"</p>");
};
/**
* Helper function to print an info message.
* The calling Javascript context MUST define
* a "showMessages(text)" function. Line feeds ('\n')
* are automatically converted to HTML line feeds '<br/>'.
* @param {String} str String of the info message
* @return {String} A HTML paragraph entity with a style attribute
* containing the HTML encoded info message
*/
this.print_info = function(str) {
str = openpgp_encoding_html_encode(str);
showMessages("<p style=\"font-size: 80%; background-color: #88FF88; margin:0; width: 652px; word-break: break-word; padding: 5px; border-bottom: 1px solid black;\"><span style=\"color: #888;\"><b>INFO:</b></span> "+str.replace(/\n/g,"<br>")+"</p>");
};
this.print_warning = function(str) {
str = openpgp_encoding_html_encode(str);
showMessages("<p style=\"font-size: 80%; background-color: #FFAA88; margin:0; width: 652px; word-break: break-word; padding: 5px; border-bottom: 1px solid black;\"><span style=\"color: #888;\"><b>WARNING:</b></span> "+str.replace(/\n/g,"<br>")+"</p>");
};
this.getLeftNBits = function (string, bitcount) {
var rest = bitcount % 8;
if (rest == 0)
return string.substring(0, bitcount / 8);
var bytes = (bitcount - rest) / 8 +1;
var result = string.substring(0, bytes);
return this.shiftRight(result, 8-rest); // +String.fromCharCode(string.charCodeAt(bytes -1) << (8-rest) & 0xFF);
};
/**
* Shifting a string to n bits right
* @param {String} value The string to shift
* @param {Integer} bitcount Amount of bits to shift (MUST be smaller
* than 9)
* @return {String} Resulting string.
*/
this.shiftRight = function(value, bitcount) {
var temp = util.str2bin(value);
if (bitcount % 8 != 0) {
for (var i = temp.length-1; i >= 0; i--) {
temp[i] >>= bitcount % 8;
if (i > 0)
temp[i] |= (temp[i - 1] << (8 - (bitcount % 8))) & 0xFF;
}
} else {
return value;
}
return util.bin2str(temp);
};
/**
* Return the algorithm type as string
* @return {String} String representing the message type
*/
this.get_hashAlgorithmString = function(algo) {
switch(algo) {
case 1:
return "MD5";
case 2:
return "SHA1";
case 3:
return "RIPEMD160";
case 8:
return "SHA256";
case 9:
return "SHA384";
case 10:
return "SHA512";
case 11:
return "SHA224";
}
return "unknown";
};
};
/**
* an instance that should be used.
*/
var util = new Util();