const fs = require('fs'); const path = require('path'); const crypto = require('crypto'); // รายชื่อไฟล์ที่สำคัญที่เราไม่ต้องการให้ใครแก้ไข const criticalFiles = [ 'main.js', 'preload.js', 'validation_gateway.js', 'generate-manifests.js', 'modules/error-handler.js', 'modules/security-logger.js', 'modules/system-detector.js', 'modules/button-generator.js', 'modules/plugin-manager.js', 'modules/executor.js', 'modules/context-manager.js' ]; const checksums = {}; console.log(' Generating checksums for critical files...'); criticalFiles.forEach(filePath => { try { const fullPath = path.join(__dirname, filePath); if (fs.existsSync(fullPath)) { const fileContent = fs.readFileSync(fullPath); const hash = crypto.createHash('sha256').update(fileContent).digest('hex'); checksums[filePath] = hash; console.log(` ${filePath} -> ${hash.substring(0, 20)}...`); } else { console.error(` ERROR: File not found at ${fullPath}`); } } catch (error) { console.error(` ERROR: Could not process file ${filePath}: ${error.message}`); } }); // เขียนผลลัพธ์ลงในไฟล์ checksums.json const checksumsJson = JSON.stringify(checksums, null, 2); fs.writeFileSync(path.join(__dirname, 'checksums.json'), checksumsJson); console.log('\n Checksums generated and saved to checksums.json'); // ส่วนที่เพิ่มเข้ามา: สร้าง Signature try { const privateKeyPath = path.join(__dirname, 'private_key.pem'); if (!fs.existsSync(privateKeyPath)) { console.error('\n FATAL ERROR: private_key.pem not found!'); console.error(' Please run "node generate-keys.js" first to generate key pair.'); console.error(' Or ensure private_key.pem is in the project root directory.'); process.exit(1); } const privateKey = fs.readFileSync(privateKeyPath, 'utf8'); const signer = crypto.createSign('sha256'); signer.update(checksumsJson); signer.end(); const signature = signer.sign(privateKey, 'hex'); fs.writeFileSync(path.join(__dirname, 'checksums.sig'), signature); console.log(' Checksums successfully signed -> checksums.sig'); console.log(' Signature:', signature.substring(0, 40) + '...'); } catch (error) { console.error('\n FATAL ERROR: Could not sign checksums:', error.message); console.error(' Please ensure private_key.pem is valid and accessible.'); console.error(' You may need to regenerate keys by running "node generate-keys.js"'); process.exit(1); }