fork download
  1. process.stdin.resume();
  2. process.stdin.setEncoding('utf8');
  3.  
  4. 'use strict';
  5.  
  6. const crypto = require('crypto');
  7.  
  8. const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY; // Must be 256 bits (32 characters)
  9. const IV_LENGTH = 16; // For AES, this is always 16
  10.  
  11. function encrypt(text) {
  12. let iv = crypto.randomBytes(IV_LENGTH);
  13. let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(ENCRYPTION_KEY), iv);
  14. let encrypted = cipher.update(text);
  15.  
  16. encrypted = Buffer.concat([encrypted, cipher.final()]);
  17.  
  18. return iv.toString('hex') + ':' + encrypted.toString('hex');
  19. }
  20.  
  21. function decrypt(text) {
  22. let textParts = text.split(':');
  23. let iv = Buffer.from(textParts.shift(), 'hex');
  24. let encryptedText = Buffer.from(textParts.join(':'), 'hex');
  25. let decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(ENCRYPTION_KEY), iv);
  26. let decrypted = decipher.update(encryptedText);
  27.  
  28. decrypted = Buffer.concat([decrypted, decipher.final()]);
  29.  
  30. return decrypted.toString();
  31. }
  32.  
  33. module.exports = { decrypt, encrypt };
  34.  
Success #stdin #stdout 0.1s 36924KB
stdin
Hello! World
stdout
Standard output is empty