forked from kjur/jsrsasign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base64x-1.1.js
463 lines (428 loc) · 12.8 KB
/
base64x-1.1.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*! base64x-1.1.6 (c) 2012-2015 Kenji Urushima | kjur.github.com/jsrsasign/license
*/
/*
* base64x.js - Base64url and supplementary functions for Tom Wu's base64.js library
*
* version: 1.1.6 (2015-Nov-11)
*
* Copyright (c) 2012-2015 Kenji Urushima ([email protected])
*
* This software is licensed under the terms of the MIT License.
* http://kjur.github.com/jsjws/license/
*
* The above copyright and license notice shall be
* included in all copies or substantial portions of the Software.
*
* DEPENDS ON:
* - base64.js - Tom Wu's Base64 library
*/
/**
* @fileOverview
* @name base64x-1.1.js
* @author Kenji Urushima [email protected]
* @version asn1 1.1.6 (2015-Nov-11)
* @since jsrsasign 2.1
* @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a>
*/
/**
* Base64URL and supplementary functions for Tom Wu's base64.js library.<br/>
* This class is just provide information about global functions
* defined in 'base64x.js'. The 'base64x.js' script file provides
* global functions for converting following data each other.
* <ul>
* <li>(ASCII) String</li>
* <li>UTF8 String including CJK, Latin and other characters</li>
* <li>byte array</li>
* <li>hexadecimal encoded String</li>
* <li>Full URIComponent encoded String (such like "%69%94")</li>
* <li>Base64 encoded String</li>
* <li>Base64URL encoded String</li>
* </ul>
* All functions in 'base64x.js' are defined in {@link _global_} and not
* in this class.
*
* @class Base64URL and supplementary functions for Tom Wu's base64.js library
* @author Kenji Urushima
* @version 1.1 (07 May 2012)
* @requires base64.js
* @see <a href="http://kjur.github.com/jsjws/">'jwjws'(JWS JavaScript Library) home page http://kjur.github.com/jsjws/</a>
* @see <a href="http://kjur.github.com/jsrsasigns/">'jwrsasign'(RSA Sign JavaScript Library) home page http://kjur.github.com/jsrsasign/</a>
*/
function Base64x() {
}
// ==== string / byte array ================================
/**
* convert a string to an array of character codes
* @param {String} s
* @return {Array of Numbers}
*/
function stoBA(s) {
var a = new Array();
for (var i = 0; i < s.length; i++) {
a[i] = s.charCodeAt(i);
}
return a;
}
/**
* convert an array of character codes to a string
* @param {Array of Numbers} a array of character codes
* @return {String} s
*/
function BAtos(a) {
var s = "";
for (var i = 0; i < a.length; i++) {
s = s + String.fromCharCode(a[i]);
}
return s;
}
// ==== byte array / hex ================================
/**
* convert an array of bytes(Number) to hexadecimal string.<br/>
* @param {Array of Numbers} a array of bytes
* @return {String} hexadecimal string
*/
function BAtohex(a) {
var s = "";
for (var i = 0; i < a.length; i++) {
var hex1 = a[i].toString(16);
if (hex1.length == 1) hex1 = "0" + hex1;
s = s + hex1;
}
return s;
}
// ==== string / hex ================================
/**
* convert a ASCII string to a hexadecimal string of ASCII codes.<br/>
* NOTE: This can't be used for non ASCII characters.
* @param {s} s ASCII string
* @return {String} hexadecimal string
*/
function stohex(s) {
return BAtohex(stoBA(s));
}
// ==== string / base64 ================================
/**
* convert a ASCII string to a Base64 encoded string.<br/>
* NOTE: This can't be used for non ASCII characters.
* @param {s} s ASCII string
* @return {String} Base64 encoded string
*/
function stob64(s) {
return hex2b64(stohex(s));
}
// ==== string / base64url ================================
/**
* convert a ASCII string to a Base64URL encoded string.<br/>
* NOTE: This can't be used for non ASCII characters.
* @param {s} s ASCII string
* @return {String} Base64URL encoded string
*/
function stob64u(s) {
return b64tob64u(hex2b64(stohex(s)));
}
/**
* convert a Base64URL encoded string to a ASCII string.<br/>
* NOTE: This can't be used for Base64URL encoded non ASCII characters.
* @param {s} s Base64URL encoded string
* @return {String} ASCII string
*/
function b64utos(s) {
return BAtos(b64toBA(b64utob64(s)));
}
// ==== base64 / base64url ================================
/**
* convert a Base64 encoded string to a Base64URL encoded string.<br/>
* Example: "ab+c3f/==" → "ab-c3f_"
* @param {String} s Base64 encoded string
* @return {String} Base64URL encoded string
*/
function b64tob64u(s) {
s = s.replace(/\=/g, "");
s = s.replace(/\+/g, "-");
s = s.replace(/\//g, "_");
return s;
}
/**
* convert a Base64URL encoded string to a Base64 encoded string.<br/>
* Example: "ab-c3f_" → "ab+c3f/=="
* @param {String} s Base64URL encoded string
* @return {String} Base64 encoded string
*/
function b64utob64(s) {
if (s.length % 4 == 2) s = s + "==";
else if (s.length % 4 == 3) s = s + "=";
s = s.replace(/-/g, "+");
s = s.replace(/_/g, "/");
return s;
}
// ==== hex / base64url ================================
/**
* convert a hexadecimal string to a Base64URL encoded string.<br/>
* @param {String} s hexadecimal string
* @return {String} Base64URL encoded string
* @description
* convert a hexadecimal string to a Base64URL encoded string.
* NOTE: If leading "0" is omitted and odd number length for
* hexadecimal leading "0" is automatically added.
*/
function hextob64u(s) {
if (s.length % 2 == 1) s = "0" + s;
return b64tob64u(hex2b64(s));
}
/**
* convert a Base64URL encoded string to a hexadecimal string.<br/>
* @param {String} s Base64URL encoded string
* @return {String} hexadecimal string
*/
function b64utohex(s) {
return b64tohex(b64utob64(s));
}
var utf8tob64u, b64utoutf8;
if (typeof Buffer === 'function')
{
utf8tob64u = function (s)
{
return b64tob64u(new Buffer(s, 'utf8').toString('base64'));
};
b64utoutf8 = function (s)
{
return new Buffer(b64utob64(s), 'base64').toString('utf8');
};
}
else
{
// ==== utf8 / base64url ================================
/**
* convert a UTF-8 encoded string including CJK or Latin to a Base64URL encoded string.<br/>
* @param {String} s UTF-8 encoded string
* @return {String} Base64URL encoded string
* @since 1.1
*/
utf8tob64u = function (s)
{
return hextob64u(uricmptohex(encodeURIComponentAll(s)));
};
/**
* convert a Base64URL encoded string to a UTF-8 encoded string including CJK or Latin.<br/>
* @param {String} s Base64URL encoded string
* @return {String} UTF-8 encoded string
* @since 1.1
*/
b64utoutf8 = function (s)
{
return decodeURIComponent(hextouricmp(b64utohex(s)));
};
}
// ==== utf8 / base64url ================================
/**
* convert a UTF-8 encoded string including CJK or Latin to a Base64 encoded string.<br/>
* @param {String} s UTF-8 encoded string
* @return {String} Base64 encoded string
* @since 1.1.1
*/
function utf8tob64(s) {
return hex2b64(uricmptohex(encodeURIComponentAll(s)));
}
/**
* convert a Base64 encoded string to a UTF-8 encoded string including CJK or Latin.<br/>
* @param {String} s Base64 encoded string
* @return {String} UTF-8 encoded string
* @since 1.1.1
*/
function b64toutf8(s) {
return decodeURIComponent(hextouricmp(b64tohex(s)));
}
// ==== utf8 / hex ================================
/**
* convert a UTF-8 encoded string including CJK or Latin to a hexadecimal encoded string.<br/>
* @param {String} s UTF-8 encoded string
* @return {String} hexadecimal encoded string
* @since 1.1.1
*/
function utf8tohex(s) {
return uricmptohex(encodeURIComponentAll(s));
}
/**
* convert a hexadecimal encoded string to a UTF-8 encoded string including CJK or Latin.<br/>
* Note that when input is improper hexadecimal string as UTF-8 string, this function returns
* 'null'.
* @param {String} s hexadecimal encoded string
* @return {String} UTF-8 encoded string or null
* @since 1.1.1
*/
function hextoutf8(s) {
return decodeURIComponent(hextouricmp(s));
}
/**
* convert a hexadecimal encoded string to raw string including non printable characters.<br/>
* @param {String} s hexadecimal encoded string
* @return {String} raw string
* @since 1.1.2
* @example
* hextorstr("610061") → "a\x00a"
*/
function hextorstr(sHex) {
var s = "";
for (var i = 0; i < sHex.length - 1; i += 2) {
s += String.fromCharCode(parseInt(sHex.substr(i, 2), 16));
}
return s;
}
/**
* convert a raw string including non printable characters to hexadecimal encoded string.<br/>
* @param {String} s raw string
* @return {String} hexadecimal encoded string
* @since 1.1.2
* @example
* rstrtohex("a\x00a") → "610061"
*/
function rstrtohex(s) {
var result = "";
for (var i = 0; i < s.length; i++) {
result += ("0" + s.charCodeAt(i).toString(16)).slice(-2);
}
return result;
}
// ==== hex / b64nl =======================================
/*
* since base64x 1.1.3
*/
function hextob64(s) {
return hex2b64(s);
}
/*
* since base64x 1.1.3
*/
function hextob64nl(s) {
var b64 = hextob64(s);
var b64nl = b64.replace(/(.{64})/g, "$1\r\n");
b64nl = b64nl.replace(/\r\n$/, '');
return b64nl;
}
/*
* since base64x 1.1.3
*/
function b64nltohex(s) {
var b64 = s.replace(/[^0-9A-Za-z\/+=]*/g, '');
var hex = b64tohex(b64);
return hex;
}
// ==== URIComponent / hex ================================
/**
* convert a URLComponent string such like "%67%68" to a hexadecimal string.<br/>
* @param {String} s URIComponent string such like "%67%68"
* @return {String} hexadecimal string
* @since 1.1
*/
function uricmptohex(s) {
return s.replace(/%/g, "");
}
/**
* convert a hexadecimal string to a URLComponent string such like "%67%68".<br/>
* @param {String} s hexadecimal string
* @return {String} URIComponent string such like "%67%68"
* @since 1.1
*/
function hextouricmp(s) {
return s.replace(/(..)/g, "%$1");
}
// ==== URIComponent ================================
/**
* convert UTFa hexadecimal string to a URLComponent string such like "%67%68".<br/>
* Note that these "<code>0-9A-Za-z!'()*-._~</code>" characters will not
* converted to "%xx" format by builtin 'encodeURIComponent()' function.
* However this 'encodeURIComponentAll()' function will convert
* all of characters into "%xx" format.
* @param {String} s hexadecimal string
* @return {String} URIComponent string such like "%67%68"
* @since 1.1
*/
function encodeURIComponentAll(u8) {
var s = encodeURIComponent(u8);
var s2 = "";
for (var i = 0; i < s.length; i++) {
if (s[i] == "%") {
s2 = s2 + s.substr(i, 3);
i = i + 2;
} else {
s2 = s2 + "%" + stohex(s[i]);
}
}
return s2;
}
// ==== new lines ================================
/**
* convert all DOS new line("\r\n") to UNIX new line("\n") in
* a String "s".
* @param {String} s string
* @return {String} converted string
*/
function newline_toUnix(s) {
s = s.replace(/\r\n/mg, "\n");
return s;
}
/**
* convert all UNIX new line("\r\n") to DOS new line("\n") in
* a String "s".
* @param {String} s string
* @return {String} converted string
*/
function newline_toDos(s) {
s = s.replace(/\r\n/mg, "\n");
s = s.replace(/\n/mg, "\r\n");
return s;
}
// ==== others ================================
/**
* convert string of integer array to hexadecimal string.<br/>
* @param {String} s string of integer array
* @return {String} hexadecimal string
* @since base64x 1.1.6 jsrsasign 5.0.2
* @throws "malformed integer array string: *" for wrong input
* @description
* This function converts a string of JavaScript integer array to
* a hexadecimal string. Each integer value shall be in a range
* from 0 to 255 otherwise it raise exception. Input string can
* have extra space or newline string so that they will be ignored.
*
* @example
* intarystrtohex(" [123, 34, 101, 34, 58] ")
* -> 7b2265223a (i.e. `{"e":` as string)
*/
function intarystrtohex(s) {
s = s.replace(/^\s*\[\s*/, '');
s = s.replace(/\s*\]\s*$/, '');
s = s.replace(/\s*/g, '');
try {
var hex = s.split(/,/).map(function(element, index, array) {
var i = parseInt(element);
if (i < 0 || 255 < i) throw "integer not in range 0-255";
var hI = ("00" + i.toString(16)).slice(-2);
return hI;
}).join('');
return hex;
} catch(ex) {
throw "malformed integer array string: " + ex;
}
}
/**
* find index of string where two string differs
* @param {String} s1 string to compare
* @param {String} s2 string to compare
* @return {Number} string index of where character differs. Return -1 if same.
* @since jsrsasign 4.9.0 base64x 1.1.5
* @example
* strdiffidx("abcdefg", "abcd4fg") -> 4
* strdiffidx("abcdefg", "abcdefg") -> -1
* strdiffidx("abcdefg", "abcdef") -> 6
* strdiffidx("abcdefgh", "abcdef") -> 6
*/
var strdiffidx = function(s1, s2) {
var n = s1.length;
if (s1.length > s2.length) n = s2.length;
for (var i = 0; i < n; i++) {
if (s1.charCodeAt(i) != s2.charCodeAt(i)) return i;
}
if (s1.length != s2.length) return n;
return -1; // same
};