File size: 2,793 Bytes
857cdcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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);
}