Skip to content

Commit ae417b7

Browse files
authored
Merge pull request #8 from simonratner/node-4
Compatibility with node v4
2 parents b16d0d6 + 57546d7 commit ae417b7

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
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: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
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
7+
const Buffer = require('safe-buffer').Buffer
68

79
function EncryptedAttributes (attributes, options) {
810
options = options || {}
@@ -24,9 +26,11 @@ function EncryptedAttributes (attributes, options) {
2426
// Recommended 96-bit nonce with AES-GCM.
2527
let iv = crypto.randomBytes(12)
2628
let aad = Buffer.from(
27-
`aes-256-gcm$${options.verifyId ? obj.id.toString() : ''}$${options.keyId}`)
29+
`${alg}$${options.verifyId ? obj.id.toString() : ''}$${options.keyId}`)
2830
let key = Buffer.from(options.keys[options.keyId], 'base64')
29-
let gcm = crypto.createCipheriv('aes-256-gcm', key, iv).setAAD(aad)
31+
let gcm = crypto.createCipheriv(alg, key, iv)
32+
gcm.setAAD(aad)
33+
3034
let result = gcm.update(val, 'utf8', 'base64') + gcm.final('base64')
3135

3236
return aad.toString('base64') + '$' +
@@ -54,16 +58,26 @@ function EncryptedAttributes (attributes, options) {
5458
if (options.verifyId && !obj.id) {
5559
throw new Error('Cannot decrypt without \'id\' attribute')
5660
}
57-
let [aad, iv, payload, tag] = val.split('$').map((x) => Buffer.from(x, 'base64'))
58-
let [, id, keyId] = aad.toString().split('$')
61+
let parts = val.split('$').map((x) => Buffer.from(x, 'base64'))
62+
let aad = parts[0]
63+
let iv = parts[1]
64+
let payload = parts[2]
65+
let tag = parts[3]
66+
67+
parts = aad.toString().split('$')
68+
let id = parts[1]
69+
let keyId = parts[2]
70+
5971
if (options.verifyId && (id !== obj.id.toString())) {
6072
throw new Error('Encrypted attribute has invalid id')
6173
}
6274
if (!options.keys[keyId]) {
6375
throw new Error('Encrypted attribute has invalid key id')
6476
}
6577
let key = Buffer.from(options.keys[keyId], 'base64')
66-
let gcm = crypto.createDecipheriv('aes-256-gcm', key, iv).setAAD(aad).setAuthTag(tag)
78+
let gcm = crypto.createDecipheriv(alg, key, iv)
79+
gcm.setAAD(aad)
80+
gcm.setAuthTag(tag)
6781

6882
return gcm.update(payload, 'binary', 'utf8') + gcm.final('utf8')
6983
}

package.json

Lines changed: 5 additions & 4 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",
@@ -15,17 +15,18 @@
1515
"orm"
1616
],
1717
"dependencies": {
18-
"lodash": "^4.17.4"
18+
"lodash": "^4.17.4",
19+
"safe-buffer": "^5.1.1"
1920
},
20-
"optionalDependencies": {},
2121
"devDependencies": {
2222
"mocha": "^3.5.0",
2323
"mocha-standard": "^1.0.0",
2424
"standard": "^10.0.3",
2525
"unexpected": "^10.33.2"
2626
},
27+
"optionalDependencies": {},
2728
"scripts": {
28-
"test": "./node_modules/.bin/mocha -R spec --recursive"
29+
"test": "mocha -R spec --recursive"
2930
},
3031
"homepage": "https://github.com/simonratner/node-encrypted-attr",
3132
"repository": {

0 commit comments

Comments
 (0)