Bläddra i källkod

加密解密走到一半了

skyffire 1 år sedan
förälder
incheckning
66aa134a27
4 ändrade filer med 143 tillägg och 32 borttagningar
  1. 83 23
      example/electron.js
  2. 35 0
      example/encrypt.js
  3. 23 8
      example/package-lock.json
  4. 2 1
      example/package.json

+ 83 - 23
example/electron.js

@@ -1,8 +1,65 @@
-const { app, BrowserWindow, Menu  } = require('electron');
+const { app, BrowserWindow, Menu, protocol } = require('electron');
 const path = require('path');
+const fs = require('fs');
+const crypto = require('crypto');
+const mime = require('mime');
+
+const algorithm = 'aes-256-ctr';
+const password = 'skyfffire-password';
+const key = crypto.createHash('sha256').update(password).digest();
+let memoryCache = {};
+
+
+// Function to decrypt a file and store its content in memory
+function decryptFile(filePath) {
+  console.log(`Decrypting: ${filePath}`);
+  const fileContent = fs.readFileSync(filePath);
+  const iv = fileContent.slice(0, 16); // Extract IV
+  const encrypted = fileContent.slice(16); // Extract encrypted data
+  const decipher = crypto.createDecipheriv(algorithm, key, iv);
+  const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()]);
+  const relativePath = path.relative(process.resourcesPath, filePath);
+  memoryCache[relativePath] = decrypted;
+  console.log(`Decrypted: ${filePath}, content: ${decrypted.slice(0, 100)}`);
+}
+
+// Recursively decrypt files in a directory and store their content in memory
+function decryptDirectory(directoryPath) {
+  const files = fs.readdirSync(directoryPath);
+  files.forEach(file => {
+    const fullPath = path.join(directoryPath, file);
+    console.log(`Processing: ${fullPath}`);
+    if (fs.lstatSync(fullPath).isDirectory()) {
+      decryptDirectory(fullPath);
+    } else {
+      decryptFile(fullPath);
+    }
+  });
+}
+
+
 
 function createWindow() {
-  const mainWindow = new BrowserWindow({
+  const directoryToDecrypt = path.join(process.resourcesPath, 'app.asar', 'static');
+  console.log(`Decrypting directory: ${directoryToDecrypt}`);
+  decryptDirectory(directoryToDecrypt);
+
+  // Register a custom protocol to serve content from memory
+  protocol.interceptBufferProtocol('file', (request, callback) => {
+    const url = request.url.substr(7); // Remove 'file://' prefix
+    const relativePath = path.relative(process.resourcesPath, url);
+    if (memoryCache[relativePath]) {
+      callback({ mimeType: 'text/javascript', data: memoryCache[relativePath] });
+    } else {
+      callback({ error: -6 }); // FILE_NOT_FOUND
+    }
+  }, (error) => {
+    if (error) {
+      console.error('Failed to register protocol');
+    }
+  });
+
+  const win = new BrowserWindow({
     width: 1600,
     height: 900,
     icon: path.join(__dirname, 'favicon.ico'), // 设置窗口图标
@@ -12,29 +69,32 @@ function createWindow() {
     },
   });
 
-  mainWindow.loadFile('index.html');
-
-  // 创建菜单模板,只包含一个刷新按钮
-  const menuTemplate = [
-    {
-      label: 'View',
-      submenu: [
-        {
-          label: 'Reload',
-          accelerator: 'CmdOrCtrl+R',
-          click: () => {
-            mainWindow.reload();
-          },
-        },
-      ],
-    },
-  ];
+  win.loadFile('index.html');
 
-  // 创建菜单
-  const menu = Menu.buildFromTemplate(menuTemplate);
+  // 打开调试工具
+  win.webContents.openDevTools();
 
-  // 设置应用程序的菜单
-  Menu.setApplicationMenu(menu);
+  // // 创建菜单模板,只包含一个刷新按钮
+  // const menuTemplate = [
+  //   {
+  //     label: 'View',
+  //     submenu: [
+  //       {
+  //         label: 'Reload',
+  //         accelerator: 'CmdOrCtrl+R',
+  //         click: () => {
+  //           mainWindow.reload();
+  //         },
+  //       },
+  //     ],
+  //   },
+  // ];
+  //
+  // // 创建菜单
+  // const menu = Menu.buildFromTemplate(menuTemplate);
+  //
+  // // 设置应用程序的菜单
+  // Menu.setApplicationMenu(menu);
 }
 
 app.on('ready', createWindow);

+ 35 - 0
example/encrypt.js

@@ -0,0 +1,35 @@
+const crypto = require('crypto');
+const fs = require('fs');
+const path = require('path');
+
+const algorithm = 'aes-256-ctr';
+const password = 'skyfffire-password';
+const key = crypto.createHash('sha256').update(password).digest();
+const iv = crypto.randomBytes(16);
+
+function encryptFile(filePath) {
+  console.log(`Encrypting: ${filePath}`);
+  const fileContent = fs.readFileSync(filePath);
+  const cipher = crypto.createCipheriv(algorithm, key, iv);
+  const encrypted = Buffer.concat([cipher.update(fileContent), cipher.final()]);
+  fs.writeFileSync(filePath, Buffer.concat([iv, encrypted])); // Store IV with encrypted data
+  console.log(`Encrypted: ${filePath}`);
+}
+
+function encryptDirectory(directoryPath) {
+  const files = fs.readdirSync(directoryPath);
+  files.forEach(file => {
+    const fullPath = path.join(directoryPath, file);
+    console.log(`Processing: ${fullPath}`);
+    if (fs.lstatSync(fullPath).isDirectory()) {
+      encryptDirectory(fullPath);
+    } else {
+      encryptFile(fullPath);
+    }
+  });
+}
+
+const directoryToEncrypt = path.join(__dirname, 'build/static');
+console.log(`Encrypting directory: ${directoryToEncrypt}`);
+encryptDirectory(directoryToEncrypt);
+console.log('Encryption completed.');

+ 23 - 8
example/package-lock.json

@@ -1,15 +1,16 @@
 {
   "name": "heatmap",
-  "version": "1.0.4",
+  "version": "1.0.9",
   "lockfileVersion": 3,
   "requires": true,
   "packages": {
     "": {
       "name": "heatmap",
-      "version": "1.0.4",
+      "version": "1.0.9",
       "dependencies": {
         "@rongmz/react-stock-heatmap": "file:..",
         "axios": "^1.7.2",
+        "mime": "^4.0.3",
         "react": "file:../node_modules/react",
         "react-dom": "file:../node_modules/react-dom",
         "react-hot-toast": "^2.4.1",
@@ -2610,6 +2611,18 @@
         "graceful-fs": "^4.1.6"
       }
     },
+    "node_modules/electron-publish/node_modules/mime": {
+      "version": "2.6.0",
+      "resolved": "https://registry.npmmirror.com/mime/-/mime-2.6.0.tgz",
+      "integrity": "sha512-USPkMeET31rOMiarsBNIHZKLGgvKc/LrjofAnBlOttf5ajRvqiRA8QsenbcooctK6d6Ts6aqZXBA+XbkKthiQg==",
+      "dev": true,
+      "bin": {
+        "mime": "cli.js"
+      },
+      "engines": {
+        "node": ">=4.0.0"
+      }
+    },
     "node_modules/electron-publish/node_modules/supports-color": {
       "version": "7.2.0",
       "resolved": "https://registry.npmmirror.com/supports-color/-/supports-color-7.2.0.tgz",
@@ -3697,15 +3710,17 @@
       }
     },
     "node_modules/mime": {
-      "version": "2.6.0",
-      "resolved": "https://registry.npmmirror.com/mime/-/mime-2.6.0.tgz",
-      "integrity": "sha512-USPkMeET31rOMiarsBNIHZKLGgvKc/LrjofAnBlOttf5ajRvqiRA8QsenbcooctK6d6Ts6aqZXBA+XbkKthiQg==",
-      "dev": true,
+      "version": "4.0.3",
+      "resolved": "https://registry.npmmirror.com/mime/-/mime-4.0.3.tgz",
+      "integrity": "sha512-KgUb15Oorc0NEKPbvfa0wRU+PItIEZmiv+pyAO2i0oTIVTJhlzMclU7w4RXWQrSOVH5ax/p/CkIO7KI4OyFJTQ==",
+      "funding": [
+        "https://github.com/sponsors/broofa"
+      ],
       "bin": {
-        "mime": "cli.js"
+        "mime": "bin/cli.js"
       },
       "engines": {
-        "node": ">=4.0.0"
+        "node": ">=16"
       }
     },
     "node_modules/mime-db": {

+ 2 - 1
example/package.json

@@ -1,7 +1,7 @@
 {
   "name": "heatmap",
   "homepage": ".",
-  "version": "1.0.8",
+  "version": "1.0.9",
   "private": true,
   "main": "main.js",
   "scripts": {
@@ -15,6 +15,7 @@
   "dependencies": {
     "@rongmz/react-stock-heatmap": "file:..",
     "axios": "^1.7.2",
+    "mime": "^4.0.3",
     "react": "file:../node_modules/react",
     "react-dom": "file:../node_modules/react-dom",
     "react-hot-toast": "^2.4.1",