|
@@ -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 };
|