1.0.19 • Published 2 months ago

secure-crypto-node-utils v1.0.19

Weekly downloads
-
License
ISC
Repository
-
Last release
2 months ago

Secure Crypto Node Utils

Node Secure Crypto Utils is a comprehensive library providing essential cryptographic utilities for secure communication in Node.js applications. It includes methods for encryption and decryption using the AES-256-CBC algorithm with customizable secret keys, ensuring robust security for your data transmission needs.

Features

  • Encryption: Encrypt sensitive data using the AES-256-CBC algorithm.
  • Decryption: Safely decrypt encrypted data using secure cryptographic methods.
  • Customizable Secret Keys: Customize secret keys to tailor the security level to your specific requirements.
  • Error Handling: Robust error handling ensures secure data transmission even in challenging conditions.

Installation

To install Node Secure Crypto Utils, you can use npm:

npm install node-secure-crypto-utils

Usage

Node Secure Crypto Utils is primarily used to encrypt and decrypt data transmitted between a client and a Node.js server. Here's how you can use it:

1. Create .env file

 Create a .env file to provide the encryption keys in your root directory. The folder structure should be as follows:
    |---root
     |---bin
     |   |-- .env

   In the .env file, provide the encryption keys:
    .env
    AES_ENC_SECRET_KEY=your-secret-key
    AES_ENC_IV=your-IV-key

Note:

  1. For obtaining the encryption keys, please contact hbabu@compindia.com.
  2. For suggestions, feedback, or requests, please feel free to reach out to us via email at hbabu@compindia.com. We value your input and strive to implement your suggestions.

2. Encryption:

const { encrypt } = require('secure-crypto-node-utils');

// Encrypt sensitive data
const encryptedData = encrypt('Hello World');

2. Decryption:

const { decrypt } = require('secure-crypto-node-utils');
// Decrypt encrypted data
const decryptedData = decrypt(encryptedData);

EXAMPLE

To enable encryption and decryption for request and response handling, follow these steps:

1. Decrypting Request Data:

To decrypt incoming request data, utilize middleware in your routes file. For example my folder structure is organized as follows:

    |---root
      |---bin
      |   |-- .env
      |---middlewares
      |   |-- default.mw.js
      |---routes.js

In your routes.js file, require and use the decryption middleware:

    var defaultMiddleware  = require("middlewares/default.mw.js");
    router.use(defaultMiddleware.DecryptHandler);
    // declare your API endpoint below

And in your default.mw.js middleware file:

    middlewareObj.DecryptHandler = async function (req, res, next) {
        // if you need to omit encryption you can use like below
        if (req.path == '/test' || req.path == '/test2') {
            req.is_no_enc = true;
        }else{
            // Decrypt encrypted data
            req.body = await decrypt(req.body.data);
        }

        next();
    }

2. Encrypting Response Data:

To encrypt the response data before sending it to the client, set up a function like below and call it within your API route handler while preparing the response. Ensure to use your encryption methods provided by secure-crypto-node-utils.

   async function sendResponse(req, res, response) {
        // you can add your conditions to process response
        response = { data: await encrypt(response) };
        res.json(response);
   }
1.0.19

2 months ago

1.0.18

2 months ago

1.0.17

2 months ago

1.0.16

2 months ago

1.0.15

2 months ago

1.0.14

2 months ago

1.0.13

2 months ago

1.0.12

2 months ago

1.0.11

2 months ago

1.0.10

2 months ago

1.0.9

2 months ago

1.0.8

2 months ago

1.0.7

2 months ago

1.0.6

2 months ago

1.0.5

2 months ago

1.0.4

2 months ago

1.0.3

2 months ago

1.0.2

2 months ago

1.0.1

2 months ago

1.0.0

2 months ago