encrypt.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. const crypto = require('crypto');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const algorithm = 'aes-256-ctr';
  5. const password = 'skyfffire-password';
  6. const key = crypto.createHash('sha256').update(password).digest();
  7. const iv = crypto.randomBytes(16);
  8. function encryptFile(filePath) {
  9. console.log(`Encrypting: ${filePath}`);
  10. const fileContent = fs.readFileSync(filePath);
  11. const cipher = crypto.createCipheriv(algorithm, key, iv);
  12. const encrypted = Buffer.concat([cipher.update(fileContent), cipher.final()]);
  13. fs.writeFileSync(filePath, Buffer.concat([iv, encrypted])); // Store IV with encrypted data
  14. console.log(`Encrypted: ${filePath}`);
  15. }
  16. function encryptDirectory(directoryPath) {
  17. const files = fs.readdirSync(directoryPath);
  18. files.forEach(file => {
  19. const fullPath = path.join(directoryPath, file);
  20. console.log(`Processing: ${fullPath}`);
  21. if (fs.lstatSync(fullPath).isDirectory()) {
  22. encryptDirectory(fullPath);
  23. } else {
  24. encryptFile(fullPath);
  25. }
  26. });
  27. }
  28. const directoryToEncrypt = path.join(__dirname, 'build/static');
  29. console.log(`Encrypting directory: ${directoryToEncrypt}`);
  30. encryptDirectory(directoryToEncrypt);
  31. console.log('Encryption completed.');