Browse Source

first commit

Paul 5 years ago
commit
456cc2ea2e
10 changed files with 343 additions and 0 deletions
  1. 16
    0
      .editorconfig
  2. 1
    0
      .gitignore
  3. 8
    0
      .prettierrc
  4. 13
    0
      dist/types/utils.d.ts
  5. 123
    0
      dist/utils.js
  6. 1
    0
      dist/utils.js.map
  7. 14
    0
      package.json
  8. 148
    0
      src/utils.ts
  9. 12
    0
      tsconfig.json
  10. 7
    0
      yarn.lock

+ 16
- 0
.editorconfig View File

@@ -0,0 +1,16 @@
1
+# http://editorconfig.org
2
+root = true
3
+
4
+[*]
5
+indent_style = space
6
+indent_size = 2
7
+end_of_line = lf
8
+charset = utf-8
9
+trim_trailing_whitespace = true
10
+insert_final_newline = true
11
+
12
+[*.md]
13
+trim_trailing_whitespace = false
14
+
15
+[Makefile]
16
+indent_style = tab

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
1
+node_modules

+ 8
- 0
.prettierrc View File

@@ -0,0 +1,8 @@
1
+{
2
+  "trailingComma": "all",
3
+  "tabWidth": 2,
4
+  "indentStyle": "es5",
5
+  "semi": true,
6
+  "arrowParens": "always",
7
+  "singleQuote": true
8
+}

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

@@ -0,0 +1,13 @@
1
+declare class Utils {
2
+    private static code;
3
+    static crc32(str: string): number;
4
+    static ab2str(buf: ArrayBuffer): string;
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;
10
+    static stringToBin(str: string): string;
11
+    static binToStr(bin: string): string;
12
+}
13
+export { Utils };

+ 123
- 0
dist/utils.js View File

