1
1
'use strict'
2
2
3
+ const alg = 'aes-256-gcm'
3
4
const crypto = require ( 'crypto' )
4
- const gcm = require ( 'node-aes-gcm' )
5
5
const { get, set } = require ( 'lodash' )
6
6
7
7
function EncryptedAttributes ( attributes , options ) {
8
8
options = options || { }
9
9
10
+ let prefix = Buffer . from ( `${ alg } $` ) . toString ( 'base64' )
11
+
10
12
function encryptAttribute ( obj , val ) {
11
13
// Encrypted attributes are prefixed with "aes-256-gcm$", the base64
12
- // encoding of which is "YWVzLTI1Ni1nY20k" . Nulls are not encrypted.
13
- if ( val == null || ( typeof val === 'string' && val . startsWith ( 'YWVzLTI1Ni1nY20k' ) ) ) {
14
+ // encoding of which is in `prefix` . Nulls are not encrypted.
15
+ if ( val == null || ( typeof val === 'string' && val . startsWith ( prefix ) ) ) {
14
16
return val
15
17
}
16
18
if ( typeof val !== 'string' ) {
@@ -24,11 +26,13 @@ function EncryptedAttributes (attributes, options) {
24
26
let aad = Buffer . from (
25
27
`aes-256-gcm$${ options . verifyId ? obj . id . toString ( ) : '' } $${ options . keyId } ` )
26
28
let key = Buffer . from ( options . keys [ options . keyId ] , 'base64' )
27
- let result = gcm . encrypt ( key , iv , Buffer . from ( val ) , aad )
29
+ let gcm = crypto . createCipheriv ( 'aes-256-gcm' , key , iv ) . setAAD ( aad )
30
+ let result = gcm . update ( val , 'utf8' , 'base64' ) + gcm . final ( 'base64' )
31
+
28
32
return aad . toString ( 'base64' ) + '$' +
29
33
iv . toString ( 'base64' ) + '$' +
30
- result . ciphertext . toString ( 'base64' ) + '$' +
31
- result . auth_tag . toString ( 'base64' ) . slice ( 0 , 22 )
34
+ result + '$' +
35
+ gcm . getAuthTag ( ) . toString ( 'base64' ) . slice ( 0 , 22 )
32
36
}
33
37
34
38
function encryptAll ( obj ) {
@@ -43,8 +47,8 @@ function EncryptedAttributes (attributes, options) {
43
47
44
48
function decryptAttribute ( obj , val ) {
45
49
// Encrypted attributes are prefixed with "aes-256-gcm$", the base64
46
- // encoding of which is "YWVzLTI1Ni1nY20k" . Nulls are not encrypted.
47
- if ( typeof val !== 'string' || ! val . startsWith ( 'YWVzLTI1Ni1nY20k' ) ) {
50
+ // encoding of which is in `prefix` . Nulls are not encrypted.
51
+ if ( typeof val !== 'string' || ! val . startsWith ( prefix ) ) {
48
52
return val
49
53
}
50
54
if ( options . verifyId && ! obj . id ) {
@@ -59,11 +63,9 @@ function EncryptedAttributes (attributes, options) {
59
63
throw new Error ( 'Encrypted attribute has invalid key id' )
60
64
}
61
65
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 ( )
66
+ let gcm = crypto . createDecipheriv ( 'aes-256-gcm' , key , iv ) . setAAD ( aad ) . setAuthTag ( tag )
67
+
68
+ return gcm . update ( payload , 'binary' , 'utf8' ) + gcm . final ( 'utf8' )
67
69
}
68
70
69
71
function decryptAll ( obj ) {
0 commit comments