Skip to content

Commit 943c82f

Browse files
committed
Use a constant for the algorithm
1 parent 19c5c99 commit 943c82f

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

lib/encrypted-attr.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
'use strict'
22

3+
const alg = 'aes-256-gcm'
34
const crypto = require('crypto')
45
const { get, set } = require('lodash')
56

67
function EncryptedAttributes (attributes, options) {
78
options = options || {}
89

10+
let prefix = Buffer.from(`${alg}$`).toString('base64')
11+
912
function encryptAttribute (obj, val) {
1013
// Encrypted attributes are prefixed with "aes-256-gcm$", the base64
11-
// encoding of which is "YWVzLTI1Ni1nY20k". Nulls are not encrypted.
12-
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))) {
1316
return val
1417
}
1518
if (typeof val !== 'string') {
@@ -44,8 +47,8 @@ function EncryptedAttributes (attributes, options) {
4447

4548
function decryptAttribute (obj, val) {
4649
// Encrypted attributes are prefixed with "aes-256-gcm$", the base64
47-
// encoding of which is "YWVzLTI1Ni1nY20k". Nulls are not encrypted.
48-
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)) {
4952
return val
5053
}
5154
if (options.verifyId && !obj.id) {

0 commit comments

Comments
 (0)