endecode-lib.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /**
  2. * 本库提供几个常用方法:
  3. * 1、enDecodeTools.uniEncode(text); 将中文进行Unicode编码并输出
  4. * 2、enDecodeTools.base64Encode(text); 将文字进行base64编码并输出
  5. * 3、enDecodeTools.base64Decode(text); 将经过base64编码的文字进行base64解码并输出
  6. * 4、enDecodeTools.utf8Encode(text); 将文字进行utf-8编码并输出
  7. * 5、enDecodeTools.utf8Decode(text); 将经过utf-8编码的文字进行utf-8解码并输出
  8. */
  9. module.exports = (() => {
  10. //base64编码字符集
  11. let _base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  12. //base64解码字符集
  13. let _base64DecodeChars = [
  14. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  15. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  16. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
  17. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
  18. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  19. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  20. -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  21. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1];
  22. /**
  23. * 此方法实现中文向Unicode的转码,与Jre中的"native2ascii"命令一样
  24. * @param {String} text 需要进行转码的字符串
  25. * @return {String} Unicode码
  26. */
  27. let _uniEncode = function (text) {
  28. let res = [];
  29. for (let i = 0; i < text.length; i++) {
  30. res[i] = ("00" + text.charCodeAt(i).toString(16)).slice(-4);
  31. }
  32. return "\\u" + res.join("\\u");
  33. };
  34. /**
  35. * 此方法用于将Unicode码解码为正常字符串
  36. * @param {Object} text
  37. */
  38. let _uniDecode = function (text) {
  39. text = text = text.replace(/(\\)?\\u/gi, "%u").replace('%u0025', '%25');
  40. text = unescape(text.toString().replace(/%2B/g, "+"));
  41. let matches = text.match(/(%u00([0-9A-F]{2}))/gi);
  42. if (matches) {
  43. for (let matchid = 0; matchid < matches.length; matchid++) {
  44. let code = matches[matchid].substring(1, 3);
  45. let x = Number("0x" + code);
  46. if (x >= 128) {
  47. text = text.replace(matches[matchid], code);
  48. }
  49. }
  50. }
  51. text = unescape(text.toString().replace(/%2B/g, "+"));
  52. return text;
  53. };
  54. /**
  55. * 此方法用于将文字进行UTF-8编码
  56. * @param {Object} str 源码
  57. * @return {String} UTF-8码
  58. */
  59. let _utf8Encode = function (str) {
  60. let out, i, len, c;
  61. out = "";
  62. len = str.length;
  63. for (i = 0; i < len; i++) {
  64. c = str.charCodeAt(i);
  65. if ((c >= 0x0001) && (c <= 0x007F)) {
  66. out += str.charAt(i);
  67. } else if (c > 0x07FF) {
  68. out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
  69. out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
  70. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  71. } else {
  72. out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
  73. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  74. }
  75. }
  76. return out;
  77. };
  78. /**
  79. * 此方法用于将文字进行UTF-8解码
  80. * @param {Object} str
  81. * @return {String} 原文字
  82. */
  83. let _utf8Decode = function (str) {
  84. let out, i, len, c;
  85. let char2, char3;
  86. out = "";
  87. len = str.length;
  88. i = 0;
  89. while (i < len) {
  90. c = str.charCodeAt(i++);
  91. switch (c >> 4) {
  92. case 0:
  93. case 1:
  94. case 2:
  95. case 3:
  96. case 4:
  97. case 5:
  98. case 6:
  99. case 7:
  100. // 0xxxxxxx
  101. out += str.charAt(i - 1);
  102. break;
  103. case 12:
  104. case 13:
  105. // 110x xxxx  10xx xxxx
  106. char2 = str.charCodeAt(i++);
  107. out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
  108. break;
  109. case 14:
  110. // 1110 xxxx 10xx xxxx 10xx xxxx
  111. char2 = str.charCodeAt(i++);
  112. char3 = str.charCodeAt(i++);
  113. out += String.fromCharCode(((c & 0x0F) << 12) |
  114. ((char2 & 0x3F) << 6) |
  115. ((char3 & 0x3F) << 0));
  116. break;
  117. }
  118. }
  119. return out;
  120. };
  121. /**
  122. * 此方法用于将文字进行base64编码
  123. * @param {Object} str 源码
  124. * @return {String} base64码
  125. */
  126. let _base64Encode = function (str) {
  127. let out, i, len;
  128. let c1, c2, c3;
  129. len = str.length;
  130. i = 0;
  131. out = "";
  132. while (i < len) {
  133. c1 = str.charCodeAt(i++) & 0xff;
  134. if (i == len) {
  135. out += _base64EncodeChars.charAt(c1 >> 2);
  136. out += _base64EncodeChars.charAt((c1 & 0x3) << 4);
  137. out += "==";
  138. break;
  139. }
  140. c2 = str.charCodeAt(i++);
  141. if (i == len) {
  142. out += _base64EncodeChars.charAt(c1 >> 2);
  143. out += _base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  144. out += _base64EncodeChars.charAt((c2 & 0xF) << 2);
  145. out += "=";
  146. break;
  147. }
  148. c3 = str.charCodeAt(i++);
  149. out += _base64EncodeChars.charAt(c1 >> 2);
  150. out += _base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  151. out += _base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
  152. out += _base64EncodeChars.charAt(c3 & 0x3F);
  153. }
  154. return out;
  155. };
  156. /**
  157. * 此方法用于将文字进行base64解码
  158. * @param {Object} str 源码
  159. * @return {String} 源码
  160. */
  161. let _base64Decode = function (str) {
  162. let c1, c2, c3, c4;
  163. let i, len, out;
  164. len = str.length;
  165. i = 0;
  166. out = "";
  167. while (i < len) {
  168. /* c1 */
  169. do {
  170. c1 = _base64DecodeChars[str.charCodeAt(i++) & 0xff];
  171. }
  172. while (i < len && c1 == -1);
  173. if (c1 == -1)
  174. break;
  175. /* c2 */
  176. do {
  177. c2 = _base64DecodeChars[str.charCodeAt(i++) & 0xff];
  178. }
  179. while (i < len && c2 == -1);
  180. if (c2 == -1)
  181. break;
  182. out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
  183. /* c3 */
  184. do {
  185. c3 = str.charCodeAt(i++) & 0xff;
  186. if (c3 == 61)
  187. return out;
  188. c3 = _base64DecodeChars[c3];
  189. }
  190. while (i < len && c3 == -1);
  191. if (c3 == -1)
  192. break;
  193. out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
  194. /* c4 */
  195. do {
  196. c4 = str.charCodeAt(i++) & 0xff;
  197. if (c4 == 61)
  198. return out;
  199. c4 = _base64DecodeChars[c4];
  200. }
  201. while (i < len && c4 == -1);
  202. if (c4 == -1)
  203. break;
  204. out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
  205. }
  206. return out;
  207. };
  208. /**
  209. * 中文,一般情况下Unicode是UTF-16实现,长度2位,而UTF-8编码是3位
  210. * @param str
  211. * @return {String}
  212. */
  213. let _utf16to8 = function (str) {
  214. return str.replace(/\\x/g, '%');
  215. };
  216. let _utf8to16 = function (str) {
  217. return str.replace(/%/g, '\\x');
  218. };
  219. /**
  220. * md5加密
  221. * @param str
  222. */
  223. let md5 = (str) => {
  224. let md5 = Tarp.require('./md5');
  225. return md5(str);
  226. };
  227. /**
  228. * gzip加密
  229. * @param str
  230. * @returns {*}
  231. */
  232. let gzipEncode = str => {
  233. let pako = Tarp.require('./pako');
  234. try {
  235. return window.btoa(pako.gzip(escape(str), {to: "string"}));
  236. } catch (e) {
  237. return 'Error: 当前字符串不能被Gzip加密';
  238. }
  239. };
  240. /**
  241. * gzip解密
  242. * @param str
  243. * @returns {string}
  244. */
  245. let gzipDecode = str => {
  246. let pako = Tarp.require('./pako');
  247. try {
  248. let charData = window.atob(str).split('').map(x => x.charCodeAt(0));
  249. let data = pako.inflate(new Uint8Array(charData));
  250. let result = String.fromCharCode.apply(null, new Uint16Array(data));
  251. try {
  252. return unescape(result);
  253. } catch (ee) {
  254. return result;
  255. }
  256. } catch (e) {
  257. return 'Error: 当前字符串不能被Gzip解密';
  258. }
  259. };
  260. /**
  261. * 字符串与Hex编码互转
  262. * @param input
  263. * @returns {string}
  264. */
  265. let hexTools = (function (input) {
  266. let utf8encode = function (str, isGetBytes) {
  267. let back = [];
  268. let byteSize = 0;
  269. for (let i = 0; i < str.length; i++) {
  270. let code = str.charCodeAt(i);
  271. if (0x00 <= code && code <= 0x7f) {
  272. byteSize += 1;
  273. back.push(code);
  274. } else if (0x80 <= code && code <= 0x7ff) {
  275. byteSize += 2;
  276. back.push((192 | (31 & (code >> 6))));
  277. back.push((128 | (63 & code)))
  278. } else if ((0x800 <= code && code <= 0xd7ff)
  279. || (0xe000 <= code && code <= 0xffff)) {
  280. byteSize += 3;
  281. back.push((224 | (15 & (code >> 12))));
  282. back.push((128 | (63 & (code >> 6))));
  283. back.push((128 | (63 & code)))
  284. }
  285. }
  286. for (i = 0; i < back.length; i++) {
  287. back[i] &= 0xff;
  288. }
  289. if (isGetBytes) {
  290. return back
  291. }
  292. if (byteSize <= 0xff) {
  293. return [0, byteSize].concat(back);
  294. } else {
  295. return [byteSize >> 8, byteSize & 0xff].concat(back);
  296. }
  297. };
  298. let utf8decode = function (arr) {
  299. if (typeof arr === 'string') {
  300. return arr;
  301. }
  302. let UTF = '', _arr = arr;
  303. for (let i = 0; i < _arr.length; i++) {
  304. let one = _arr[i].toString(2),
  305. v = one.match(/^1+?(?=0)/);
  306. if (v && one.length === 8) {
  307. let bytesLength = v[0].length;
  308. let store = _arr[i].toString(2).slice(7 - bytesLength);
  309. for (let st = 1; st < bytesLength; st++) {
  310. store += _arr[st + i].toString(2).slice(2)
  311. }
  312. UTF += String.fromCharCode(parseInt(store, 2));
  313. i += bytesLength - 1
  314. } else {
  315. UTF += String.fromCharCode(_arr[i])
  316. }
  317. }
  318. return UTF
  319. };
  320. let hexEncode = function (str) {
  321. let charBuf = utf8encode(str, true);
  322. let re = '';
  323. for (let i = 0; i < charBuf.length; i++) {
  324. let x = (charBuf[i] & 0xFF).toString(16);
  325. if (x.length === 1) {
  326. x = '0' + x;
  327. }
  328. re += x;
  329. }
  330. return re;
  331. };
  332. let hexDecode = function (str) {
  333. let buf = [];
  334. for (let i = 0; i < str.length; i += 2) {
  335. buf.push(parseInt(str.substring(i, i + 2), 16));
  336. }
  337. return utf8decode(buf);
  338. };
  339. return {hexEncode, hexDecode};
  340. })();
  341. /**
  342. * html代码转换成js
  343. * @param txt
  344. * @returns {string}
  345. */
  346. let _html2js = function (txt) {
  347. let htmlArr = txt.replace(/\\/g, "\\\\").replace(/\\/g, "\\/").replace(/\'/g, "\\\'").split('\n');
  348. let len = htmlArr.length;
  349. let outArr = [];
  350. outArr.push("let htmlCodes = [\n");
  351. htmlArr.forEach((value, index) => {
  352. if (value !== "") {
  353. if (index === len - 1) {
  354. outArr.push("\'" + value + "\'");
  355. } else {
  356. outArr.push("\'" + value + "\',\n");
  357. }
  358. }
  359. });
  360. outArr.push("\n].join(\"\");");
  361. return outArr.join("");
  362. };
  363. return {
  364. uniEncode: _uniEncode,
  365. uniDecode: _uniDecode,
  366. base64Encode: _base64Encode,
  367. base64Decode: _base64Decode,
  368. utf8Encode: _utf8Encode,
  369. utf8Decode: _utf8Decode,
  370. utf16to8: _utf16to8,
  371. utf8to16: _utf8to16,
  372. md5: md5,
  373. gzipEncode: gzipEncode,
  374. gzipDecode: gzipDecode,
  375. hexEncode: hexTools.hexEncode,
  376. hexDecode: hexTools.hexDecode,
  377. html2js: _html2js
  378. };
  379. })();