ts-sdk

utils.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. exports.__esModule = true;
  3. var crypto_js_1 = require("crypto-js");
  4. var makeCRCTable = function () {
  5. var c;
  6. var crcTable = [];
  7. for (var n = 0; n < 256; n++) {
  8. c = n;
  9. for (var k = 0; k < 8; k++) {
  10. c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
  11. }
  12. crcTable[n] = c;
  13. }
  14. return crcTable;
  15. };
  16. var Utils = /** @class */ (function () {
  17. function Utils() {
  18. }
  19. Utils.crc32 = function (str) {
  20. var crcTable = window.crcTable || (window.crcTable = makeCRCTable());
  21. var crc = 0 ^ -1;
  22. for (var i = 0; i < str.length; i++) {
  23. crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xff];
  24. }
  25. return (crc ^ -1) >>> 0;
  26. };
  27. // ArrayBuffer 转为字符串,参数为 ArrayBuffer 对象
  28. Utils.ab2str = function (buf) {
  29. // 注意,如果是大型二进制数组,为了避免溢出,必须一个一个字符地转
  30. if (buf && buf.byteLength < 1024) {
  31. return String.fromCharCode.apply(null, new Uint8Array(buf));
  32. }
  33. var bufView = new Uint8Array(buf);
  34. var len = bufView.length;
  35. var byteStr = new Array(len);
  36. for (var i = 0; i < len; i++) {
  37. byteStr[i] = String.fromCharCode.call(null, bufView[i]);
  38. }
  39. return byteStr.join('');
  40. };
  41. // 字符串转为 ArrayBuffer 对象,参数为字符串
  42. Utils.str2ab = function (str) {
  43. var buf = new ArrayBuffer(str.length); // 每个字符占用2个字节
  44. var bufView = new Uint8Array(buf);
  45. for (var i = 0, strLen = str.length; i < strLen; i++) {
  46. bufView[i] = str.charCodeAt(i);
  47. }
  48. return buf;
  49. };
  50. // 解密服务端传递过来的字符串
  51. Utils.decrypt = function (data, key, iv) {
  52. var binData = Utils.stringToBin(data);
  53. var base64Data = Utils.binToBase64(binData);
  54. var bytes = crypto_js_1.AES.decrypt(base64Data, crypto_js_1.enc.Latin1.parse(key), {
  55. iv: crypto_js_1.enc.Latin1.parse(iv),
  56. mode: crypto_js_1.mode.CBC,
  57. padding: crypto_js_1.pad.Pkcs7
  58. });
  59. return bytes.toString(crypto_js_1.enc.Utf8);
  60. };
  61. // 加密字符串以后传递到服务端
  62. Utils.encrypt = function (data, key, iv) {
  63. var result = crypto_js_1.AES.encrypt(data, crypto_js_1.enc.Latin1.parse(key), {
  64. iv: crypto_js_1.enc.Latin1.parse(iv),
  65. mode: crypto_js_1.mode.CBC,
  66. padding: crypto_js_1.pad.Pkcs7
  67. });
  68. return Utils.binToString(Utils.base64ToBin(result.toString()));
  69. };
  70. // 字节数组转换为base64编码
  71. Utils.binToBase64 = function (bitString) {
  72. var result = '';
  73. var tail = bitString.length % 6;
  74. var bitStringTemp1 = bitString.substr(0, bitString.length - tail);
  75. var bitStringTemp2 = bitString.substr(bitString.length - tail, tail);
  76. for (var i = 0; i < bitStringTemp1.length; i += 6) {
  77. var index = parseInt(bitStringTemp1.substr(i, 6), 2);
  78. result += this.code[index];
  79. }
  80. bitStringTemp2 += new Array(7 - tail).join('0');
  81. if (tail) {
  82. result += this.code[parseInt(bitStringTemp2, 2)];
  83. result += new Array((6 - tail) / 2 + 1).join('=');
  84. }
  85. return result;
  86. };
  87. // base64编码转换为字节数组
  88. Utils.base64ToBin = function (str) {
  89. var bitString = '';
  90. var tail = 0;
  91. for (var i = 0; i < str.length; i++) {
  92. if (str[i] !== '=') {
  93. var decode = this.code.indexOf(str[i]).toString(2);
  94. bitString += new Array(7 - decode.length).join('0') + decode;
  95. }
  96. else {
  97. tail++;
  98. }
  99. }
  100. return bitString.substr(0, bitString.length - tail * 2);
  101. };
  102. // 字符串转换为字节数组
  103. Utils.stringToBin = function (str) {
  104. var result = '';
  105. for (var i = 0; i < str.length; i++) {
  106. var charCode = str.charCodeAt(i).toString(2);
  107. result += new Array(9 - charCode.length).join('0') + charCode;
  108. }
  109. return result;
  110. };
  111. // 字节数组转化为字符串
  112. Utils.binToString = function (bin) {
  113. var result = '';
  114. for (var i = 0; i < bin.length; i += 8) {
  115. result += String.fromCharCode(parseInt(bin.substr(i, 8), 2));
  116. }
  117. return result;
  118. };
  119. Utils.code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
  120. return Utils;
  121. }());
  122. exports.Utils = Utils;
  123. //# sourceMappingURL=utils.js.map