fast-cocos 1.1.5 → 1.1.7

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.
Files changed (2) hide show
  1. package/dist/xor/src/xor.js +24 -14
  2. package/package.json +1 -1
@@ -31,6 +31,13 @@ function xorTransform(opts) {
31
31
  }
32
32
  return result;
33
33
  }
34
+ function xor(data, key) {
35
+ let result = "";
36
+ for (let i = 0; i < data.length; i++) {
37
+ result += String.fromCharCode(data.charCodeAt(i) ^ key.charCodeAt(i % key.length));
38
+ }
39
+ return result;
40
+ }
34
41
  /**
35
42
  * 加密文件(签名头 + XOR),输出文件名与源文件 basename 相同(通常配合输出目录使用)
36
43
  */
@@ -52,11 +59,14 @@ function encryptFile(opts) {
52
59
  throw new Error("输出路径解析失败");
53
60
  }
54
61
  const destPath = out;
55
- const rawData = fs_1.default.readFileSync(opts.inputPath);
56
- const encrypted = xorTransform({ data: rawData, key: finalKey });
57
- const signature = Buffer.from(finalSign, "utf-8");
58
- const output = Buffer.concat([signature, encrypted]);
59
- fs_1.default.writeFileSync(destPath, output);
62
+ const rawData = fs_1.default.readFileSync(opts.inputPath, { encoding: "binary" });
63
+ const fileSign = rawData.slice(0, finalSign.length);
64
+ if (fileSign == finalSign) {
65
+ console.log(`跳过(已加密): ${opts.inputPath}`);
66
+ return;
67
+ }
68
+ const encrypted = finalSign + xor(rawData, finalKey);
69
+ fs_1.default.writeFileSync(destPath, encrypted, { encoding: "binary" });
60
70
  console.info("\x1b[32m%s\x1b[0m", `加密: ${opts.inputPath} -> ${destPath} [sign:${finalSign}, key:${finalKey.substring(0, 4)}...]`);
61
71
  }
62
72
  /**
@@ -81,17 +91,17 @@ function decryptFile(opts) {
81
91
  throw new Error("输出路径解析失败");
82
92
  }
83
93
  const destPath = out;
84
- const encryptedData = fs_1.default.readFileSync(opts.inputPath);
85
- const signBuffer = Buffer.from(finalSign, "utf-8");
86
- const signSize = signBuffer.length;
87
- if (encryptedData.length < signSize || !encryptedData.subarray(0, signSize).equals(signBuffer)) {
88
- console.log(`跳过(签名不匹配): ${path_1.default.basename(opts.inputPath)}`);
94
+ const encryptedData = fs_1.default.readFileSync(opts.inputPath, { encoding: "binary" });
95
+ const fileSign = encryptedData.slice(0, finalSign.length);
96
+ console.log("文件签名:", fileSign);
97
+ if (fileSign != finalSign) {
98
+ console.info("\x1b[31m%s\x1b[0m", `跳过(签名不匹配): ${opts.inputPath}`);
89
99
  return false;
90
100
  }
91
- const dataWithoutSign = encryptedData.subarray(signSize);
92
- const decrypted = xorTransform({ data: dataWithoutSign, key: finalKey });
93
- fs_1.default.writeFileSync(destPath, decrypted);
94
- console.log(`解密: ${opts.inputPath} -> ${destPath} [sign:${finalSign}, key:${finalKey.substring(0, 4)}...]`);
101
+ const encrypted = encryptedData.slice(finalSign.length);
102
+ const decrypted = xor(encrypted, finalKey);
103
+ fs_1.default.writeFileSync(destPath, decrypted, { encoding: "binary" });
104
+ console.info("\x1b[32m%s\x1b[0m", `解密: ${opts.inputPath} -> ${destPath} [sign:${finalSign}, key:${finalKey.substring(0, 4)}...]`);
95
105
  return true;
96
106
  }
97
107
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-cocos",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
4
4
  "scripts": {
5
5
  "clean": "rm -rf dist",
6
6
  "start": "node ./dist/index.js",