npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@danilo_pereira/json-crypto

v1.0.4

Published

A simple way to encrypt and decrypt JSON, creating secure data transfer

Downloads

71

Readme

About

Encrypt and decrypt JSON or Simple JavaScript objects. Designed to be used as middleware between the backend and the frontend to prevent web users from getting a human readable JSON, directly in the browser. Useful for frontend applications with logic and/or data processing.

Restrictions

For now, JSON-Crypto can encrypt and decrypt any JSON. But for JavaScript Objects, the available key values must be of type: Object, Array, String, Number, Boolean, ObjectId or null. We're calling this a Simple JavaScript Object. In this case you cannot have a new Date() as a value, for example.

Installation

Use your favorite JavaScript package manager!

$ npm install @danilo_pereira/json-crypto

$ yarn add @danilo_pereira/json-crypto

Dependencies

Perhaps, the dependecies, like browser-or-node, crypto, crypto-js and bson dependency must be installed manually for the project to work.

$ npm install <DEPENDENCY>

or

$ yarn add <DEPENDENCY>

Usage

Once installed, you can import JSON-Crypto into your JavaScript project, insert at the top of your file the following line:

import jsonCrypto from '@danilo_pereira/json-crypto';

or

var jsonCrypto = require('@danilo_pereira/json-crypto');

You also can import the methods one by one, using:

import { encrypt, decrypt, encryptKey, decryptKey } from "@danilo_pereira/json-crypto";

In the latter case, you should not call json-crypto methods like jsonCrypto.encrypt(...) but use the method directly like encrypt(...)

Encrypt

To encrypt a JSON or Simple JavaScript Object you should call jsonCrypto.encrypt(json, encriptedKeys, secret, secret_2).

Arguments:

  1. json: REQUIRED A input JSON or Object that should be encrypted. Should be of type JSON or Object.
  2. encriptedKeys: OPTIONAL Define if Object keys will be also encrypted. Should be of type Boolean. The default value is false.
  3. secret: OPTIONAL Secret hex string (with up to 32 chars), used as hash to encrypt. Should be of type String. We use a default hash if not informed.
  4. secret_2: OPTIONAL Secret hex string (with up to 16 chars), also used as hash to encrypt. Should be of type String. We use a default hash if not informed.

Output

This should return a valid Simple JavaScript Object encrypted.

Example

import jsonCrypto from '@danilo_pereira/json-crypto';
...
// SIMPLE CASE:
jsonCrypto.encrypt({
    key_1: "value_1",
    key_2: 2,
    key_3: null,
    key_4: false,
    key_5: { key_5_1: "value_5" },
    key_6: ["value_6_1", "value_6_2"],
  });
/*
Output:
 {
   key_1: '126b1c3b908fc3',
   key_2: '1073002bd6be87660199ef1043',
   key_3: '1073002bd6be87670f',
   key_4: '1073002bd6b29d640f99fc444bb14f69ce5c',
   key_5: { key_5_1: '2c6b0326a3b19e7e06a3a8' },
   key_6: [ '126b1c3b908fc45452', '126b1c3b908fc45451' ]
 }
 */

// KEY ENCRYPT CASE:
jsonCrypto.encrypt({
    key_1: "value_1",
    key_2: 2,
    key_3: null,
    key_4: false,
    key_5: { key_5_1: "value_5" },
    key_6: ["value_6_1", "value_6_2"],
  }, true);
/*
Output:
{
  '0f6f0911c4': '126b1c3b908fc3',
  '0f6f0911c7': '1073002bd6be87660199ef1043',
  '0f6f0911c6': '1073002bd6be87670f',
  '0f6f0911c1': '1073002bd6b29d640f99fc444bb14f69ce5c',
  '0f6f0911c0': { '0f6f0911c08fc3': '2c6b0326a3b19e7e06a3a8' },
  '0f6f0911c3': [ '126b1c3b908fc45452', '126b1c3b908fc45451' ]
}
*/

// KEY ENCRYPT CASE, WITH OPTIONAL HASH:
jsonCrypto.encrypt({
    key_1: "value_1",
    key_2: 2,
    key_3: null,
    key_4: false,
    key_5: { key_5_1: "value_5" },
    key_6: ["value_6_1", "value_6_2"],
}, true, "cad63375bd783382", "a92e070ecd301b6de99a8985f8e9cf9f");
/*
Output:
{
  '16de1fe44c': '0bda0ace187253',
  '16de1fe44f': '09c216de5e43174cbf5b8cd709',
  '16de1fe44e': '09c216de5e43174db1',
  '16de1fe449': '09c216de5e4f0d4eb15b9f830178d87bc7ff',
  '16de1fe448': { '16de1fe4487253': '35da15d32b4c0e54b861cb' },
  '16de1fe44b': [ '0bda0ace1872547eec', '0bda0ace1872547eef' ]
}
In this case, the secret was defined at: "cad63375bd783382####################" (32 chars) and secret_2 was defined at: "a92e070ecd301b6d" (16ch)
*/

Decrypt

To decrypt a JSON or Simple JavaScript Object you should call jsonCrypto.decrypt(json, encriptedKeys, secret, secret_2).

