Coder Social home page Coder Social logo

ajimae / ncrypt-js Goto Github PK

View Code? Open in Web Editor NEW
11.0 4.0 1.0 208 KB

A light weight javascript data encryption and decryption library

License: MIT License

TypeScript 100.00%
ncrypt-js crypto javascript-library decryption cryptography simplecryptojs encryption ecmascript5 typescript encrypt

ncrypt-js's Introduction

NcryptJs

Build Status Coverage Status NPM

GitHub release (latest by date) GitHub code size in bytes GitHub issues

NcryptJs is a light weight javascript data encryption and decryption library. This library implements the nodejs default crypto functionality as a mid-channel cipher in addition to a simple and elegant custom data encoding and encryption algorithm.

Contents

Getting Started

This library is available through these javascript node package manager npm and yarn.

Installation

To use this library, first ensure you have a package manager initialized either with npm or yarn

# for npm use:
npm install --save ncrypt-js

# for yarn use:
yarn add ncrypt-js

To include ncrypt-js in your project. use one of these:

// ES6 and later
import ncrypt from "ncrypt-js";
// or import { ncrypt } from "ncrypt-js"

However, if you are using ECMAScript 5 and older, use the require statement:

// ES5 and older
var { ncrypt } = require("ncrypt-js");

Documentation

NcryptJs is a simple library with only two two exposed functions. This is all intentional mainly to keep everything simple. This is a complete documentation of the library and how to use it in your project. All examples work on both ECMAScript 6 (and later) and ECMAScript 5 (and older).

NcryptJs Methods

List of NcryptJs Methods.

Methods Description Parameters Return
[static] randomString() Random String. size: number - An optional size of the generated randomBytes.
enc: base64/hex - Encoding used for encoding the randomBytes defaults to base64
encoded: string - encoded string.
encrypt() Encrypts data. data: object/string/number/boolean - The data to be encrypted.
ciphered: string - encrypted data.
decrypt() Decrypts the encrypted or ciphered data encodedData: string - The encrypted data: string to be decrypted. data: string/object/number/boolean - The decrypted or original data (it might be string or object, depends on the initial input data type).

Using randomString method

The randomString() static method can generate random bytes encoded into a hexadecimal or base64 strings. This string can be useful in a variety of use cases e.g to generate database ids, to generate a unique string for a list, a unique serial strings etc.

var { ncrypt } = require('ncrypt-js'); // or import ncrypt from 'ncrypt-js'

var randomStr = ncrypt.randomString(8, 'base64');
console.log(randomStr) // t78WcmYAFOY=

// signature
ncrypt.randomString(size?: number, enc?: 'base64' | 'hex');

Using encrypt() and decrypt() methods

The encrypt() and decrypt() methods as of version 2.0.0 directly importing or invoking these methods is deprecated, an object must first be created with a secret, before the methods can then be invoked on the created object.

To encrypt and decrypt data, simply use encrypt() and decrypt() methods respectively. This will use AES-256-CBC encryption algorithm as the mid-channel cipher.

- var { encrypt, decrypt } = require("ncrypt-js");
+ var { ncrypt } = require("ncrypt-js");


var data = "Hello World!";
var _secretKey = "some-super-secret-key";

+ var { encrypt, decrypt } = new ncrypt(_secretKey);

// encrypting super sensitive data here
- var encryptedData = encrypt(data, _secretKey);
+ var encryptedData = encrypt(data);

console.log("Encryption process...");
console.log("Plain Text    : " + data);
console.log("Cipher Text   : " + encryptedData);

// decrypted super encrypted string here
var decryptedData = decrypt(encryptedData);
console.log("... and then decryption...");
console.log("Decipher Text : " + decryptedData);
console.log("...done.");

String Encryption

var { ncrypt } = require("ncrypt-js");

var data = "Hello World!";
var _secretKey = "some-super-secret-key";

var ncryptObject = new ncrypt(_secretKey);

// encrypting super sensitive data here
var encryptedData = ncryptObject.encrypt(data);
console.log("Encryption process...");
console.log("Plain Text    : " + data);
console.log("Cipher Text   : " + encryptedData);

// decrypted super encrypted data here
var decryptedData = ncryptObject.decrypt(encryptedData);
console.log("... and then decryption...");
console.log("Decipher Text : " + decryptedData);
console.log("...done.");

Object Encryption

Encryption and decryption of JavaScript object literal has never been simpler than this.

To encrypt and decrypt JavaScript object literal, simply use encrypt() and decrypt() function from an instance. This will use AES-CBC encryption algorithm.

var { ncrypt } = require("ncrypt-js");

var _secretKey = "some-super-secret-key";
var object = {
  NycryptJs: "is cool and fun.",
  You: "should try it!"
}

var ncryptObject = new ncrypt('ncrypt-js');

// encrypting super sensitive data here
var encryptedObject = ncryptObject.encrypt(object);
console.log("Encryption process...");
console.log("Plain Object     : ", object);
console.log("Encrypted Object : " + encryptedObject);

// decrypted super sensitive data here
var decryptedObject = ncryptObject.decrypt(encryptedObject);
console.log("... and then decryption...");
console.log("Decipher Text : ", decryptedObject);
console.log("...done.");

If you are using any sort of environmental key-value store, e.g .env and for additional security, you can add the following to your environment.

# .env

# used internally to set the `key`
KEY='sshhhh this is a super secret key'

# used internally to set the `encoding` - ['base64' | 'binary' | 'hex' | 'ucs-2' | 'ucs2' | 'utf16le']
NCRPT_ENC='hex'

SECRET='this is our hashing secret'

When creating your object, you can use the SECRET from your environment e.g:

var { ncrypt } = require('ncrypt-js');
var { encrypt, decrypt } = new ncrypt(process.env.SECRET);
...

NOTE: The secret is required to decrypt the encrypted data, if the secret used to encrypt a specific data is lost, then that data cannot be decripted.

Built With

Written in TypeScript, built into ECMAScript 5 using the TypeScript compiler.

Contribution

To contribute, simply fork this project, and issue a pull request.

Version Management

We use SemVer for version management. For the versions available, see the tags on this repository.

Authors

  • Chukwuemeka Ajima - Initial work - ajimae

License

This project is licensed under the MIT License - see the LICENSE file for details

Acknowledgments

  • This library was developed to simplify how data is encrypted and decrypted in javascript.
  • Made available by open source and special thanks to Shahid for his super simple article on node core encryption (crypto) library.
  • Thanks to danang-id whose README was very insightful and Jorgeblom for his custom cipher algorithm on this stackoverflow answer

ncrypt-js's People

Contributors

ajimae avatar dependabot[bot] avatar lojzatran avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

realsoftgt

ncrypt-js's Issues

Encrypt/Decrypt C++

has how to make a lib for c ++? I want to make requests more secure but I'm looking for a lib to encrypt via c ++ decryptar in node.js, and when I return the answer, the token will also have encryption in the node and decrypt in c ++. I can't get anyone to help me if you can help me.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.