1.0.0 • Published 4 years ago

encryptor-node v1.0.0

Weekly downloads
78
License
MIT
Repository
github
Last release
4 years ago

Encryptor

Build Status

Library for encrypt and decrypt messages using a secret key.

Installation

npm install --save encryptor-node

Import methods

// As a whole
import * as Encryptor from 'encryptor-node';

// Each method
import { encrypt, decrypt } from 'encryptor-node';

Encrypting and decrypting a message or object

import { encrypt, decrypt } from 'encryptor-node';

const secret = 's3cr3t!';
const payload = { message: 'This is an very important message' };

// Encrypting
const encrypted = encrypt(secret, payload);
console.log(encrypted); // e20f64009fe0daa88......

// Decrypting
const result = decrypt(secret, encrypted);
console.log(result); // { message: 'This is an very important message' }

Extra Options

The encrypt function takes an additional optional options hash consisting of the following:

{
  /**
   * Optional - define the algorithm used for encryption
   * @default aes-256-cbc
   */
  algorithm?: string;

  /**
   * Optional - pass your own salt
   */
  salt?: string;

  /**
   * Optional - specify how long the generated salt string should be
   */
  saltLength?: number;

  /**
   * Optional - return encrypted data as a hex string, rather than a buffer. Defaults to true.
   */
  stringify?: boolean;
}

This is provided to allow the developer somewhat more control over the internals of the library. The decrypt function takes an options hash with just the algorithm key.

The algorithm must be compatible with the built-in node crypto library.