sm2
Version:
sm2,sm3,cryptojs,crypto,base64,rsa,aes crypt
700 lines (642 loc) • 20.8 kB
JavaScript
/*! 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
};