Paul 5 years ago
parent
commit
22275e3f0d
4 changed files with 40 additions and 37 deletions
  1. 1
    0
      dist/types/utils.d.ts
  2. 15
    15
      dist/utils.js
  3. 1
    1
      dist/utils.js.map
  4. 23
    21
      src/utils.ts

+ 1
- 0
dist/types/utils.d.ts View File

@@ -9,5 +9,6 @@ declare class Utils {
9 9
     static base64ToBin(str: string): string;
10 10
     static stringToBin(str: string): string;
11 11
     static binToString(bin: string): string;
12
+    private static makeCRCTable;
12 13
 }
13 14
 export { Utils };

+ 15
- 15
dist/utils.js View File

@@ -1,23 +1,11 @@
1 1
 "use strict";
2 2
 exports.__esModule = true;
3 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 4
 var Utils = /** @class */ (function () {
17 5
     function Utils() {
18 6
     }
19 7
     Utils.crc32 = function (str) {
20
-        var crcTable = window.crcTable || (window.crcTable = makeCRCTable());
8
+        var crcTable = window.crcTable || (window.crcTable = Utils.makeCRCTable());
21 9
         var crc = 0 ^ -1;
22 10
         for (var i = 0; i < str.length; i++) {
23 11
             crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xff];
@@ -75,11 +63,11 @@ var Utils = /** @class */ (function () {
75 63
         var bitStringTemp2 = bitString.substr(bitString.length - tail, tail);
76 64
         for (var i = 0; i < bitStringTemp1.length; i += 6) {
77 65
             var index = parseInt(bitStringTemp1.substr(i, 6), 2);
78
-            result += this.code[index];
66
+            result += Utils.code[index];
79 67
         }
80 68
         bitStringTemp2 += new Array(7 - tail).join('0');
81 69
         if (tail) {
82
-            result += this.code[parseInt(bitStringTemp2, 2)];
70
+            result += Utils.code[parseInt(bitStringTemp2, 2)];
83 71
             result += new Array((6 - tail) / 2 + 1).join('=');
84 72
         }
85 73
         return result;
@@ -116,6 +104,18 @@ var Utils = /** @class */ (function () {
116 104
         }
117 105
         return result;
118 106
     };
107
+    Utils.makeCRCTable = function () {
108
+        var c;
109
+        var crcTable = [];
110
+        for (var n = 0; n < 256; n++) {
111
+            c = n;
112
+            for (var k = 0; k < 8; k++) {
113
+                c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
114
+            }
115
+            crcTable[n] = c;
116
+        }
117
+        return crcTable;
118
+    };
119 119
     Utils.code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
120 120
     return Utils;
121 121
 }());

+ 1
- 1
dist/utils.js.map
File diff suppressed because it is too large
View File


+ 23
- 21
src/utils.ts View File

@@ -1,18 +1,5 @@
1 1
 import { AES, enc, mode, pad } from 'crypto-js';
2 2
 
3
-const makeCRCTable = function() {
4
-  var c;
5
-  var crcTable = [];
6
-  for (var n = 0; n < 256; n++) {
7
-    c = n;
8
-    for (var k = 0; k < 8; k++) {
9
-      c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
10
-    }
11
-    crcTable[n] = c;
12
-  }
13
-  return crcTable;
14
-};
15
-
16 3
 class Utils {
17 4
   private static code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split(
18 5
     '',
@@ -20,7 +7,7 @@ class Utils {
20 7
 
21 8
   public static crc32(str: string): number {
22 9
     let crcTable =
23
-      (<any>window).crcTable || ((<any>window).crcTable = makeCRCTable());
10
+      (<any>window).crcTable || ((<any>window).crcTable = Utils.makeCRCTable());
24 11
     let crc = 0 ^ -1;
25 12
 
26 13
     for (let i = 0; i < str.length; i++) {
@@ -49,7 +36,7 @@ class Utils {
49 36
   }
50 37
 
51 38
   // 字符串转为 ArrayBuffer 对象,参数为字符串
52
-  static str2ab(str: string): ArrayBuffer {
39
+  public static str2ab(str: string): ArrayBuffer {
53 40
     let buf = new ArrayBuffer(str.length); // 每个字符占用2个字节
54 41
     let bufView = new Uint8Array(buf);
55 42
 
@@ -61,7 +48,7 @@ class Utils {
61 48
   }
62 49
 
63 50
   // 解密服务端传递过来的字符串
64
-  static decrypt(data: string, key: string, iv: string): string {
51
+  public static decrypt(data: string, key: string, iv: string): string {
65 52
     const binData = Utils.stringToBin(data);
66 53
     const base64Data = Utils.binToBase64(binData);
67 54
 
@@ -75,7 +62,7 @@ class Utils {
75 62
   }
76 63
 
77 64
   // 加密字符串以后传递到服务端
78
-  static encrypt(data: string, key: string, iv: string): string {
65
+  public static encrypt(data: string, key: string, iv: string): string {
79 66
     const result = AES.encrypt(data, enc.Latin1.parse(key), {
80 67
       iv: enc.Latin1.parse(iv),
81 68
       mode: mode.CBC,
@@ -86,7 +73,7 @@ class Utils {
86 73
   }
87 74
 
88 75
   // 字节数组转换为base64编码
89
-  static binToBase64(bitString: string): string {
76
+  public static binToBase64(bitString: string): string {
90 77
     let result = '';
91 78
     let tail = bitString.length % 6;
92 79
     let bitStringTemp1 = bitString.substr(0, bitString.length - tail);
@@ -94,12 +81,12 @@ class Utils {
94 81
 
95 82
     for (let i = 0; i < bitStringTemp1.length; i += 6) {
96 83
       let index = parseInt(bitStringTemp1.substr(i, 6), 2);
97
-      result += this.code[index];
84
+      result += Utils.code[index];
98 85
     }
99 86
 
100 87
     bitStringTemp2 += new Array(7 - tail).join('0');
101 88
     if (tail) {
102
-      result += this.code[parseInt(bitStringTemp2, 2)];
89
+      result += Utils.code[parseInt(bitStringTemp2, 2)];
103 90
       result += new Array((6 - tail) / 2 + 1).join('=');
104 91
     }
105 92
 
@@ -107,7 +94,7 @@ class Utils {
107 94
   }
108 95
 
109 96
   // base64编码转换为字节数组
110
-  static base64ToBin(str: string): string {
97
+  public static base64ToBin(str: string): string {
111 98
     let bitString = '';
112 99
     let tail = 0;
113 100
 
@@ -143,6 +130,21 @@ class Utils {
143 130
 
144 131
     return result;
145 132
   }
133
+
134
+  private static makeCRCTable(): number[] {
135
+    let c: number;
136
+    let crcTable: number[] = [];
137
+
138
+    for (let n = 0; n < 256; n++) {
139
+      c = n;
140
+      for (let k = 0; k < 8; k++) {
141
+        c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
142
+      }
143
+      crcTable[n] = c;
144
+    }
145
+
146
+    return crcTable;
147
+  }
146 148
 }
147 149
 
148 150
 export { Utils };