@@ -0,0 +1,123 @@
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.binToStr(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.binToStr = 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

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

@@ -0,0 +1 @@
1
+{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;AAAA,uCAAgD;AAEhD,IAAM,YAAY,GAAG;IACnB,IAAI,CAAC,CAAC;IACN,IAAI,QAAQ,GAAG,EAAE,CAAC;IAClB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE;QAC5B,CAAC,GAAG,CAAC,CAAC;QACN,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE;YAC1B,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;SAC9C;QACD,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;KACjB;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC;AAEF;IAAA;IAkIA,CAAC;IA7He,WAAK,GAAnB,UAAoB,GAAW;QAC7B,IAAI,QAAQ,GACJ,MAAO,CAAC,QAAQ,IAAI,CAAO,MAAO,CAAC,QAAQ,GAAG,YAAY,EAAE,CAAC,CAAC;QACtE,IAAI,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;QAEjB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YACnC,GAAG,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,GAAG,G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
3
+  "version": "1.0.0",
4
+  "description": "",
5
+  "main": "index.js",
6
+  "scripts": {
7
+    "test": "echo \"Error: no test specified\" && exit 1"
8
+  },
9
+  "author": "",
10
+  "license": "ISC",
11
+  "dependencies": {
12
+    "crypto-js": "^3.1.9-1"
13
+  }
14
+}

+ 148
- 0
src/utils.ts View File

@@ -0,0 +1,148 @@
1
+import { AES, enc, mode, pad } from 'crypto-js';
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
+class Utils {
17
+  private static code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split(
18
+    '',
19
+  );
20
+
21
+  public static crc32(str: string): number {
22
+    let crcTable =
23
+      (<any>window).crcTable || ((<any>window).crcTable = makeCRCTable());
24
+    let crc = 0 ^ -1;
25
+
26
+    for (let i = 0; i < str.length; i++) {
27
+      crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xff];
28
+    }
29
+
30
+    return (crc ^ -1) >>> 0;
31
+  }
32
+
33
+  // ArrayBuffer 转为字符串,参数为 ArrayBuffer 对象
34
+  public static ab2str(buf: ArrayBuffer): string {
35
+    // 注意,如果是大型二进制数组,为了避免溢出,必须一个一个字符地转
36
+    if (buf && buf.byteLength < 1024) {
37
+      return String.fromCharCode.apply(null, new Uint8Array(buf));
38
+    }
39
+
40
+    let bufView = new Uint8Array(buf);
41
+    let len = bufView.length;
42
+    let byteStr = new Array(len);
43
+
44
+    for (let i = 0; i < len; i++) {
45
+      byteStr[i] = String.fromCharCode.call(null, bufView[i]);
46
+    }
47
+
48
+    return byteStr.join('');
49
+  }
50
+
51
+  // 字符串转为 ArrayBuffer 对象,参数为字符串
52
+  static str2ab(str: string): ArrayBuffer {
53
+    let buf = new ArrayBuffer(str.length); // 每个字符占用2个字节
54
+    let bufView = new Uint8Array(buf);
55
+
56
+    for (let i = 0, strLen = str.length; i < strLen; i++) {
57
+      bufView[i] = str.charCodeAt(i);
58
+    }
59
+
60
+    return buf;
61
+  }
62
+
63
+  // 解密服务端传递过来的字符串
64
+  static decrypt(data, key, iv) {
65
+    let binData = Utils.stringToBin(data);
66
+    let base64Data = Utils.binToBase64(binData);
67
+
68
+    let bytes = AES.decrypt(base64Data, enc.Latin1.parse(key), {
69
+      iv: enc.Latin1.parse(iv),
70
+      mode: mode.CBC,
71
+      padding: pad.Pkcs7,
72
+    });
73
+
74
+    return bytes.toString(enc.Utf8);
75
+  }
76
+
77
+  // 加密字符串以后传递到服务端
78
+  static encrypt(data, key, iv) {
79
+    let result = AES.encrypt(data, enc.Latin1.parse(key), {
80
+      iv: enc.Latin1.parse(iv),
81
+      mode: mode.CBC,
82
+      padding: pad.Pkcs7,
83
+    });
84
+
85
+    return Utils.binToStr(Utils.base64ToBin(result.toString()));
86
+  }
87
+
88
+  // 字节数组转换为base64编码
89
+  static binToBase64(bitString) {
90
+    let result = '';
91
+    let tail = bitString.length % 6;
92
+    let bitStringTemp1 = bitString.substr(0, bitString.length - tail);
93
+    let bitStringTemp2 = bitString.substr(bitString.length - tail, tail);
94
+
95
+    for (let i = 0; i < bitStringTemp1.length; i += 6) {
96
+      let index = parseInt(bitStringTemp1.substr(i, 6), 2);
97
+      result += this.code[index];
98
+    }
99
+
100
+    bitStringTemp2 += new Array(7 - tail).join('0');
101
+    if (tail) {
102
+      result += this.code[parseInt(bitStringTemp2, 2)];
103
+      result += new Array((6 - tail) / 2 + 1).join('=');
104
+    }
105
+
106
+    return result;
107
+  }
108
+
109
+  // base64编码转换为字节数组
110
+  static base64ToBin(str) {
111
+    let bitString = '';
112
+    let tail = 0;
113
+
114
+    for (let i = 0; i < str.length; i++) {
115
+      if (str[i] !== '=') {
116
+        let decode = this.code.indexOf(str[i]).toString(2);
117
+        bitString += new Array(7 - decode.length).join('0') + decode;
118
+      } else {
119
+        tail++;
120
+      }
121
+    }
122
+
123
+    return bitString.substr(0, bitString.length - tail * 2);
124
+  }
125
+
126
+  // 字符串转换为字节数组
127
+  public static stringToBin(str: string): string {
128
+    let result = '';
129
+    for (let i = 0; i < str.length; i++) {
130
+      let charCode = str.charCodeAt(i).toString(2);
131
+      result += new Array(9 - charCode.length).join('0') + charCode;
132
+    }
133
+
134
+    return result;
135
+  }
136
+
137
+  // 字节数组转化为字符串
138
+  public static binToStr(bin: string): string {
139
+    let result = '';
140
+    for (let i = 0; i < bin.length; i += 8) {
141
+      result += String.fromCharCode(parseInt(bin.substr(i, 8), 2));
142
+    }
143
+
144
+    return result;
145
+  }
146
+}
147
+
148
+export { Utils };

+ 12
- 0
tsconfig.json View File

@@ -0,0 +1,12 @@
1
+{
2
+  "compileOnSave": true,
3
+  "compilerOptions": {
4
+    "alwaysStrict": true,
5
+    "sourceMap": true,
6
+    "declaration": true,
7
+    "rootDir": "src",
8
+    "outDir": "dist",
9
+    "declarationDir": "dist/types"
10
+  },
11
+  "include": ["src/**/*"]
12
+}

+ 7
- 0
yarn.lock View File

@@ -0,0 +1,7 @@
1
+# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2
+# yarn lockfile v1
3
+
4
+
5
+crypto-js@^3.1.9-1:
6
+  version "3.1.9-1"
7
+  resolved "http://registry.npm.taobao.org/crypto-js/download/crypto-js-3.1.9-1.tgz#fda19e761fc077e01ffbfdc6e9fdfc59e8806cd8"