-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_encrypted_csv.js
More file actions
128 lines (100 loc) · 4.32 KB
/
Copy pathgenerate_encrypted_csv.js
File metadata and controls
128 lines (100 loc) · 4.32 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const fs = require('fs');
const crypto = require('crypto');
const path = require('path');
// Load secret key from environment
require('dotenv').config();
const SECRET_KEY = process.env.SECRET_KEY || 'ORPHEUSFURRY';
// Directories and files
const REMEMBER_DIR = 'stages/6_REMEMBER';
const CSV_FILE = 'encrypted_memories.csv';
// Function to extract memory code from txt file content
function extractMemoryCode(content) {
const lines = content.split('\n');
for (const line of lines) {
const trimmedLine = line.trim();
// Look for pattern like "(fragment 3 of 5, memory code: DESIGN. the 4 other DESIGN fragments are required to reconstruct)"
const fragmentMatch = trimmedLine.match(/\(fragment\s+(\d+)\s+of\s+\d+,\s+memory\s+code:\s+(\w+)\./);
if (fragmentMatch) {
const fragmentNumber = fragmentMatch[1];
const fragmentType = fragmentMatch[2];
return fragmentType + fragmentNumber;
}
}
return '';
}
// Function to encrypt data using AES-256-CBC
function encryptData(text, password) {
const iv = crypto.randomBytes(16);
const key = crypto.scryptSync(password, 'salt', 32);
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
return iv.toString('hex') + ':' + encrypted;
}
// Main function to process all memory files and create CSV
function createEncryptedMemoriesCSV() {
console.log('🔄 Processing memory files to create encrypted_memories.csv...');
// Get all txt files from REMEMBER directory
const txtFiles = fs.readdirSync(REMEMBER_DIR)
.filter(file => file.endsWith('.txt'))
.sort();
console.log(`📁 Found ${txtFiles.length} memory files`);
const csvData = [];
// Add CSV header
csvData.push('number,encryption,memory_code');
txtFiles.forEach((txtFile, index) => {
const filePath = path.join(REMEMBER_DIR, txtFile);
const content = fs.readFileSync(filePath, 'utf8');
const fileNumber = path.basename(txtFile, '.txt'); // e.g., "0111"
// Extract memory code from file content
const memoryCode = extractMemoryCode(content);
// Encrypt the entire file content
const encryptedContent = encryptData(content, SECRET_KEY);
// Add to CSV data: number, encrypted content, memory code
csvData.push(`${fileNumber},"${encryptedContent}",${memoryCode}`);
console.log(`✅ Processed ${txtFile} -> ${memoryCode}`);
});
// Write the CSV file
fs.writeFileSync(CSV_FILE, csvData.join('\n') + '\n');
console.log(`\n📝 Written ${csvData.length - 1} entries to ${CSV_FILE}`);
// Verification
const csvContent = fs.readFileSync(CSV_FILE, 'utf8');
const csvLines = csvContent.trim().split('\n').length;
console.log(`✓ ${CSV_FILE} contains ${csvLines} lines (including header)`);
// Count by memory types
const memoryCounts = {};
csvData.slice(1).forEach(line => {
const parts = line.split(',');
const memoryCode = parts[2];
const type = memoryCode.replace(/\d+$/, ''); // Remove number to get type
memoryCounts[type] = (memoryCounts[type] || 0) + 1;
});
console.log('\n📊 Memory Type Breakdown:');
Object.entries(memoryCounts).forEach(([type, count]) => {
console.log(` ${type}: ${count} fragments`);
});
return {
totalFiles: txtFiles.length,
csvEntries: csvData.length - 1,
memoryTypes: Object.keys(memoryCounts).length
};
}
// Run the script
if (require.main === module) {
console.log('🚀 Starting encrypted memories CSV generation...\n');
try {
const results = createEncryptedMemoriesCSV();
console.log('\n🎉 CSV generation completed successfully!');
console.log(`📄 Processed ${results.totalFiles} memory files`);
console.log(`💾 Generated ${results.csvEntries} encrypted entries`);
console.log(`🔗 Found ${results.memoryTypes} different memory types`);
} catch (error) {
console.error('❌ Error during processing:', error);
process.exit(1);
}
}
module.exports = {
createEncryptedMemoriesCSV,
extractMemoryCode,
encryptData
};