Skip to content

Commit 19c5c99

Browse files
committed
Use built-in cipher instead of external package
1 parent e20585c commit 19c5c99

File tree

4 files changed

+11
-26
lines changed

4 files changed

+11
-26
lines changed

lib/encrypted-attr.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const crypto = require('crypto')
4-
const gcm = require('node-aes-gcm')
54
const { get, set } = require('lodash')
65

76
function EncryptedAttributes (attributes, options) {
@@ -24,11 +23,13 @@ function EncryptedAttributes (attributes, options) {
2423
let aad = Buffer.from(
2524
`aes-256-gcm$${options.verifyId ? obj.id.toString() : ''}$${options.keyId}`)
2625
let key = Buffer.from(options.keys[options.keyId], 'base64')
27-
let result = gcm.encrypt(key, iv, Buffer.from(val), aad)
26+
let gcm = crypto.createCipheriv('aes-256-gcm', key, iv).setAAD(aad)
27+
let result = gcm.update(val, 'utf8', 'base64') + gcm.final('base64')
28+
2829
return aad.toString('base64') + '$' +
2930
iv.toString('base64') + '$' +
30-
result.ciphertext.toString('base64') + '$' +
31-
result.auth_tag.toString('base64').slice(0, 22)
31+
result + '$' +
32+
gcm.getAuthTag().toString('base64').slice(0, 22)
3233
}
3334

3435
function encryptAll (obj) {
@@ -59,11 +60,9 @@ function EncryptedAttributes (attributes, options) {
5960
throw new Error('Encrypted attribute has invalid key id')
6061
}
6162
let key = Buffer.from(options.keys[keyId], 'base64')
62-
let result = gcm.decrypt(key, iv, payload, aad, tag)
63-
if (!result.auth_ok) {
64-
throw new Error('Encrypted attribute has invalid auth tag')
65-
}
66-
return result.plaintext.toString()
63+
let gcm = crypto.createDecipheriv('aes-256-gcm', key, iv).setAAD(aad).setAuthTag(tag)
64+
65+
return gcm.update(payload, 'binary', 'utf8') + gcm.final('utf8')
6766
}
6867

6968
function decryptAll (obj) {

package-lock.json

Lines changed: 0 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
"orm"
1616
],
1717
"dependencies": {
18-
"lodash": "^4.17.4",
19-
"node-aes-gcm": "^0.2.2"
18+
"lodash": "^4.17.4"
2019
},
2120
"optionalDependencies": {},
2221
"devDependencies": {

test/encrypted-attr.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,15 @@ describe('encrypted attributes', function () {
158158
let obj = {id: 1}
159159
// aad: aes-256-gcm$01$k2
160160
let wrongKey = 'YWVzLTI1Ni1nY20kMSRrMg==$sK91YfUvv+O8Jx/m$OOQniq8=$WLbWYz7uCQBTNO3Fc+5UvA'
161-
expect(() => enc.decryptAttribute(obj, wrongKey), 'to throw', /invalid auth tag/i)
161+
expect(() => enc.decryptAttribute(obj, wrongKey), 'to throw', /unable to auth/i)
162162
})
163163

164164
it('should throw when decrypting with wrong auth tag', function () {
165165
let enc = EncryptedAttributes(['secret'], this.options)
166166
let obj = {id: 1}
167167
// aad: aes-256-gcm$01$k1
168168
let wrongAuthTag = 'YWVzLTI1Ni1nY20kMSRrMQ==$sK91YfUvv+O8Jx/m$OOQniq8=$VLbWYz7uCQBTNO3Fc+5UvA'
169-
expect(() => enc.decryptAttribute(obj, wrongAuthTag), 'to throw', /invalid auth tag/i)
169+
expect(() => enc.decryptAttribute(obj, wrongAuthTag), 'to throw', /unable to auth/i)
170170
})
171171

172172
it('should throw when decrypting without id', function () {

0 commit comments

Comments
 (0)