crypto.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const { createCipheriv, createDecipheriv, randomBytes } = require('crypto');
  2. const { createLogger } = require('./logger');
  3. const ALGORITHM = 'aes-256-gcm';
  4. const ENC_PREFIX = 'ENC:v1:';
  5. const log = createLogger('crypto');
  6. function _getKey() {
  7. const keyHex = process.env.ENCRYPTION_KEY;
  8. if (!keyHex || keyHex.length !== 64) return null;
  9. return Buffer.from(keyHex, 'hex');
  10. }
  11. // Returns the ciphertext string, or plaintext if key not configured.
  12. function encryptToken(plaintext) {
  13. if (!plaintext) return plaintext;
  14. if (String(plaintext).startsWith(ENC_PREFIX)) return plaintext; // already encrypted
  15. const key = _getKey();
  16. if (!key) return plaintext;
  17. const iv = randomBytes(12);
  18. const cipher = createCipheriv(ALGORITHM, key, iv);
  19. const encrypted = Buffer.concat([cipher.update(String(plaintext), 'utf8'), cipher.final()]);
  20. const tag = cipher.getAuthTag();
  21. return `${ENC_PREFIX}${iv.toString('hex')}:${tag.toString('hex')}:${encrypted.toString('hex')}`;
  22. }
  23. // Returns decrypted plaintext, or the original value if not encrypted.
  24. // Returns null and logs an error if decryption fails.
  25. function decryptToken(value) {
  26. if (!value) return value;
  27. if (!String(value).startsWith(ENC_PREFIX)) return value; // plaintext passthrough (legacy data)
  28. const key = _getKey();
  29. if (!key) {
  30. log.error({ action: 'decrypt', outcome: 'failure', err: 'ENCRYPTION_KEY not set — cannot decrypt stored token' });
  31. return null;
  32. }
  33. try {
  34. const parts = String(value).slice(ENC_PREFIX.length).split(':');
  35. if (parts.length !== 3) throw new Error('malformed ciphertext');
  36. const [ivHex, tagHex, ciphertextHex] = parts;
  37. const iv = Buffer.from(ivHex, 'hex');
  38. const tag = Buffer.from(tagHex, 'hex');
  39. const ciphertext = Buffer.from(ciphertextHex, 'hex');
  40. const decipher = createDecipheriv(ALGORITHM, key, iv);
  41. decipher.setAuthTag(tag);
  42. return Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString('utf8');
  43. } catch (err) {
  44. log.error({ action: 'decrypt', outcome: 'failure', err: err.message });
  45. return null;
  46. }
  47. }
  48. // Log a startup warning when no key is configured so operators notice.
  49. function warnIfNoKey(serviceName) {
  50. if (!_getKey()) {
  51. log.warn({ action: 'startup_check', service: serviceName, outcome: 'warning', err: 'ENCRYPTION_KEY is not set — tokens will be stored in plaintext. Generate a key with: node -e "console.log(require(\'crypto\').randomBytes(32).toString(\'hex\'))"' });
  52. }
  53. }
  54. module.exports = { encryptToken, decryptToken, warnIfNoKey };