File size: 5,483 Bytes
62cfde8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
129
130
131
132
133
134
135
136
137
138
diff --git a/src/app/lib/enc.js b/src/app/lib/enc.js
index ce608ba9..b889766b 100644
--- a/src/app/lib/enc.js
+++ b/src/app/lib/enc.js
@@ -1,8 +1,23 @@
 /**
  * data encrypt/decrypt
+ *
+ * New format (GCM):  'gcm:<iv_hex>:<salt_hex>:<authtag_hex>:<ciphertext_hex>'
+ * Legacy format:     '<ciphertext_hex>'  (pure hex, no colons — aes-192-cbc)
+ *
+ * decrypt/decryptAsync detect the format automatically via the 'gcm:' prefix,
+ * so old data encrypted with the static IV/salt continues to work without migration.
  */
 
-const algorithmDefault = 'aes-192-cbc'
+const algorithmDefault = 'aes-256-gcm'
+
+// Legacy constants — kept only for decrypting old data (aes-192-cbc)
+const LEGACY_ALGORITHM = 'aes-192-cbc'
+const LEGACY_IV = Buffer.alloc(16, 0)
+const LEGACY_SALT = 'salt'
+const LEGACY_KEY_LENGTH = 24
+const IV_LENGTH = 12 // 12 bytes is recommended for GCM
+const SALT_LENGTH = 16
+const KEY_LENGTH = 32 // aes-256 requires a 32-byte key
 
 function scryptAsync (...args) {
   const crypto = require('crypto')
@@ -19,29 +34,42 @@ function scryptAsync (...args) {
 exports.encrypt = function (
   str = '',
   password,
-  algorithm = algorithmDefault,
-  iv = Buffer.alloc(16, 0)
+  algorithm = algorithmDefault
 ) {
   const crypto = require('crypto')
-  const key = crypto.scryptSync(password, 'salt', 24)
-  // Use `crypto.randomBytes` to generate a random iv instead of the static iv
+  const iv = crypto.randomBytes(IV_LENGTH)
+  const salt = crypto.randomBytes(SALT_LENGTH)
+  const key = crypto.scryptSync(password, salt, KEY_LENGTH)
   const cipher = crypto.createCipheriv(algorithm, key, iv)
   let encrypted = cipher.update(str, 'utf8', 'hex')
   encrypted += cipher.final('hex')
-  return encrypted
+  const authTag = cipher.getAuthTag()
+  return 'gcm:' + iv.toString('hex') + ':' + salt.toString('hex') + ':' + authTag.toString('hex') + ':' + encrypted
 }
 
 exports.decrypt = function (
   encrypted = '',
   password,
-  algorithm = algorithmDefault,
-  iv = Buffer.alloc(16, 0)
+  algorithm = algorithmDefault
 ) {
   const crypto = require('crypto')
-  // Use the async `crypto.scrypt()` instead.
-  const key = crypto.scryptSync(password, 'salt', 24)
-  const decipher = crypto.createDecipheriv(algorithm, key, iv)
-  // Encrypted using same algorithm, key and iv.
+  if (encrypted.startsWith('gcm:')) {
+    // New format: gcm:iv_hex:salt_hex:authtag_hex:ciphertext_hex
+    const parts = encrypted.split(':')
+    const iv = Buffer.from(parts[1], 'hex')
+    const salt = Buffer.from(parts[2], 'hex')
+    const authTag = Buffer.from(parts[3], 'hex')
+    const ciphertext = parts[4]
+    const key = crypto.scryptSync(password, salt, KEY_LENGTH)
+    const decipher = crypto.createDecipheriv(algorithm, key, iv)
+    decipher.setAuthTag(authTag)
+    let decrypted = decipher.update(ciphertext, 'hex', 'utf8')
+    decrypted += decipher.final('utf8')
+    return decrypted
+  }
+  // Legacy format: aes-192-cbc with static IV and salt
+  const key = crypto.scryptSync(password, LEGACY_SALT, LEGACY_KEY_LENGTH)
+  const decipher = crypto.createDecipheriv(LEGACY_ALGORITHM, key, LEGACY_IV)
   let decrypted = decipher.update(encrypted, 'hex', 'utf8')
   decrypted += decipher.final('utf8')
   return decrypted
@@ -50,29 +78,42 @@ exports.decrypt = function (
 exports.encryptAsync = async function (
   str = '',
   password,
-  algorithm = algorithmDefault,
-  iv = Buffer.alloc(16, 0)
+  algorithm = algorithmDefault
 ) {
   const crypto = require('crypto')
-  const key = await scryptAsync(password, 'salt', 24)
-  // Use `crypto.randomBytes` to generate a random iv instead of the static iv
+  const iv = crypto.randomBytes(IV_LENGTH)
+  const salt = crypto.randomBytes(SALT_LENGTH)
+  const key = await scryptAsync(password, salt, KEY_LENGTH)
   const cipher = crypto.createCipheriv(algorithm, key, iv)
   let encrypted = cipher.update(str, 'utf8', 'hex')
   encrypted += cipher.final('hex')
-  return encrypted
+  const authTag = cipher.getAuthTag()
+  return 'gcm:' + iv.toString('hex') + ':' + salt.toString('hex') + ':' + authTag.toString('hex') + ':' + encrypted
 }
 
 exports.decryptAsync = async function (
   encrypted = '',
   password,
-  algorithm = algorithmDefault,
-  iv = Buffer.alloc(16, 0)
+  algorithm = algorithmDefault
 ) {
   const crypto = require('crypto')
-  // Use the async `crypto.scrypt()` instead.
-  const key = await scryptAsync(password, 'salt', 24)
-  const decipher = crypto.createDecipheriv(algorithm, key, iv)
-  // Encrypted using same algorithm, key and iv.
+  if (encrypted.startsWith('gcm:')) {
+    // New format: gcm:iv_hex:salt_hex:authtag_hex:ciphertext_hex
+    const parts = encrypted.split(':')
+    const iv = Buffer.from(parts[1], 'hex')
+    const salt = Buffer.from(parts[2], 'hex')
+    const authTag = Buffer.from(parts[3], 'hex')
+    const ciphertext = parts[4]
+    const key = await scryptAsync(password, salt, KEY_LENGTH)
+    const decipher = crypto.createDecipheriv(algorithm, key, iv)
+    decipher.setAuthTag(authTag)
+    let decrypted = decipher.update(ciphertext, 'hex', 'utf8')
+    decrypted += decipher.final('utf8')
+    return decrypted
+  }
+  // Legacy format: aes-192-cbc with static IV and salt
+  const key = await scryptAsync(password, LEGACY_SALT, LEGACY_KEY_LENGTH)
+  const decipher = crypto.createDecipheriv(LEGACY_ALGORITHM, key, LEGACY_IV)
   let decrypted = decipher.update(encrypted, 'hex', 'utf8')
   decrypted += decipher.final('utf8')
   return decrypted