Comparing version 1.0.0 to 2.0.0
34
index.js
@@ -7,5 +7,5 @@ // Part of https://github.com/chris-rock/node-crypto-examples | ||
function deepCrypto(password, fieldsToExclude, algorithm) { | ||
fieldsToExclude = fieldsToExclude || [] | ||
algorithm = algorithm || 'aes-256-ctr' | ||
function deepCrypto(password, opts) { | ||
var fieldsToExclude = opts.exclude || [], | ||
algorithm = opts.algorithm || 'aes-256-ctr' | ||
@@ -26,10 +26,8 @@ function encrypt(text){ | ||
function deepEncrypt(obj) { | ||
function e(v) { | ||
return Object.keys(v).reduce( (acc, item) => { | ||
// skip id and version fields | ||
// skip excluded fields | ||
if (fieldsToExclude.indexOf(item) > -1) { | ||
@@ -52,5 +50,4 @@ acc[item] = v[item] | ||
var obj = {} | ||
obj[item] = v[item] | ||
acc[item] = encrypt( JSON.stringify( obj ) ) | ||
var val = 'A' + crypto.randomBytes(4).toString() + JSON.stringify( v[item] ) | ||
acc[item] = encrypt( val ) | ||
return acc; | ||
@@ -71,2 +68,3 @@ | ||
// skip excluded fields | ||
if (fieldsToExclude.indexOf(item) > -1) { | ||
@@ -89,4 +87,18 @@ acc[item] = v[item] | ||
acc[item] = JSON.parse( decrypt(v[item]) )[item] | ||
return acc; | ||
var val = decrypt(v[item]) | ||
// version 1 | ||
if (val[0] === '{') { | ||
acc[item] = JSON.parse( val )[item] | ||
return acc; | ||
} | ||
// version 2 | ||
if (val[0] === 'A') { | ||
acc[item] = JSON.parse( val.substr(5) ) | ||
return acc; | ||
} | ||
throw 'unsupported version!' | ||
}, {}) | ||
@@ -93,0 +105,0 @@ } |
{ | ||
"name": "deepcrypt", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "encrypt/decrypt js objects", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
32
test.js
var test = require('tape') | ||
var dc = require('./index')('foo', ['id', 'updated']) | ||
var dc = require('./index')('shhhhhhh', { exclude: ['id', 'updated'] }) | ||
test('deepcrypt', function(t) { | ||
var e = dc.deepEncrypt | ||
var d = dc.deepDecrypt | ||
var deepEncrypt = dc.deepEncrypt | ||
var deepDecrypt = dc.deepDecrypt | ||
t.plan(8) | ||
// test strings | ||
t.deepEqual(e({ foo: "foo" }), { foo: '36e506799ef6510dc711dc99bb' }); | ||
t.deepEqual(d({ foo: '36e506799ef6510dc711dc99bb' }), { foo: "foo" }); | ||
// basic tests | ||
t.deepEqual(deepDecrypt(deepEncrypt({ foo: "foo" })), { foo: "foo" }); | ||
t.deepEqual(deepDecrypt(deepEncrypt({ foo: 1 })), { foo: 1 }); | ||
t.deepEqual(deepDecrypt(deepEncrypt({ foo: 1, bar: true })), { foo: 1, bar: true }); | ||
// test numbers | ||
t.deepEqual(e({ foo: 1 }), { foo: '36e506799ef6511edc' } ); | ||
t.deepEqual(d({ foo: '36e506799ef6511edc' }), { foo: 1 }); | ||
// test exlude fields | ||
var obj = deepEncrypt({ id: 1, updated: "test", foo: 1, bar: true }) | ||
var encrypted = deepEncrypt(obj) | ||
t.equal(obj.id, encrypted.id) | ||
t.equal(obj.updated, encrypted.updated) | ||
t.notEqual(obj.foo, encrypted.foo) | ||
t.notEqual(obj.bar, encrypted.bar) | ||
t.deepEqual(deepDecrypt(encrypted), obj); | ||
// test boolean values | ||
t.deepEqual(e({ foo: 1, bar: true }), { foo: '36e506799ef6511edc', bar: '36e5027783f6515bd30bd6c6', } ); | ||
t.deepEqual(d({ foo: '36e506799ef6511edc', bar: '36e5027783f6515bd30bd6c6', } ), { foo: 1, bar: true }); | ||
// check fields to exclude | ||
t.deepEqual(e({ id: 1, updated: "test", foo: 1, bar: true }), { id: 1, updated: "test", foo: '36e506799ef6511edc', bar: '36e5027783f6515bd30bd6c6', } ); | ||
t.deepEqual(d({ id: 1, foo: '36e506799ef6511edc', bar: '36e5027783f6515bd30bd6c6', }), { id: 1, foo: 1, bar: true }); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
5444
6
106
1
24