JSPM

object-encrypter

0.1.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 49
  • Score
    100M100P100Q67935F
  • License MIT

Encrypt/decrypt javascript objects as strings with TTL

Package Exports

  • object-encrypter

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (object-encrypter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

node-object-encrypter

build status

Encrypt/decrypt JavaScript objects as base64 strings with optional TTL support.

Install

npm install object-encrypter

API

encrypter(secret, options)

Create an instance of encrypter, secret could be a string or an array of strings.

Options

  • ttl - enable support for TTL of the encoded data, after its' expiration you won't be able to decrypt a string, default false.

  • algorithm - cipher algorithm that will be used to encrypt/decrypt data. You can check the list of available ciphers with crypto.getCiphers(), default aes-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.

.encrypt(object, [ttl])

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.

.decrypt(generatedString)

Returns original object value if no errors occured (or time to live is not expired), in other cases returns null.

Example

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