Paul 5 years ago
parent
commit
be483f1f6a
5 changed files with 38 additions and 20 deletions
  1. 5
    5
      dist/types/utils.d.ts
  2. 2
    2
      dist/utils.js
  3. 1
    1
      dist/utils.js.map
  4. 10
    10
      src/utils.ts
  5. 20
    2
      test/utils.test.ts

+ 5
- 5
dist/types/utils.d.ts View File

@@ -3,11 +3,11 @@ declare class Utils {
3 3
     static crc32(str: string): number;
4 4
     static ab2str(buf: ArrayBuffer): string;
5 5
     static str2ab(str: string): ArrayBuffer;
6
-    static decrypt(data: any, key: any, iv: any): any;
7
-    static encrypt(data: any, key: any, iv: any): string;
8
-    static binToBase64(bitString: any): string;
9
-    static base64ToBin(str: any): string;
6
+    static decrypt(data: string, key: string, iv: string): string;
7
+    static encrypt(data: string, key: string, iv: string): string;
8
+    static binToBase64(bitString: string): string;
9
+    static base64ToBin(str: string): string;
10 10
     static stringToBin(str: string): string;
11
-    static binToStr(bin: string): string;
11
+    static binToString(bin: string): string;
12 12
 }
13 13
 export { Utils };

+ 2
- 2
dist/utils.js View File

@@ -65,7 +65,7 @@ var Utils = /** @class */ (function () {
65 65
             mode: crypto_js_1.mode.CBC,
66 66
             padding: crypto_js_1.pad.Pkcs7
67 67
         });
68
-        return Utils.binToStr(Utils.base64ToBin(result.toString()));
68
+        return Utils.binToString(Utils.base64ToBin(result.toString()));
69 69
     };
70 70
     // 字节数组转换为base64编码
71 71
     Utils.binToBase64 = function (bitString) {
@@ -109,7 +109,7 @@ var Utils = /** @class */ (function () {
109 109
         return result;
110 110
     };
111 111
     // 字节数组转化为字符串
112
-    Utils.binToStr = function (bin) {
112
+    Utils.binToString = function (bin) {
113 113
         var result = '';
114 114
         for (var i = 0; i < bin.length; i += 8) {
115 115
             result += String.fromCharCode(parseInt(bin.substr(i, 8), 2));

+ 1
- 1
dist/utils.js.map View File

@@ -1 +1 @@
1
-{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;AAAA,uCAAgD;AAEhD,IAAM,YA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I,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC;SAC/D;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,aAAa;IACC,iBAAW,GAAzB,UAA0B,GAAW;QACnC,IAAI,MAAM,GAAG,EAAE,CAAC;QAChB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE;YACtC,MAAM,IAAI,MAAM,CAAC,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;SAC9D;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAhIc,UAAI,GAAG,kEAAkE,CAAC,KAAK,CAC5F,EAAE,CACH,CAAC;IA+HJ,YAAC;CAAA,AAlID,IAkIC;AAEQ,sBAAK"}

+ 10
- 10
src/utils.ts View File

@@ -61,11 +61,11 @@ class Utils {
61 61
   }
62 62
 
63 63
   // 解密服务端传递过来的字符串
64
-  static decrypt(data, key, iv) {
65
-    let binData = Utils.stringToBin(data);
66
-    let base64Data = Utils.binToBase64(binData);
64
+  static decrypt(data: string, key: string, iv: string): string {
65
+    const binData = Utils.stringToBin(data);
66
+    const base64Data = Utils.binToBase64(binData);
67 67
 
68
-    let bytes = AES.decrypt(base64Data, enc.Latin1.parse(key), {
68
+    const bytes = AES.decrypt(base64Data, enc.Latin1.parse(key), {
69 69
       iv: enc.Latin1.parse(iv),
70 70
       mode: mode.CBC,
71 71
       padding: pad.Pkcs7,
@@ -75,18 +75,18 @@ class Utils {
75 75
   }
76 76
 
77 77
   // 加密字符串以后传递到服务端
78
-  static encrypt(data, key, iv) {
79
-    let result = AES.encrypt(data, enc.Latin1.parse(key), {
78
+  static encrypt(data: string, key: string, iv: string): string {
79
+    const result = AES.encrypt(data, enc.Latin1.parse(key), {
80 80
       iv: enc.Latin1.parse(iv),
81 81
       mode: mode.CBC,
82 82
       padding: pad.Pkcs7,
83 83
     });
84 84
 
85
-    return Utils.binToStr(Utils.base64ToBin(result.toString()));
85
+    return Utils.binToString(Utils.base64ToBin(result.toString()));
86 86
   }
87 87
 
88 88
   // 字节数组转换为base64编码
89
-  static binToBase64(bitString) {
89
+  static binToBase64(bitString: string): string {
90 90
     let result = '';
91 91
     let tail = bitString.length % 6;
92 92
     let bitStringTemp1 = bitString.substr(0, bitString.length - tail);
@@ -107,7 +107,7 @@ class Utils {
107 107
   }
108 108
 
109 109
   // base64编码转换为字节数组
110
-  static base64ToBin(str) {
110
+  static base64ToBin(str: string): string {
111 111
     let bitString = '';
112 112
     let tail = 0;
113 113
 
@@ -135,7 +135,7 @@ class Utils {
135 135
   }
136 136
 
137 137
   // 字节数组转化为字符串
138
-  public static binToStr(bin: string): string {
138
+  public static binToString(bin: string): string {
139 139
     let result = '';
140 140
     for (let i = 0; i < bin.length; i += 8) {
141 141
       result += String.fromCharCode(parseInt(bin.substr(i, 8), 2));

+ 20
- 2
test/utils.test.ts View File

@@ -1,6 +1,24 @@
1 1
 import { Utils } from '../src/utils';
2 2
 
3
-test('object assignment', () => {
3
+test('utils crc32', () => {
4 4
   const v = Utils.crc32('/v1/session/init');
5
-  console.log(v);
5
+  expect(v).toBe(1897767088);
6
+});
7
+
8
+test('utils str2ab and ab2str', () => {
9
+  expect(Utils.ab2str(Utils.str2ab('test'))).toBe('test');
10
+});
11
+
12
+test('utils encrypt and decrypt', () => {
13
+  const key = 'b8ca9aa66def05ff3f24919274bb4a66';
14
+  const iv = key;
15
+  expect(Utils.decrypt(Utils.encrypt('test', key, iv), key, iv)).toBe('test');
16
+});
17
+
18
+test('utils binToBase64 and base64ToBin', () => {
19
+  expect(Utils.binToBase64(Utils.base64ToBin('test'))).toBe('test');
20
+});
21
+
22
+test('utils stringToBin and binToString', () => {
23
+  expect(Utils.binToString(Utils.stringToBin('test'))).toBe('test');
6 24
 });