更换基础镜像
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>格式转换</title>
|
||||
<style>
|
||||
body {
|
||||
text-align: center;
|
||||
margin-top: 100px;
|
||||
}
|
||||
.io {
|
||||
width: 80%;
|
||||
display: inline-block;
|
||||
height : 5rem
|
||||
}
|
||||
.turn {
|
||||
margin: 2rem;
|
||||
}
|
||||
|
||||
|
||||
button-group {
|
||||
display: inline-block;
|
||||
margin: 0 0.5rem;
|
||||
/*width: 1rem;*/
|
||||
}
|
||||
|
||||
button-group button {
|
||||
display: block;
|
||||
}
|
||||
|
||||
|
||||
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h3>输入</h3>
|
||||
<textarea id="in" class="io"></textarea>
|
||||
|
||||
<div class="turn">
|
||||
|
||||
<button-group>
|
||||
<button id="turnio" style="margin-right: 5rem; ">交换输入输出</button>
|
||||
<button id="clearo" style="margin-right: 5rem; "> 清空输出 </button>
|
||||
<button id="clear" style="margin-right: 5rem; "> 清空所有 </button>
|
||||
</button-group>
|
||||
|
||||
<button-group>
|
||||
<button id="utf8Tobase64">UTF8 转 base64</button>
|
||||
<br>
|
||||
<button id="base64ToUtf8">base64 转 UTF8</button>
|
||||
</button-group>
|
||||
|
||||
|
||||
<button-group>
|
||||
<button id="base64ToHex">base64 转 Hex</button>
|
||||
<br>
|
||||
<button id="hexToBase64">Hex 转 base64</button>
|
||||
</button-group>
|
||||
|
||||
|
||||
<button-group>
|
||||
<button id="utf8ToHex">UTF8 转 Hex</button>
|
||||
<br>
|
||||
<button id="hexToUtf8">Hex 转 UTF8</button>
|
||||
</button-group>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
<h3>输出</h3>
|
||||
<textarea id="out" class="io"></textarea>
|
||||
|
||||
<!--<script type="application/javascript" src="base64.js"></script>-->
|
||||
<script type="application/javascript" src="tool.js"></script>
|
||||
<script type="text/javascript" src="./jquery.min.js"></script>
|
||||
<script type="application/javascript">
|
||||
$(function () {
|
||||
$("#turnio").click(function () {
|
||||
var inText = $("#in").val();
|
||||
var outText = $("#out").val();
|
||||
|
||||
$("#in").val( outText );
|
||||
$("#out").val( inText );
|
||||
})
|
||||
|
||||
$("#cleari").click(function () {
|
||||
$("#in").val("");
|
||||
})
|
||||
|
||||
$("#clearo").click(function () {
|
||||
$("#out").val("");
|
||||
})
|
||||
|
||||
$("#clear").click(function () {
|
||||
$("#in").val("");
|
||||
$("#out").val("");
|
||||
})
|
||||
|
||||
$("#utf8Tobase64").click(function () {
|
||||
var inText = $("#in").val();
|
||||
var out = utf8ToBase64( inText );
|
||||
$("#out").val( out );
|
||||
})
|
||||
|
||||
|
||||
$("#base64ToUtf8").click(function () {
|
||||
var inText = $("#in").val();
|
||||
var out = base64ToUtf8(inText)
|
||||
$("#out").val( out );
|
||||
})
|
||||
|
||||
$("#base64ToHex").click(function () {
|
||||
var inText = $("#in").val();
|
||||
var out = base64ToHex(inText)
|
||||
$("#out").val( out );
|
||||
})
|
||||
|
||||
$("#hexToBase64").click(function () {
|
||||
var inText = $("#in").val();
|
||||
var out = hexToBase64( inText )
|
||||
$("#out").val( out );
|
||||
})
|
||||
|
||||
|
||||
$("#hexToUtf8").click(function () {
|
||||
var inText = $("#in").val();
|
||||
out = hexToUtf8(inText)
|
||||
$("#out").val( out );
|
||||
})
|
||||
|
||||
|
||||
$("#utf8ToHex").click(function () {
|
||||
var inText = $("#in").val();
|
||||
out = utf8ToHex( inText )
|
||||
$("#out").val( out );
|
||||
})
|
||||
|
||||
})
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Vendored
+4
File diff suppressed because one or more lines are too long
@@ -0,0 +1,37 @@
|
||||
const tool = require("./tool")
|
||||
|
||||
const str1 = "722 黄谦 cheney";
|
||||
const str2 = "MIIBygYJKoZIhvcNAQcDoIIBuzCCAbcCAQAxggFLMIIBRwIBADAvMCMxCzAJBgNVBAYTAmNuMRQwEgYDVQQKEwtQQkMgVEVTVCBDQQIIE6NthajbntAwDQYJKoZIhvcNAQEBBQAEggEAfpqiXGzxULaCOJIPqRKFAsk/21WIg+NnrkgP/18OxSr68vnpLXKB2aDsyntX9RkwDVc/VwD7nrAq9l0xZPndrYQS2h8ayFfFUan7L5MtOvxS4Pt5+o84DE00+3axfkAr2v7I79tWFmAc0sipn+nQcJzoy0owknfG41hMauMrPI9jdtb1ZcUWLH0bMQ3VeYJJi8RSFYqirpc+RM3sOqwIvBthDcMdnH4XoUx4FKSdnpUd5wHd8Q1cEleVA1PAuEtim2ivjJ8450Y/08pURDOWcmabvX/f4mMp9437uigaC4qShRilyX3/Dt3xhdjq6ImKGxwLUqAaIM6DtHub08S0OTBjBgkqhkiG9w0BBwEwFAYIKoZIhvcNAwcECP6Q/L2a03NMgECXtfqTp+J8JJGL9mNjf51jMe+C4E02q6UQtImLqmvnBEXCnkDLKOBIvOO7ADrlRDN73cma0865Qp1QXP2PT7ez"
|
||||
const hex2 = "308201ca06092a864886f70d010703a08201bb308201b70201003182014b30820147020100302f3023310b300906035504061302636e31143012060355040a130b5042432054455354204341020813a36d85a8db9ed0300d06092a864886f70d0101010500048201007e9aa25c6cf150b68238920fa9128502c93fdb558883e367ae480fff5f0ec52afaf2f9e92d7281d9a0ecca7b57f519300d573f5700fb9eb02af65d3164f9ddad8412da1f1ac857c551a9fb2f932d3afc52e0fb79fa8f380c4d34fb76b17e402bdafec8efdb5616601cd2c8a99fe9d0709ce8cb4a309277c6e3584c6ae32b3c8f6376d6f565c5162c7d1b310dd57982498bc452158aa2ae973e44cdec3aac08bc1b610dc31d9c7e17a14c7814a49d9e951de701ddf10d5c1257950353c0b84b629b68af8c9f38e7463fd3ca5444339672669bbd7fdfe26329f78dfbba281a0b8a928518a5c97dff0eddf185d8eae8898a1b1c0b52a01a20ce83b47b9bd3c4b439306306092a864886f70d010701301406082a864886f70d03070408fe90fcbd9ad3734c804097b5fa93a7e27c24918bf663637f9d6331ef82e04d36aba510b4898baa6be70445c29e40cb28e048bce3bb003ae544337bddc99ad3ceb9429d505cfd8f4fb7b3"
|
||||
const str3 = "722 cheney";
|
||||
|
||||
|
||||
const ret2 = tool.base64ToHex( str2 )
|
||||
console.log("hex=" + ret2)
|
||||
console.assert( ret2 === hex2, "base64ToHex 转换失败" );
|
||||
|
||||
const ret22 = tool.hexToBase64( ret2 )
|
||||
console.assert( str2 === ret22, "base64 hex 转换失败" );
|
||||
|
||||
|
||||
const base64 = tool.utf8ToBase64(str1)
|
||||
const ret3 = tool.base64ToHex( base64 )
|
||||
console.log("base64ToHex hex=" + ret3 )
|
||||
const ret32 = tool.hexToBase64( ret3 )
|
||||
console.assert( ret32 === base64, "base64 hex 中文 转换失败" );
|
||||
|
||||
|
||||
var ret11 = tool.utf8ToBase64(str1)
|
||||
console.log("hex=" + ret11 )
|
||||
|
||||
var ret12 = tool.base64ToUtf8(ret11)
|
||||
console.log("ret12=" + ret12)
|
||||
console.assert( str1 === ret12, "base64 String 中文转换失败" );
|
||||
|
||||
|
||||
var ret4 = tool.utf8ToHex(str1);
|
||||
var ret41 = tool.hexToUtf8(ret4);
|
||||
console.assert( str1 === ret41, "hex String 中文转换失败" );
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,551 @@
|
||||
var codePoint2utf8 = function (code) {
|
||||
var bin = parseInt(code).toString(2)
|
||||
var ln = 0;
|
||||
var mx = 0;
|
||||
if (bin.length <= 7) {
|
||||
ln = 1;
|
||||
mx = 7;
|
||||
} else if (bin.length > 7 && bin.length <= 11) {
|
||||
ln = 2;
|
||||
mx = 11;
|
||||
} else if (bin.length > 11 && bin.length <= 16) {
|
||||
ln = 3;
|
||||
mx = 16;
|
||||
} else if (bin.length > 16 && bin.length <= 21) {
|
||||
ln = 4;
|
||||
mx = 21;
|
||||
} else if (bin.length > 21 && bin.length <= 26) {
|
||||
ln = 5;
|
||||
mx = 26;
|
||||
} else if (bin.length > 26 && bin.length <= 31) {
|
||||
ln = 6;
|
||||
mx = 31;
|
||||
};
|
||||
var longbin = ("0".repeat(mx) + bin).slice(-mx)
|
||||
var result = '';
|
||||
for (var i = 0; i < ln; i++) {
|
||||
if (ln != 1) {
|
||||
if (i == 0) {
|
||||
result += parseInt("1".repeat(ln) + "0" + longbin.slice(0,7-ln),2).toString(16);
|
||||
longbin = longbin.slice(7-ln);
|
||||
} else {
|
||||
result += parseInt("10" + longbin.slice(0,6),2).toString(16);
|
||||
longbin = longbin.slice(6);
|
||||
};
|
||||
} else {
|
||||
result += ("00" + parseInt(longbin,2).toString(16)).slice(-2);
|
||||
};
|
||||
};
|
||||
return result;
|
||||
};
|
||||
|
||||
var stringToHex = function (inputstring) {
|
||||
var result = '';
|
||||
for (var i = 0; i < inputstring.length; i++) {
|
||||
result += codePoint2utf8(inputstring.codePointAt(i))
|
||||
};
|
||||
return result;
|
||||
};
|
||||
|
||||
var hexToString = function (inputstring) {
|
||||
try {
|
||||
var result = decodeURIComponent(inputstring.toLowerCase().replace(/\s+/g, '').replace(/[0-9a-f]{2}/g, '%$&'));
|
||||
} catch(e) {
|
||||
throw "Not valid UTF-8 hex code!";
|
||||
};
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
function stringToByte(str) {
|
||||
var bytes = new Array();
|
||||
var len, c;
|
||||
len = str.length;
|
||||
for(var i = 0; i < len; i++) {
|
||||
c = str.charCodeAt(i);
|
||||
if(c >= 0x010000 && c <= 0x10FFFF) {
|
||||
bytes.push(((c >> 18) & 0x07) | 0xF0);
|
||||
bytes.push(((c >> 12) & 0x3F) | 0x80);
|
||||
bytes.push(((c >> 6) & 0x3F) | 0x80);
|
||||
bytes.push((c & 0x3F) | 0x80);
|
||||
} else if(c >= 0x000800 && c <= 0x00FFFF) {
|
||||
bytes.push(((c >> 12) & 0x0F) | 0xE0);
|
||||
bytes.push(((c >> 6) & 0x3F) | 0x80);
|
||||
bytes.push((c & 0x3F) | 0x80);
|
||||
} else if(c >= 0x000080 && c <= 0x0007FF) {
|
||||
bytes.push(((c >> 6) & 0x1F) | 0xC0);
|
||||
bytes.push((c & 0x3F) | 0x80);
|
||||
} else {
|
||||
bytes.push(c & 0xFF);
|
||||
}
|
||||
}
|
||||
return bytes;
|
||||
|
||||
|
||||
}
|
||||
// utf8
|
||||
function byteToString(arr) {
|
||||
if(typeof arr === 'string') {
|
||||
return arr;
|
||||
}
|
||||
var str = '',
|
||||
_arr = arr;
|
||||
for(var i = 0; i < _arr.length; i++) {
|
||||
var one = _arr[i].toString(2),
|
||||
v = one.match(/^1+?(?=0)/);
|
||||
if(v && one.length == 8) {
|
||||
var bytesLength = v[0].length;
|
||||
var store = _arr[i].toString(2).slice(7 - bytesLength);
|
||||
for(var st = 1; st < bytesLength; st++) {
|
||||
store += _arr[st + i].toString(2).slice(2);
|
||||
}
|
||||
str += String.fromCharCode(parseInt(store, 2));
|
||||
i += bytesLength - 1;
|
||||
} else {
|
||||
str += String.fromCharCode(_arr[i]);
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
/**
|
||||
* Convert the given string to byte array
|
||||
*
|
||||
* @param {string} str the string to be converted to byte array
|
||||
* @returns {number[]}
|
||||
*/
|
||||
var str2bytes = function (str) {
|
||||
var i = 0;
|
||||
var ch = null;
|
||||
var hex = null;
|
||||
var encoded = encodeURIComponent(str);
|
||||
var length = encoded.length;
|
||||
var bytes = [];
|
||||
|
||||
while (i < length) {
|
||||
ch = encoded.charAt(i++);
|
||||
if (ch === '%') {
|
||||
hex = encoded.charAt(i++)
|
||||
hex += encoded.charAt(i++);
|
||||
bytes.push(parseInt(hex, 16));
|
||||
} else {
|
||||
bytes.push(ch.charCodeAt(0));
|
||||
}
|
||||
}
|
||||
|
||||
return bytes;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert the given byte array to string
|
||||
*
|
||||
* @param {number[]} bytes The byte array to be converted to string
|
||||
* @returns {string}
|
||||
*/
|
||||
var bytes2str = function (bytes) {
|
||||
var i = 0;
|
||||
var hex = null;
|
||||
var byte = 0;
|
||||
var hexArray = [];
|
||||
var length = bytes.length;
|
||||
|
||||
while (i < length) {
|
||||
byte = bytes[i++];
|
||||
hex = byte.toString(16);
|
||||
hex = hex.length < 2 ? ('%0' + hex) : ('%' + hex);
|
||||
hexArray.push(hex);
|
||||
}
|
||||
|
||||
return decodeURIComponent(hexArray.join(''));
|
||||
};
|
||||
function base64ArrayBuffer(arrayBuffer) {
|
||||
var base64 = ''
|
||||
var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
|
||||
|
||||
var bytes = new Uint8Array(arrayBuffer)
|
||||
var byteLength = bytes.byteLength
|
||||
var byteRemainder = byteLength % 3
|
||||
var mainLength = byteLength - byteRemainder
|
||||
|
||||
var a, b, c, d
|
||||
var chunk
|
||||
|
||||
// Main loop deals with bytes in chunks of 3
|
||||
for (var i = 0; i < mainLength; i = i + 3) {
|
||||
// Combine the three bytes into a single integer
|
||||
chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]
|
||||
|
||||
// Use bitmasks to extract 6-bit segments from the triplet
|
||||
a = (chunk & 16515072) >> 18 // 16515072 = (2^6 - 1) << 18
|
||||
b = (chunk & 258048) >> 12 // 258048 = (2^6 - 1) << 12
|
||||
c = (chunk & 4032) >> 6 // 4032 = (2^6 - 1) << 6
|
||||
d = chunk & 63 // 63 = 2^6 - 1
|
||||
|
||||
// Convert the raw binary segments to the appropriate ASCII encoding
|
||||
base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d]
|
||||
}
|
||||
|
||||
// Deal with the remaining bytes and padding
|
||||
if (byteRemainder == 1) {
|
||||
chunk = bytes[mainLength]
|
||||
|
||||
a = (chunk & 252) >> 2 // 252 = (2^6 - 1) << 2
|
||||
|
||||
// Set the 4 least significant bits to zero
|
||||
b = (chunk & 3) << 4 // 3 = 2^2 - 1
|
||||
|
||||
base64 += encodings[a] + encodings[b] + '=='
|
||||
} else if (byteRemainder == 2) {
|
||||
chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1]
|
||||
|
||||
a = (chunk & 64512) >> 10 // 64512 = (2^6 - 1) << 10
|
||||
b = (chunk & 1008) >> 4 // 1008 = (2^6 - 1) << 4
|
||||
|
||||
// Set the 2 least significant bits to zero
|
||||
c = (chunk & 15) << 2 // 15 = 2^4 - 1
|
||||
|
||||
base64 += encodings[a] + encodings[b] + encodings[c] + '='
|
||||
}
|
||||
|
||||
return base64
|
||||
}
|
||||
|
||||
(function() {
|
||||
var base64EncodeChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
|
||||
base64DecodeChars = new Array(( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 62, ( - 1), ( - 1), ( - 1), 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 0, 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, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 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, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1));
|
||||
this.base64encode = function(e) {
|
||||
var r, a, c, h, o, t;
|
||||
for (c = e.length, a = 0, r = ''; a < c;) {
|
||||
if (h = 255 & e.charCodeAt(a++), a == c) {
|
||||
r += base64EncodeChars.charAt(h >> 2),
|
||||
r += base64EncodeChars.charAt((3 & h) << 4),
|
||||
r += '==';
|
||||
break
|
||||
}
|
||||
if (o = e.charCodeAt(a++), a == c) {
|
||||
r += base64EncodeChars.charAt(h >> 2),
|
||||
r += base64EncodeChars.charAt((3 & h) << 4 | (240 & o) >> 4),
|
||||
r += base64EncodeChars.charAt((15 & o) << 2),
|
||||
r += '=';
|
||||
break
|
||||
}
|
||||
t = e.charCodeAt(a++),
|
||||
r += base64EncodeChars.charAt(h >> 2),
|
||||
r += base64EncodeChars.charAt((3 & h) << 4 | (240 & o) >> 4),
|
||||
r += base64EncodeChars.charAt((15 & o) << 2 | (192 & t) >> 6),
|
||||
r += base64EncodeChars.charAt(63 & t)
|
||||
}
|
||||
return r
|
||||
}
|
||||
this.base64decode = function(e) {
|
||||
var r, a, c, h, o, t, d;
|
||||
for (t = e.length, o = 0, d = ''; o < t;) {
|
||||
do r = base64DecodeChars[255 & e.charCodeAt(o++)];
|
||||
while (o < t && r == -1);
|
||||
if (r == -1) break;
|
||||
do a = base64DecodeChars[255 & e.charCodeAt(o++)];
|
||||
while (o < t && a == -1);
|
||||
if (a == -1) break;
|
||||
d += String.fromCharCode(r << 2 | (48 & a) >> 4);
|
||||
do {
|
||||
if (c = 255 & e.charCodeAt(o++), 61 == c) return d;
|
||||
c = base64DecodeChars[c]
|
||||
} while ( o < t && c == - 1 );
|
||||
if (c == -1) break;
|
||||
d += String.fromCharCode((15 & a) << 4 | (60 & c) >> 2);
|
||||
do {
|
||||
if (h = 255 & e.charCodeAt(o++), 61 == h) return d;
|
||||
h = base64DecodeChars[h]
|
||||
} while ( o < t && h == - 1 );
|
||||
if (h == -1) break;
|
||||
d += String.fromCharCode((3 & c) << 6 | h)
|
||||
}
|
||||
return d
|
||||
}
|
||||
this.HexToBase64 = function(str) {
|
||||
return base64encode(String.fromCharCode.apply(null, str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
|
||||
}
|
||||
this.Base64Tohex = function(str) {
|
||||
for (var i = 0,
|
||||
bin = base64decode(str.replace(/[ \r\n]+$/, "")), hex = []; i < bin.length; ++i) {
|
||||
var tmp = bin.charCodeAt(i).toString(16);
|
||||
if (tmp.length === 1) tmp = "0" + tmp;
|
||||
hex[hex.length] = tmp;
|
||||
}
|
||||
return hex.join("");
|
||||
},
|
||||
this.toBytes = function(str) {
|
||||
for (var bytes = [], c = 0; c < str.length; c += 2)
|
||||
bytes.push(parseInt(str.substr(c, 2), 16));
|
||||
return bytes;
|
||||
}
|
||||
|
||||
}) ();
|
||||
//hexToBase64 Base64Tohex base64decode base64encode
|
||||
|
||||
var bytesToString = function(bytes){
|
||||
return hexToString(bytesToHex(bytes));
|
||||
}
|
||||
|
||||
|
||||
var bytesToBase64 = function(bytes){
|
||||
return base64ArrayBuffer(bytes);
|
||||
}
|
||||
|
||||
// Convert a byte array to a hex string
|
||||
var bytesToHex = function(bytes) {
|
||||
for (var hex = [], i = 0; i < bytes.length; i++) {
|
||||
hex.push((bytes[i] >>> 4).toString(16));
|
||||
hex.push((bytes[i] & 0xF).toString(16));
|
||||
}
|
||||
return hex.join("");
|
||||
}
|
||||
|
||||
|
||||
var stringToBase64 = function(str){
|
||||
return base64encode(str);
|
||||
}
|
||||
|
||||
var stringToBytes = function(str){
|
||||
return hexToBytes(stringToHex(str));
|
||||
}
|
||||
|
||||
// Convert a ASCII string to a hex string
|
||||
var stringToHex = function(str) {
|
||||
return str.split("").map(function(c) {
|
||||
return ("0" + c.charCodeAt(0).toString(16)).slice(-2);
|
||||
}).join("");
|
||||
}
|
||||
|
||||
var hexToBytes= function(hex) {
|
||||
for (var bytes = [], c = 0; c < hex.length; c += 2)
|
||||
bytes.push(parseInt(hex.substr(c, 2), 16));
|
||||
return bytes;
|
||||
}
|
||||
|
||||
// Convert a hex string to a ASCII string
|
||||
var hexToString = function(hexStr) {
|
||||
var hex = hexStr.toString();//force conversion
|
||||
var str = '';
|
||||
for (var i = 0; i < hex.length; i += 2)
|
||||
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
|
||||
return str;
|
||||
}
|
||||
|
||||
var hexToBase64 = function(hexStr){
|
||||
return HexToBase64(hexStr);
|
||||
}
|
||||
|
||||
var base64ToString = function(base64str){
|
||||
return base64decode(base64str);
|
||||
}
|
||||
|
||||
|
||||
const Base64 = {
|
||||
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
|
||||
encode: function(e) {
|
||||
var t = "";
|
||||
var n, r, i, s, o, u, a;
|
||||
var f = 0;
|
||||
e = Base64._utf8_encode(e);
|
||||
while (f < e.length) {
|
||||
n = e.charCodeAt(f++);
|
||||
r = e.charCodeAt(f++);
|
||||
i = e.charCodeAt(f++);
|
||||
s = n >> 2;
|
||||
o = (n & 3) << 4 | r >> 4;
|
||||
u = (r & 15) << 2 | i >> 6;
|
||||
a = i & 63;
|
||||
if (isNaN(r)) {
|
||||
u = a = 64
|
||||
} else if (isNaN(i)) {
|
||||
a = 64
|
||||
}
|
||||
t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a)
|
||||
}
|
||||
return t
|
||||
},
|
||||
decode: function(e) {
|
||||
var t = "";
|
||||
var n, r, i;
|
||||
var s, o, u, a;
|
||||
var f = 0;
|
||||
e = e.replace(/[^A-Za-z0-9+/=]/g, "");
|
||||
while (f < e.length) {
|
||||
s = this._keyStr.indexOf(e.charAt(f++));
|
||||
o = this._keyStr.indexOf(e.charAt(f++));
|
||||
u = this._keyStr.indexOf(e.charAt(f++));
|
||||
a = this._keyStr.indexOf(e.charAt(f++));
|
||||
n = s << 2 | o >> 4;
|
||||
r = (o & 15) << 4 | u >> 2;
|
||||
i = (u & 3) << 6 | a;
|
||||
t = t + String.fromCharCode(n);
|
||||
if (u != 64) {
|
||||
t = t + String.fromCharCode(r)
|
||||
}
|
||||
if (a != 64) {
|
||||
t = t + String.fromCharCode(i)
|
||||
}
|
||||
}
|
||||
t = Base64._utf8_decode(t);
|
||||
return t
|
||||
},
|
||||
_utf8_encode: function(e) {
|
||||
e = e.replace(/rn/g, "n");
|
||||
var t = "";
|
||||
for (var n = 0; n < e.length; n++) {
|
||||
var r = e.charCodeAt(n);
|
||||
if (r < 128) {
|
||||
t += String.fromCharCode(r)
|
||||
} else if (r > 127 && r < 2048) {
|
||||
t += String.fromCharCode(r >> 6 | 192);
|
||||
t += String.fromCharCode(r & 63 | 128)
|
||||
} else {
|
||||
t += String.fromCharCode(r >> 12 | 224);
|
||||
t += String.fromCharCode(r >> 6 & 63 | 128);
|
||||
t += String.fromCharCode(r & 63 | 128)
|
||||
}
|
||||
}
|
||||
return t
|
||||
},
|
||||
_utf8_decode: function(e) {
|
||||
var t = "";
|
||||
var n = 0;
|
||||
var r = 0;
|
||||
var c1 =0;
|
||||
var c2 =0;
|
||||
var c3 =0;
|
||||
while (n < e.length) {
|
||||
r = e.charCodeAt(n);
|
||||
if (r < 128) {
|
||||
t += String.fromCharCode(r);
|
||||
n++
|
||||
} else if (r > 191 && r < 224) {
|
||||
c2 = e.charCodeAt(n + 1);
|
||||
t += String.fromCharCode((r & 31) << 6 | c2 & 63);
|
||||
n += 2
|
||||
} else {
|
||||
c2 = e.charCodeAt(n + 1);
|
||||
c3 = e.charCodeAt(n + 2);
|
||||
t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
|
||||
n += 3
|
||||
}
|
||||
}
|
||||
return t
|
||||
}
|
||||
}
|
||||
|
||||
function encodeUtf8(text) {
|
||||
const code = encodeURIComponent(text);
|
||||
const bytes = [];
|
||||
for (var i = 0; i < code.length; i++) {
|
||||
const c = code.charAt(i);
|
||||
if (c === '%') {
|
||||
const hex = code.charAt(i + 1) + code.charAt(i + 2);
|
||||
const hexVal = parseInt(hex, 16);
|
||||
bytes.push(hexVal);
|
||||
i += 2;
|
||||
} else bytes.push(c.charCodeAt(0));
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
function decodeUtf8(bytes) {
|
||||
var encoded = "";
|
||||
for (var i = 0; i < bytes.length; i++) {
|
||||
encoded += '%' + bytes[i].toString(16);
|
||||
}
|
||||
return decodeURIComponent(encoded);
|
||||
}
|
||||
function strToBytesUTF8(str) {
|
||||
let utf8 = [];
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
let charcode = str.charCodeAt(i);
|
||||
if (charcode < 0x80) utf8.push(charcode);
|
||||
else if (charcode < 0x800) {
|
||||
utf8.push(0xc0 | (charcode >> 6),
|
||||
0x80 | (charcode & 0x3f));
|
||||
}
|
||||
else if (charcode < 0xd800 || charcode >= 0xe000) {
|
||||
utf8.push(0xe0 | (charcode >> 12),
|
||||
0x80 | ((charcode>>6) & 0x3f),
|
||||
0x80 | (charcode & 0x3f));
|
||||
}
|
||||
// surrogate pair
|
||||
else {
|
||||
i++;
|
||||
// UTF-16 encodes 0x10000-0x10FFFF by
|
||||
// subtracting 0x10000 and splitting the
|
||||
// 20 bits of 0x0-0xFFFFF into two halves
|
||||
charcode = 0x10000 + (((charcode & 0x3ff)<<10)
|
||||
| (str.charCodeAt(i) & 0x3ff));
|
||||
utf8.push(0xf0 | (charcode >>18),
|
||||
0x80 | ((charcode>>12) & 0x3f),
|
||||
0x80 | ((charcode>>6) & 0x3f),
|
||||
0x80 | (charcode & 0x3f));
|
||||
}
|
||||
}
|
||||
return utf8;
|
||||
}
|
||||
|
||||
function utf8ToBase64(str){
|
||||
var ret11 = strToBytesUTF8( str )
|
||||
return bytesToBase64( ret11 )
|
||||
}
|
||||
|
||||
function base64ToUtf8( str ){
|
||||
var hex = base64ToHex( str );
|
||||
var ret = hexToBytes( hex )
|
||||
// var ret = base64ToBytes( str )
|
||||
return decodeUtf8(ret);
|
||||
}
|
||||
|
||||
var base64ToHex = function(base64str){
|
||||
return Base64Tohex(base64str);
|
||||
}
|
||||
|
||||
var base64ToBytes = function(base64str){
|
||||
return toBytes(base64str);
|
||||
//null
|
||||
}
|
||||
var utf8ToHex = function (str) {
|
||||
var ret11 = strToBytesUTF8( str )
|
||||
return bytesToHex( ret11 )
|
||||
}
|
||||
|
||||
var hexToUtf8 = function (hex) {
|
||||
var ret = hexToBytes( hex )
|
||||
// var ret = base64ToBytes( str )
|
||||
return decodeUtf8(ret);
|
||||
}
|
||||
|
||||
if ( module ) {
|
||||
|
||||
module.exports = {
|
||||
|
||||
stringToByte,
|
||||
byteToString,
|
||||
str2bytes,
|
||||
bytes2str,
|
||||
base64ArrayBuffer,
|
||||
bytesToString,
|
||||
bytesToBase64,
|
||||
bytesToHex,
|
||||
stringToBase64,
|
||||
stringToBytes,
|
||||
stringToHex,
|
||||
hexToBytes,
|
||||
hexToString,
|
||||
hexToBase64,
|
||||
base64ToString,
|
||||
base64ToHex,
|
||||
base64ToBytes,
|
||||
Base64,
|
||||
encodeUtf8,
|
||||
decodeUtf8,
|
||||
strToBytesUTF8,
|
||||
utf8ToBase64,
|
||||
base64ToUtf8,
|
||||
utf8ToHex,
|
||||
hexToUtf8
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user