forked from kjur/jsrsasign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base64x-1.1.js
699 lines (642 loc) · 20.3 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*! base64x-1.1.8 (c) 2012-2016 Kenji Urushima | kjur.github.com/jsrsasign/license
*/
/*
* base64x.js - Base64url and supplementary functions for Tom Wu's base64.js library
*
* version: 1.1.8 (2016-Oct-16)
*
* Copyright (c) 2012-2016 Kenji Urushima (kenji.urushima@gmail.com)
*
* 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 kenji.urushima@gmail.com
* @version asn1 1.1.8 (2016-Oct-16)
* @since jsrsasign 2.1
* @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a>
*/
var KJUR;
if (typeof KJUR == "undefined" || !KJUR) KJUR = {};
if (typeof KJUR.lang == "undefined" || !KJUR.lang) KJUR.lang = {};
/**
* String and its utility class <br/>
* This class provides some static utility methods for string.
* @class String and its utility class
* @author Kenji Urushima
* @version 1.0 (2016-Aug-05)
* @since base64x 1.1.7 jsrsasign 5.0.13
* @description
* <br/>
* This class provides static methods for string utility.
* <dl>
* <dt><b>STRING TYPE CHECKERS</b>
* <dd>
* <ul>
* <li>{@link KJUR.lang.String.isInteger} - check whether argument is an integer</li>
* <li>{@link KJUR.lang.String.isHex} - check whether argument is a hexadecimal string</li>
* <li>{@link KJUR.lang.String.isBase64} - check whether argument is a Base64 encoded string</li>
* <li>{@link KJUR.lang.String.isBase64URL} - check whether argument is a Base64URL encoded string</li>
* <li>{@link KJUR.lang.String.isIntegerArray} - check whether argument is an array of integers</li>
* </ul>
* </dl>
*/
KJUR.lang.String = function() {};
/**
* 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/>
* @param {String} s Base64 encoded string
* @return {String} Base64URL encoded string
* @example
* b64tob64u("ab+c3f/==") → "ab-c3f_"
*/
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/>
* @param {String} s Base64URL encoded string
* @return {String} Base64 encoded string
* @example
* b64utob64("ab-c3f_") → "ab+c3f/=="
*/
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));
}
// ==== 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
*/
/**
* 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
*/
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 {
utf8tob64u = function (s) {
return hextob64u(uricmptohex(encodeURIComponentAll(s)));
};
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 =======================================
/**
* convert a hexadecimal string to Base64 encoded string<br/>
* @param {String} s hexadecimal string
* @return {String} resulted Base64 encoded string
* @since base64x 1.1.3
* @description
* This function converts from a hexadecimal string to Base64 encoded
* string without new lines.
* @example
* hextob64("616161") → "YWFh"
*/
function hextob64(s) {
return hex2b64(s);
}
/**
* convert a hexadecimal string to Base64 encoded string with new lines<br/>
* @param {String} s hexadecimal string
* @return {String} resulted Base64 encoded string with new lines
* @since base64x 1.1.3
* @description
* This function converts from a hexadecimal string to Base64 encoded
* string with new lines for each 64 characters. This is useful for
* PEM encoded file.
* @example
* hextob64nl("123456789012345678901234567890123456789012345678901234567890")
* →
* MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4 // new line
* OTAxMjM0NTY3ODkwCg==
*/
function hextob64nl(s) {
var b64 = hextob64(s);
var b64nl = b64.replace(/(.{64})/g, "$1\r\n");
b64nl = b64nl.replace(/\r\n$/, '');
return b64nl;
}
/**
* convert a Base64 encoded string with new lines to a hexadecimal string<br/>
* @param {String} s Base64 encoded string with new lines
* @return {String} hexadecimal string
* @since base64x 1.1.3
* @description
* This function converts from a Base64 encoded
* string with new lines to a hexadecimal string.
* This is useful to handle PEM encoded file.
* This function removes any non-Base64 characters (i.e. not 0-9,A-Z,a-z,\,+,=)
* including new line.
* @example
* hextob64nl(
* "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4\r\n" +
* "OTAxMjM0NTY3ODkwCg==\r\n")
* →
* "123456789012345678901234567890123456789012345678901234567890"
*/
function b64nltohex(s) {
var b64 = s.replace(/[^0-9A-Za-z\/+=]*/g, '');
var hex = b64tohex(b64);
return hex;
}
// ==== hex / ArrayBuffer =================================
/**
* convert a ArrayBuffer to a hexadecimal string<br/>
* @param {String} hex hexadecimal string
* @return {ArrayBuffer} ArrayBuffer
* @since jsrsasign 6.1.4 base64x 1.1.8
* @description
* This function converts from a ArrayBuffer to a hexadecimal string.
* @example
* var buffer = new ArrayBuffer(3);
* var view = new DataView(buffer);
* view.setUint8(0, 0xfa);
* view.setUint8(1, 0xfb);
* view.setUint8(2, 0x01);
* ArrayBuffertohex(buffer) → "fafb01"
*/
function hextoArrayBuffer(hex) {
if (hex.length % 2 != 0) throw "input is not even length";
if (hex.match(/^[0-9A-Fa-f]+$/) == null) throw "input is not hexadecimal";
var buffer = new ArrayBuffer(hex.length / 2);
var view = new DataView(buffer);
for (var i = 0; i < hex.length / 2; i++) {
view.setUint8(i, parseInt(hex.substr(i * 2, 2), 16));
}
return buffer;
}
// ==== ArrayBuffer / hex =================================
/**
* convert a ArrayBuffer to a hexadecimal string<br/>
* @param {ArrayBuffer} buffer ArrayBuffer
* @return {String} hexadecimal string
* @since jsrsasign 6.1.4 base64x 1.1.8
* @description
* This function converts from a ArrayBuffer to a hexadecimal string.
* @example
* hextoArrayBuffer("fffa01") → ArrayBuffer of [255, 250, 1]
*/
function ArrayBuffertohex(buffer) {
var hex = "";
var view = new DataView(buffer);
for (var i = 0; i < buffer.byteLength; i++) {
hex += ("00" + view.getUint8(i).toString(16)).slice(-2);
}
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;
}
// ==== string type checker ===================
/**
* check whether a string is an integer string or not<br/>
* @name isInteger
* @memberOf KJUR.lang.String
* @function
* @static
* @param {String} s input string
* @return {Boolean} true if a string "s" is an integer string otherwise false
* @since base64x 1.1.7 jsrsasign 5.0.13
* @example
* KJUR.lang.String.isInteger("12345") → true
* KJUR.lang.String.isInteger("123ab") → false
*/
KJUR.lang.String.isInteger = function(s) {
if (s.match(/^[0-9]+$/)) {
return true;
} else if (s.match(/^-[0-9]+$/)) {
return true;
} else {
return false;
}
};
/**
* check whether a string is an hexadecimal string or not<br/>
* @name isHex
* @memberOf KJUR.lang.String
* @function
* @static
* @param {String} s input string
* @return {Boolean} true if a string "s" is an hexadecimal string otherwise false
* @since base64x 1.1.7 jsrsasign 5.0.13
* @example
* KJUR.lang.String.isHex("1234") → true
* KJUR.lang.String.isHex("12ab") → true
* KJUR.lang.String.isHex("12AB") → true
* KJUR.lang.String.isHex("12ZY") → false
* KJUR.lang.String.isHex("121") → false -- odd length
*/
KJUR.lang.String.isHex = function(s) {
if (s.length % 2 == 0 &&
(s.match(/^[0-9a-f]+$/) || s.match(/^[0-9A-F]+$/))) {
return true;
} else {
return false;
}
};
/**
* check whether a string is a base64 encoded string or not<br/>
* Input string can conclude new lines or space characters.
* @name isBase64
* @memberOf KJUR.lang.String
* @function
* @static
* @param {String} s input string
* @return {Boolean} true if a string "s" is a base64 encoded string otherwise false
* @since base64x 1.1.7 jsrsasign 5.0.13
* @example
* KJUR.lang.String.isBase64("YWE=") → true
* KJUR.lang.String.isBase64("YW_=") → false
* KJUR.lang.String.isBase64("YWE") → false -- length shall be multiples of 4
*/
KJUR.lang.String.isBase64 = function(s) {
s = s.replace(/\s+/g, "");
if (s.match(/^[0-9A-Za-z+\/]+={0,3}$/) && s.length % 4 == 0) {
return true;
} else {
return false;
}
};
/**
* check whether a string is a base64url encoded string or not<br/>
* Input string can conclude new lines or space characters.
* @name isBase64URL
* @memberOf KJUR.lang.String
* @function
* @static
* @param {String} s input string
* @return {Boolean} true if a string "s" is a base64url encoded string otherwise false
* @since base64x 1.1.7 jsrsasign 5.0.13
* @example
* KJUR.lang.String.isBase64URL("YWE") → true
* KJUR.lang.String.isBase64URL("YW-") → true
* KJUR.lang.String.isBase64URL("YW+") → false
*/
KJUR.lang.String.isBase64URL = function(s) {
if (s.match(/[+/=]/)) return false;
s = b64utob64(s);
return KJUR.lang.String.isBase64(s);
};
/**
* check whether a string is a string of integer array or not<br/>
* Input string can conclude new lines or space characters.
* @name isIntegerArray
* @memberOf KJUR.lang.String
* @function
* @static
* @param {String} s input string
* @return {Boolean} true if a string "s" is a string of integer array otherwise false
* @since base64x 1.1.7 jsrsasign 5.0.13
* @example
* KJUR.lang.String.isIntegerArray("[1,2,3]") → true
* KJUR.lang.String.isIntegerArray(" [1, 2, 3 ] ") → true
* KJUR.lang.String.isIntegerArray("[a,2]") → false
*/
KJUR.lang.String.isIntegerArray = function(s) {
s = s.replace(/\s+/g, "");
if (s.match(/^\[[0-9,]+\]$/)) {
return true;
} else {
return false;
}
};
// ==== 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
};