Encrypt/decrypt JavaScript objects as base64 strings with optional TTL support.
npm install object-encrypter
Create an instance of encrypter, secret
could be a string
or an array
of strings.
-
ttl
- enable support for TTL of the encoded data, after its' expiration you won't be able to decrypt a string, defaultfalse
. -
algorithm
- cipher algorithm that will be used to encrypt/decrypt data. You can check the list of available ciphers withcrypto.getCiphers()
, defaultaes-256-cbc
. -
outputEncoding
- option should be one of'latin1'
,'base64'
(default) or'hex'
.
It returns 2 methods that will do the main thing encryption/decryption of an object.
Returns Base64 encoded string. ttl
is ms
value of during how long time this string could be decrypted, it's needed only if ttl
option is enabled.
Returns original object value if no errors occured (or time to live is not expired), in other cases returns null
.
var encrypter = require('object-encrypter');
var engine = encrypter('your secret', {ttl: true});
var obj = {
userId: 12345,
description: 'test description',
valid: true,
tags: ['encrypt', 'decrypt', 'ttl']
};
var hex = engine.encrypt(obj, 5000); // generated string will live 5 seconds
console.log(hex);
// ->
// 'eyJ1c2VySWQiOjEyMzQ1LCJkZXNjcmlwdGlvbiI6InRlc3QgZGVzY3JpcHRpb24iLCJ2YWxpZCI6dHJ1ZSwidGFncyI6WyJlbmNyeXB0IiwiZGVjcnlwdCIsInR0bCJdLCJleHBpcmVzIjoxNDEwNjkyODQ3NTU2fQo4ZTczNjhkMDc2ZWZhZWNlMGViYjYzYTAxOTBhNzU5Yw'
console.dir(engine.decrypt(hex));
// ->
// { userId: 12345,
// description: 'test description',
// valid: true,
// tags: [ 'encrypt', 'decrypt', 'ttl' ],
// expires: 1410692847556 }
(c) 2014 MIT License