Recursively encrypt/decrypt objects selectively by keys.
$ npm install crypto-json --save
const cryptoJSON = require('crypto-json')
cryptoJSON.encrypt(object, password, [config]) => encryptedObject
cryptoJSON.decrypt(encryptedObject, password, [config]) => object
password
Random password, length according to the selected algorithm, e.g. 32 bytes length with aes-256-cbc
.
config (optional)
algorithm
- select any supported by the version of Node you are using (default:aes-256-cbc
)encoding
-hex
,base64
,binary
(default:hex
)keys
- specify which keys to encrypting/decrypting (default:[]
, i.e. encrypt/decrypt everything)
const util = require('util')
const cryptoJSON = require('crypto-json')
const algorithm = 'aes-256-cbc'
const encoding = 'hex'
const input = {
hello: {
bar: ['hello', 'world'],
baz: {
secret: 'hide a secret',
b: {test: 1}
}
}
}
const password = 'random password 32 bytes length.'
// keys act like a white list, so for example if you want to encrypt a nested
// key "secret" you also need to specify its parent keys,
// i.e. "secret", "baz", "hello" in the above input object
const keys = ['hello', 'baz', 'secret']
const output = cryptoJSON.encrypt(
input, password, {encoding, keys, algorithm}
)
console.log(util.inspect(input ,{showHidden: false, depth: null, colors: true}))
console.log(util.inspect(output ,{showHidden: false, depth: null, colors: true}))
/*
{
hello: {
bar: [ 'hello', 'world' ],
baz: {
secret: 'b2114cc78fcee8c58a14ba2df511dd05:e5a58d9b9eaab60ca0830d1c7ad4fd41',
b: { test: 1 }
}
}
}
*/