endecode-lib.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. text = escape(text.toString()).replace(/\+/g, "%2B");
  29. let matches = text.match(/(%([0-9A-F]{2}))/gi);
  30. if (matches) {
  31. for (let matchid = 0; matchid < matches.length; matchid++) {
  32. let code = matches[matchid].substring(1, 3);
  33. if (parseInt(code, 16) >= 128) {
  34. text = text.replace(matches[matchid], '%u00' + code);
  35. }
  36. }
  37. }
  38. text = text.replace('%25', '%u0025').replace(/%/g, "\\");
  39. return text;
  40. };
  41. /**
  42. * 此方法用于将Unicode码解码为正常字符串
  43. * @param {Object} text
  44. */
  45. let _uniDecode = function (text) {
  46. text = text.replace(/\\/g, "%").replace('%U', '%u').replace('%u0025', '%25');
  47. text = unescape(text.toString().replace(/%2B/g, "+"));
  48. let matches = text.match(/(%u00([0-9A-F]{2}))/gi);
  49. if (matches) {
  50. for (let matchid = 0; matchid < matches.length; matchid++) {
  51. let code = matches[matchid].substring(1, 3);
  52. let x = Number("0x" + code);
  53. if (x >= 128) {
  54. text = text.replace(matches[matchid], code);
  55. }
  56. }
  57. }
  58. text = unescape(text.toString().replace(/%2B/g, "+"));
  59. return text;
  60. };
  61. /**
  62. * 此方法用于将文字进行UTF-8编码
  63. * @param {Object} str 源码
  64. * @return {String} UTF-8码
  65. */
  66. let _utf8Encode = function (str) {
  67. let out, i, len, c;
  68. out = "";
  69. len = str.length;
  70. for (i = 0; i < len; i++) {
  71. c = str.charCodeAt(i);
  72. if ((c >= 0x0001) && (c <= 0x007F)) {
  73. out += str.charAt(i);
  74. } else if (c > 0x07FF) {
  75. out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
  76. out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
  77. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  78. } else {
  79. out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
  80. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  81. }
  82. }
  83. return out;
  84. };
  85. /**
  86. * 此方法用于将文字进行UTF-8解码
  87. * @param {Object} str
  88. * @return {String} 原文字
  89. */
  90. let _utf8Decode = function (str) {
  91. let out, i, len, c;
  92. let char2, char3;
  93. out = "";
  94. len = str.length;
  95. i = 0;
  96. while (i < len) {
  97. c = str.charCodeAt(i++);
  98. switch (c >> 4) {
  99. case 0:
  100. case 1:
  101. case 2:
  102. case 3:
  103. case 4:
  104. case 5:
  105. case 6:
  106. case 7:
  107. // 0xxxxxxx
  108. out += str.charAt(i - 1);
  109. break;
  110. case 12:
  111. case 13:
  112. // 110x xxxx  10xx xxxx
  113. char2 = str.charCodeAt(i++);
  114. out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
  115. break;
  116. case 14:
  117. // 1110 xxxx 10xx xxxx 10xx xxxx
  118. char2 = str.charCodeAt(i++);
  119. char3 = str.charCodeAt(i++);
  120. out += String.fromCharCode(((c & 0x0F) << 12) |
  121. ((char2 & 0x3F) << 6) |
  122. ((char3 & 0x3F) << 0));
  123. break;
  124. }
  125. }
  126. return out;
  127. };
  128. /**
  129. * 此方法用于将文字进行base64编码
  130. * @param {Object} str 源码
  131. * @return {String} base64码
  132. */
  133. let _base64Encode = function (str) {
  134. let out, i, len;
  135. let c1, c2, c3;
  136. len = str.length;
  137. i = 0;
  138. out = "";
  139. while (i < len) {
  140. c1 = str.charCodeAt(i++) & 0xff;
  141. if (i == len) {
  142. out += _base64EncodeChars.charAt(c1 >> 2);
  143. out += _base64EncodeChars.charAt((c1 & 0x3) << 4);
  144. out += "==";
  145. break;
  146. }
  147. c2 = str.charCodeAt(i++);
  148. if (i == len) {
  149. out += _base64EncodeChars.charAt(c1 >> 2);
  150. out += _base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  151. out += _base64EncodeChars.charAt((c2 & 0xF) << 2);
  152. out += "=";
  153. break;
  154. }
  155. c3 = str.charCodeAt(i++);
  156. out += _base64EncodeChars.charAt(c1 >> 2);
  157. out += _base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  158. out += _base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
  159. out += _base64EncodeChars.charAt(c3 & 0x3F);
  160. }
  161. return out;
  162. };
  163. /**
  164. * 此方法用于将文字进行base64解码
  165. * @param {Object} str 源码
  166. * @return {String} 源码
  167. */
  168. let _base64Decode = function (str) {
  169. let c1, c2, c3, c4;
  170. let i, len, out;
  171. len = str.length;
  172. i = 0;
  173. out = "";
  174. while (i < len) {
  175. /* c1 */
  176. do {
  177. c1 = _base64DecodeChars[str.charCodeAt(i++) & 0xff];
  178. }
  179. while (i < len && c1 == -1);
  180. if (c1 == -1)
  181. break;
  182. /* c2 */
  183. do {
  184. c2 = _base64DecodeChars[str.charCodeAt(i++) & 0xff];
  185. }
  186. while (i < len && c2 == -1);
  187. if (c2 == -1)
  188. break;
  189. out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
  190. /* c3 */
  191. do {
  192. c3 = str.charCodeAt(i++) & 0xff;
  193. if (c3 == 61)
  194. return out;
  195. c3 = _base64DecodeChars[c3];
  196. }
  197. while (i < len && c3 == -1);
  198. if (c3 == -1)
  199. break;
  200. out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
  201. /* c4 */
  202. do {
  203. c4 = str.charCodeAt(i++) & 0xff;
  204. if (c4 == 61)
  205. return out;
  206. c4 = _base64DecodeChars[c4];
  207. }
  208. while (i < len && c4 == -1);
  209. if (c4 == -1)
  210. break;
  211. out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
  212. }
  213. return out;
  214. };
  215. /**
  216. * 中文,一般情况下Unicode是UTF-16实现,长度2位,而UTF-8编码是3位
  217. * @param str
  218. * @return {String}
  219. */
  220. let _utf16to8 = function (str) {
  221. let out, i, len, c;
  222. out = "";
  223. len = str.length;
  224. for (i = 0; i < len; i++) {
  225. c = str.charCodeAt(i);
  226. if ((c >= 0x0001) && (c <= 0x007F)) {
  227. out += str.charAt(i);
  228. } else if (c > 0x07FF) {
  229. out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
  230. out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
  231. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  232. } else {
  233. out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
  234. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  235. }
  236. }
  237. return out;
  238. };
  239. /**
  240. * md5加密
  241. * @param str
  242. */
  243. let md5 = (str) => {
  244. let tools = Tarp.require('./md5');
  245. return tools.hex_md5(str);
  246. };
  247. return {
  248. uniEncode: _uniEncode,
  249. uniDecode: _uniDecode,
  250. base64Encode: _base64Encode,
  251. base64Decode: _base64Decode,
  252. utf8Encode: _utf8Encode,
  253. utf8Decode: _utf8Decode,
  254. utf16to8: _utf16to8,
  255. md5: md5
  256. };
  257. })();