Skip to content

Commit 42f4fc1

Browse files
committed
Compatibility with node v4
1 parent b16d0d6 commit 42f4fc1

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: node_js
22
node_js:
3+
- "4"
34
- "6"
45
- "8"

lib/encrypted-attr.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const alg = 'aes-256-gcm'
44
const crypto = require('crypto')
5-
const { get, set } = require('lodash')
5+
const get = require('lodash').get
6+
const set = require('lodash').set
67

78
function EncryptedAttributes (attributes, options) {
89
options = options || {}
@@ -24,9 +25,11 @@ function EncryptedAttributes (attributes, options) {
2425
// Recommended 96-bit nonce with AES-GCM.
2526
let iv = crypto.randomBytes(12)
2627
let aad = Buffer.from(
27-
`aes-256-gcm$${options.verifyId ? obj.id.toString() : ''}$${options.keyId}`)
28+
`${alg}$${options.verifyId ? obj.id.toString() : ''}$${options.keyId}`)
2829
let key = Buffer.from(options.keys[options.keyId], 'base64')
29-
let gcm = crypto.createCipheriv('aes-256-gcm', key, iv).setAAD(aad)
30+
let gcm = crypto.createCipheriv(alg, key, iv)
31+
gcm.setAAD(aad)
32+
3033
let result = gcm.update(val, 'utf8', 'base64') + gcm.final('base64')
3134

3235
return aad.toString('base64') + '$' +
@@ -54,16 +57,26 @@ function EncryptedAttributes (attributes, options) {
5457
if (options.verifyId && !obj.id) {
5558
throw new Error('Cannot decrypt without \'id\' attribute')
5659
}
57-
let [aad, iv, payload, tag] = val.split('$').map((x) => Buffer.from(x, 'base64'))
58-
let [, id, keyId] = aad.toString().split('$')
60+
let parts = val.split('$').map((x) => Buffer.from(x, 'base64'))
61+
let aad = parts[0]
62+
let iv = parts[1]
63+
let payload = parts[2]
64+
let tag = parts[3]
65+
66+
parts = aad.toString().split('$')
67+
let id = parts[1]
68+
let keyId = parts[2]
69+
5970
if (options.verifyId && (id !== obj.id.toString())) {
6071
throw new Error('Encrypted attribute has invalid id')
6172
}
6273
if (!options.keys[keyId]) {
6374
throw new Error('Encrypted attribute has invalid key id')
6475
}
6576
let key = Buffer.from(options.keys[keyId], 'base64')
66-
let gcm = crypto.createDecipheriv('aes-256-gcm', key, iv).setAAD(aad).setAuthTag(tag)
77+
let gcm = crypto.createDecipheriv(alg, key, iv)
78+
gcm.setAAD(aad)
79+
gcm.setAuthTag(tag)
6780

6881
return gcm.update(payload, 'binary', 'utf8') + gcm.final('utf8')
6982
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Encrypted model attributes in your favourite ORM.",
55
"author": "Simon Ratner (https://github.com/simonratner)",
66
"engines": {
7-
"node": ">=6.0"
7+
"node": ">=4.0"
88
},
99
"keywords": [
1010
"attr_encrypted",
@@ -17,15 +17,15 @@
1717
"dependencies": {
1818
"lodash": "^4.17.4"
1919
},
20-
"optionalDependencies": {},
2120
"devDependencies": {
2221
"mocha": "^3.5.0",
2322
"mocha-standard": "^1.0.0",
2423
"standard": "^10.0.3",
2524
"unexpected": "^10.33.2"
2625
},
26+
"optionalDependencies": {},
2727
"scripts": {
28-
"test": "./node_modules/.bin/mocha -R spec --recursive"
28+
"test": "mocha -R spec --recursive"
2929
},
3030
"homepage": "https://github.com/simonratner/node-encrypted-attr",
3131
"repository": {

0 commit comments

Comments
 (0)