Skip to content

s-barrah/encrypt-decrypt-library

Repository files navigation

CircleCI semantic-release semantic-release

encrypt-decrypt-library

Simple Encryption Library for encrypting strings and integers into unique tokens, safe for use in URL paths and queries. Supports 64bit signed and unsigned big endians.

Install

// Via NPM
$ npm install --save encrypt-decrypt-library

// Via Yarn
$ yarn add encrypt-decrypt-library

Usage

import Encryption from "encrypt-decrypt-library";

const config = {
   algorithm: process.env.ALGORITHM,
   encryptionKey: process.env.ENCRYPTION_KEY,
   salt: process.env.SALT,
} 
const encryption = new Encryption(config);

encryption.encrypt('Hello world')
// xxxxxxx

Documentation

The library requires the following configuration options:

  • algorithm: String - The algorithm type to be used by the library

  • encryptionKey: String - The encryption key to be used by the library

  • salt: String - The salt to be used by the library

encrypt(value: string, isInt: boolean)

Returns an encrypted value.

value

Type: string - The value that will be encrypted.

isInt

Type: boolean (optional) - The type of value. Provides support for Big Integer encryption.

Example

import Encryption from "encrypt-decrypt-library";

const config = {
   algorithm: process.env.ALGORITHM,
   encryptionKey: process.env.ENCRYPTION_KEY,
   salt: process.env.SALT,
} 
const encryption = new Encryption(config);

// Encrypted as a string
encryption.encrypt('Hello world')

// Encrypted as a string
encryption.encrypt('1234567890')

// Encrypted as an unsigned 64-bit Integer
encryption.encrypt(123, true)

decrypt(value: string, isInt: boolean)

Returns a decrypted value.

value

Type: string - The token that will be decrypted.

isInt

Type: boolean (optional) - The type of encrypted value if known. Provides support for decrypting small and big integers.

Example

import Encryption from "encrypt-decrypt-library";

const config = {
   algorithm: process.env.ALGORITHM,
   encryptionKey: process.env.ENCRYPTION_KEY,
   salt: process.env.SALT,
} 
const encryption = new Encryption(config);

// Encoded as string
encryption.decrypt('gmmBh17Q4QA=')
// xxx

// Encoded as an integer
encryption.decrypt('NF1r855MimY=', true)
// xxx 

Semantic release

Release management is automated using semantic-release.

License

The MIT License