Arguments:

  1. json: REQUIRED A input JSON or Object that should be decrypted. Should be of type JSON or Object.
  2. encriptedKeys: OPTIONAL Define if Object keys will be also decrypted. Should be of type Boolean. The default value is false.
  3. secret: OPTIONAL Secret hex string (with up to 32 chars). Needs to be the same secret hash used to encrypt. Should be of type String. We use a default hash if not informed.
  4. secret_2: OPTIONAL Secret hex string (with up to 16 chars). Needs to be the same secret hash used to encrypt. Should be of type String. We use a default hash if not informed.

Output

This should return a valid Simple JavaScript Object decrypted.

Example

import jsonCrypto from '@danilo_pereira/json-crypto';
...
// SIMPLE CASE:
jsonCrypto.decrypt(
  {
   key_1: '126b1c3b908fc3',
   key_2: '1073002bd6be87660199ef1043',
   key_3: '1073002bd6be87670f',
   key_4: '1073002bd6b29d640f99fc444bb14f69ce5c',
   key_5: { key_5_1: '2c6b0326a3b19e7e06a3a8' },
   key_6: [ '126b1c3b908fc45452', '126b1c3b908fc45451' ]
 });
/*
Output:
 {
    key_1: "value_1",
    key_2: 2,
    key_3: null,
    key_4: false,
    key_5: { key_5_1: "value_5" },
    key_6: ["value_6_1", "value_6_2"],
  }
 */

// KEY ENCRYPT CASE:
jsonCrypto.decrypt(
  {
  '0f6f0911c4': '126b1c3b908fc3',
  '0f6f0911c7': '1073002bd6be87660199ef1043',
  '0f6f0911c6': '1073002bd6be87670f',
  '0f6f0911c1': '1073002bd6b29d640f99fc444bb14f69ce5c',
  '0f6f0911c0': { '0f6f0911c08fc3': '2c6b0326a3b19e7e06a3a8' },
  '0f6f0911c3': [ '126b1c3b908fc45452', '126b1c3b908fc45451' ]
}, true);
/*
Output:
{
    key_1: "value_1",
    key_2: 2,
    key_3: null,
    key_4: false,
    key_5: { key_5_1: "value_5" },
    key_6: ["value_6_1", "value_6_2"],
  }
*/


// KEY ENCRYPT CASE, WITH OPTIONAL HASH:
jsonCrypto.decrypt({
  '16de1fe44c': '0bda0ace187253',
  '16de1fe44f': '09c216de5e43174cbf5b8cd709',
  '16de1fe44e': '09c216de5e43174db1',
  '16de1fe449': '09c216de5e4f0d4eb15b9f830178d87bc7ff',
  '16de1fe448': { '16de1fe4487253': '35da15d32b4c0e54b861cb' },
  '16de1fe44b': [ '0bda0ace1872547eec', '0bda0ace1872547eef' ]
}, true, "cad63375bd783382", "a92e070ecd301b6de99a8985f8e9cf9f");
/*
Output:
{
    key_1: "value_1",
    key_2: 2,
    key_3: null,
    key_4: false,
    key_5: { key_5_1: "value_5" },
    key_6: ["value_6_1", "value_6_2"],
}
In this case, the secret was defined at: "cad63375bd783382####################" (32 chars) and secret_2 was defined at: "a92e070ecd301b6d" (16ch)
*/

Decrypt Syngle Key

To decrypt a String you should call jsonCrypto.decryptKey(string, secret, secret_2).

Arguments:

  1. string: REQUIRED A input string that should be decrypted. Should be of type string.
  2. secret: OPTIONAL Secret hex string (with up to 32 chars). Needs to be the same secret hash used to encrypt. Should be of type String. We use a default hash if not informed.
  3. secret_2: OPTIONAL Secret hex string (with up to 16 chars). Needs to be the same secret hash used to encrypt. Should be of type String. We use a default hash if not informed.

Output

This should return a String, Number, Boolean or null decrypted.

Example

import jsonCrypto from '@danilo_pereira/json-crypto';
...
// SIMPLE CASE:
jsonCrypto.decryptKey("126b1c3b908fc3");
/*
Output:
"value_1"
 */

Encrypt Syngle Key

To encrypt a String you should call jsonCrypto.encryptKey(string, secret, secret_2).

Arguments:

  1. string: REQUIRED A input string that should be encrypted. Should be of type string.
  2. secret: OPTIONAL Secret hex string (with up to 32 chars). Needs to be the same secret hash used to encrypt. Should be of type String. We use a default hash if not informed.
  3. secret_2: OPTIONAL Secret hex string (with up to 16 chars). Needs to be the same secret hash used to encrypt. Should be of type String. We use a default hash if not informed.

Output

This should return a String, Number, Boolean or null decrypted.

Example

import jsonCrypto from '@danilo_pereira/json-crypto';
...
// SIMPLE CASE:
jsonCrypto.decryptKey("126b1c3b908fc3");
/*
Output:
"value_1"
 */

Authors

License

This project is under the GNU General Public License.


Documentation Template made by: