@ruder/object-zip 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/base64Crypt.js +120 -0
- package/index.js +23 -13
- package/package.json +13 -13
package/base64Crypt.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// Base64字符集
|
|
2
|
+
const BASE64_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
|
3
|
+
|
|
4
|
+
// 预先计算字符映射表(显著提高性能)
|
|
5
|
+
const CHAR_TO_INDEX = new Array(256);
|
|
6
|
+
const INDEX_TO_CHAR = new Array(65); // 0-64
|
|
7
|
+
|
|
8
|
+
for (let i = 0; i < BASE64_CHARS.length; i++) {
|
|
9
|
+
const char = BASE64_CHARS.charAt(i);
|
|
10
|
+
const code = char.charCodeAt(0);
|
|
11
|
+
CHAR_TO_INDEX[code] = i;
|
|
12
|
+
INDEX_TO_CHAR[i] = char;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* 快速密钥扩展函数 - 简化版,提高性能
|
|
16
|
+
* @param {string} key - 用户密钥
|
|
17
|
+
* @param {number} length - 需要生成的序列长度
|
|
18
|
+
* @returns {Uint8Array} 密钥流
|
|
19
|
+
*/
|
|
20
|
+
function expandKeyFast(key, length) {
|
|
21
|
+
const keyLen = key.length;
|
|
22
|
+
const expanded = new Uint8Array(length);
|
|
23
|
+
|
|
24
|
+
// 使用简单的哈希算法生成密钥流
|
|
25
|
+
let hash = 0;
|
|
26
|
+
for (let i = 0; i < keyLen; i++) {
|
|
27
|
+
hash = ((hash << 5) - hash) + key.charCodeAt(i);
|
|
28
|
+
hash |= 0; // 转换为32位整数
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
for (let i = 0; i < length; i++) {
|
|
32
|
+
hash = (hash * 1664525 + 1013904223) | 0; // 线性同余生成器
|
|
33
|
+
expanded[i] = hash & 0xFF;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return expanded;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 原始算法加密(优化版)- 保留作为对比
|
|
40
|
+
* @param {string} text - Base64原文
|
|
41
|
+
* @param {string} key - 密钥
|
|
42
|
+
* @returns {string} Base64密文
|
|
43
|
+
*/
|
|
44
|
+
function encrypt(text, key) {
|
|
45
|
+
if (!text || !key) return '';
|
|
46
|
+
|
|
47
|
+
const len = text.length;
|
|
48
|
+
const keyStream = expandKeyFast(key, len);
|
|
49
|
+
let result = '';
|
|
50
|
+
|
|
51
|
+
for (let i = 0; i < len; i++) {
|
|
52
|
+
const char = text.charAt(i);
|
|
53
|
+
|
|
54
|
+
if (char === '=') {
|
|
55
|
+
result += '=';
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const charIndex = CHAR_TO_INDEX[char.charCodeAt(0)];
|
|
60
|
+
const keyByte = keyStream[i];
|
|
61
|
+
|
|
62
|
+
let encryptedIndex = charIndex;
|
|
63
|
+
|
|
64
|
+
// 第一轮:XOR变换
|
|
65
|
+
encryptedIndex ^= (keyByte & 0x3F);
|
|
66
|
+
|
|
67
|
+
// 第二轮:位移变换
|
|
68
|
+
encryptedIndex = ((encryptedIndex << 3) | (encryptedIndex >> 3)) & 0x3F;
|
|
69
|
+
|
|
70
|
+
// 第三轮:位置相关变换
|
|
71
|
+
encryptedIndex ^= (i & 0x3F);
|
|
72
|
+
|
|
73
|
+
result += INDEX_TO_CHAR[encryptedIndex];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 原始算法解密(优化版)
|
|
81
|
+
* @param {string} cipherText - Base64密文
|
|
82
|
+
* @param {string} key - 密钥
|
|
83
|
+
* @returns {string} Base64原文
|
|
84
|
+
*/
|
|
85
|
+
function decrypt(cipherText, key) {
|
|
86
|
+
if (!cipherText || !key) return '';
|
|
87
|
+
|
|
88
|
+
const len = cipherText.length;
|
|
89
|
+
const keyStream = expandKeyFast(key, len);
|
|
90
|
+
let result = '';
|
|
91
|
+
|
|
92
|
+
for (let i = 0; i < len; i++) {
|
|
93
|
+
const char = cipherText.charAt(i);
|
|
94
|
+
|
|
95
|
+
if (char === '=') {
|
|
96
|
+
result += '=';
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const charIndex = CHAR_TO_INDEX[char.charCodeAt(0)];
|
|
101
|
+
const keyByte = keyStream[i];
|
|
102
|
+
|
|
103
|
+
let decryptedIndex = charIndex;
|
|
104
|
+
|
|
105
|
+
// 逆向操作
|
|
106
|
+
decryptedIndex ^= (i & 0x3F);
|
|
107
|
+
decryptedIndex = ((decryptedIndex >> 3) | (decryptedIndex << 3)) & 0x3F;
|
|
108
|
+
decryptedIndex ^= (keyByte & 0x3F);
|
|
109
|
+
|
|
110
|
+
result += INDEX_TO_CHAR[decryptedIndex];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
module.exports = {
|
|
118
|
+
encrypt,
|
|
119
|
+
decrypt
|
|
120
|
+
}
|
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
const JSZip = require("./jszip.min.js");
|
|
3
3
|
const polyfill = require("./base64")
|
|
4
|
+
const { encrypt, decrypt } = require("./base64Crypt.js")
|
|
4
5
|
|
|
5
6
|
const base64ToArray = (base64) => {
|
|
6
7
|
const binaryStr = polyfill.atob(base64);
|
|
@@ -45,22 +46,22 @@ class ObjectZip {
|
|
|
45
46
|
// resolve(null)
|
|
46
47
|
// }
|
|
47
48
|
|
|
48
|
-
|
|
49
|
+
|
|
49
50
|
// }).catch(ex=>{
|
|
50
51
|
// console.error(ex);
|
|
51
52
|
// resolve(null)
|
|
52
53
|
// });
|
|
53
54
|
|
|
54
|
-
JSZip.loadAsync(this.target).then((zip) => {
|
|
55
|
-
zip.file("data.json").async("string").then(d=>{
|
|
56
|
-
let obj=JSON.parse(d);
|
|
55
|
+
JSZip.loadAsync(this.target).then((zip) => {
|
|
56
|
+
zip.file("data.json").async("string").then(d => {
|
|
57
|
+
let obj = JSON.parse(d);
|
|
57
58
|
// console.log("obj",obj)
|
|
58
59
|
resolve(obj)
|
|
59
|
-
}).catch(ex=>{
|
|
60
|
+
}).catch(ex => {
|
|
60
61
|
console.error(ex);
|
|
61
62
|
resolve(null)
|
|
62
|
-
});
|
|
63
|
-
}).catch(ex=>{
|
|
63
|
+
});
|
|
64
|
+
}).catch(ex => {
|
|
64
65
|
console.error(ex);
|
|
65
66
|
resolve(null)
|
|
66
67
|
});
|
|
@@ -82,7 +83,7 @@ class ObjectZip {
|
|
|
82
83
|
let zip = new JSZip();
|
|
83
84
|
zip.file("data.json", JSON.stringify(this.target));
|
|
84
85
|
zip.generateAsync({
|
|
85
|
-
type: JSZip.support["nodebuffer"]?"nodebuffer":"arraybuffer",
|
|
86
|
+
type: JSZip.support["nodebuffer"] ? "nodebuffer" : "arraybuffer",
|
|
86
87
|
compression: "DEFLATE",
|
|
87
88
|
compressionOptions: {
|
|
88
89
|
level: 9
|
|
@@ -103,14 +104,19 @@ class ObjectZip {
|
|
|
103
104
|
|
|
104
105
|
}
|
|
105
106
|
|
|
106
|
-
async getBase64() {
|
|
107
|
+
async getBase64(key) {
|
|
107
108
|
// if (typeof Buffer !== "undefined"){
|
|
108
109
|
// let buffer = await this.getBuffer()
|
|
109
110
|
// return buffer.toString("base64")
|
|
110
111
|
// }
|
|
111
|
-
|
|
112
|
+
|
|
112
113
|
let buffer = await this.getBuffer()
|
|
113
|
-
|
|
114
|
+
let base64 = arrayToBase64(buffer);
|
|
115
|
+
|
|
116
|
+
// 如果设定了密钥,则对base64进行加密
|
|
117
|
+
if (key) base64 = encrypt(base64, key)
|
|
118
|
+
|
|
119
|
+
return base64;
|
|
114
120
|
}
|
|
115
121
|
}
|
|
116
122
|
|
|
@@ -122,12 +128,16 @@ ObjectZip.fromBuffer = (buffer) => {
|
|
|
122
128
|
return new ObjectZip("buffer", buffer)
|
|
123
129
|
}
|
|
124
130
|
|
|
125
|
-
ObjectZip.fromBase64 = (base64) => {
|
|
131
|
+
ObjectZip.fromBase64 = (base64, key) => {
|
|
132
|
+
|
|
133
|
+
//如果是密文,则解密
|
|
134
|
+
if (key) base64 = decrypt(base64, key)
|
|
135
|
+
|
|
126
136
|
let buffer = null
|
|
127
137
|
// if (typeof Buffer !== "undefined")
|
|
128
138
|
// buffer = Buffer.from(base64, 'base64')
|
|
129
139
|
// else
|
|
130
|
-
|
|
140
|
+
buffer = base64ToArray(base64)
|
|
131
141
|
|
|
132
142
|
return new ObjectZip("buffer", buffer)
|
|
133
143
|
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@ruder/object-zip",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"description": "javascript中object,base64,zip互转工具。ObjectZip.fromObject({}).getBuffer(); ObjectZip.fromBuffer([]).getObject(); ObjectZip.fromBase64('').getBase64()",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
-
},
|
|
9
|
-
"dependencies": {
|
|
10
|
-
},
|
|
11
|
-
"author": "ruder",
|
|
12
|
-
"license": "ISC"
|
|
13
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@ruder/object-zip",
|
|
3
|
+
"version": "2.3.0",
|
|
4
|
+
"description": "javascript中object,base64,zip互转工具。ObjectZip.fromObject({}).getBuffer(); ObjectZip.fromBuffer([]).getObject(); ObjectZip.fromBase64('').getBase64()",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
},
|
|
11
|
+
"author": "ruder",
|
|
12
|
+
"license": "ISC"
|
|
13
|
+
}
|