From 23ac39c112a2a781616cbf7414323fa2558c752f Mon Sep 17 00:00:00 2001 From: Chukwuemeka Ajima Date: Sat, 30 Mar 2024 22:23:46 +0100 Subject: [PATCH] chore(ncrypt-js): add named exports - add commonjs named exports - remove redundant dependency crypto --- README.md | 47 +++++++++++++++++++++-------------------------- index.ts | 5 ++--- package.json | 7 ++----- yarn.lock | 5 ----- 4 files changed, 25 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index cfa9b92..3b3744c 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ * [NcryptJs Methods](#ncryptjs-methods) * [Using the `randomString()` methods](#using-randomstring-method) * [Using `encrypt()` and `decrypt()` methods](#using-encrypt-and-decrypt-methods) - * [Using default imports](#using-default-imports) + * [Stirng Encryption](#string-encryption) + * [Object Encryption](#object-encryption) * [Built With](#built-with) * [Contribution](#contribution) * [Version Management](#version-management) @@ -51,23 +52,16 @@ yarn add ncrypt-js To include **_ncrypt-js_** in your project. use one of these: -```diff +```js // ES6 and later -+ import ncrypt from "ncrypt-js"; -- import * as ncrypt from "ncrypt-js"; - -// or -- import { encrypt, decrypt } from "ncrypt-js"; +import ncrypt from "ncrypt-js"; ``` However, if you are using ECMAScript 5 and older, use the require statement: -```diff +```js // ES5 and older -+ var ncrypt = require("ncrypt-js"); - -// or -- var { encrypt, decrypt } = require("ncrypt-js"); +var { ncrypt } = require("ncrypt-js"); ``` ## Documentation @@ -93,7 +87,7 @@ However, if you are using ECMAScript 5 and older, use the require statement: The `randomString()` static method can generate [random bytes](https://nodejs.org/api/crypto.html#cryptorandombytessize-callback) 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. ```ts -var ncrypt = require('ncrypt-js'); +var { ncrypt } = require('ncrypt-js'); // or import ncrypt from 'ncrypt-js' var randomStr = ncrypt.randomString(8, 'base64'); console.log(randomStr) // t78WcmYAFOY= @@ -103,13 +97,13 @@ 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 be created with a secret first, before the methods can now be invoked on the created object. +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. ```diff - var { encrypt, decrypt } = require("ncrypt-js"); -+ var ncrypt = require("ncrypt-js"); ++ var { ncrypt } = require("ncrypt-js"); var data = "Hello World!"; @@ -132,10 +126,10 @@ console.log("Decipher Text : " + decryptedData); console.log("...done."); ``` -### Using default imports +### String Encryption ```javascript -var ncrypt = require("ncrypt-js"); +var { ncrypt } = require("ncrypt-js"); var data = "Hello World!"; var _secretKey = "some-super-secret-key"; @@ -148,7 +142,7 @@ console.log("Encryption process..."); console.log("Plain Text : " + data); console.log("Cipher Text : " + encryptedData); -// decrypted super encrypted string here +// decrypted super encrypted data here var decryptedData = ncryptObject.decrypt(encryptedData); console.log("... and then decryption..."); console.log("Decipher Text : " + decryptedData); @@ -163,7 +157,7 @@ To encrypt and decrypt JavaScript object literal, simply use `encrypt()` and `de ```javascript -var ncrypt = require("ncrypt-js"); +var { ncrypt } = require("ncrypt-js"); var _secretKey = "some-super-secret-key"; var object = { @@ -176,13 +170,13 @@ 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("Plain Object : ", object); console.log("Encrypted Object : " + encryptedObject); -// decrypted super encrypted string here +// decrypted super sensitive data here var decryptedObject = ncryptObject.decrypt(encryptedObject); console.log("... and then decryption..."); -console.log("Decipher Text : " + decryptedObject); +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. @@ -198,13 +192,14 @@ NCRPT_ENC='hex' SECRET='this is our hashing secret' ``` -Then when creating your object, you can use the SECRET from your environment e.g: -``` -... -var ncrypt = require('ncrypt-js'); +When creating your object, you can use the `SECRET` from your environment e.g: + +```js +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 diff --git a/index.ts b/index.ts index 13c23a9..65ccd94 100644 --- a/index.ts +++ b/index.ts @@ -1,7 +1,6 @@ import ncrypt from './src/ncrypt'; module.exports = ncrypt; +module.exports.ncrypt = ncrypt; export default ncrypt; -export { - ncrypt -} +export { ncrypt } diff --git a/package.json b/package.json index 52a006d..63a8551 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ncrypt-js", - "version": "2.1.0", + "version": "2.1.1", "description": "a light weight javascript data encryption and decryption library", "main": "dist/index.js", "scripts": { @@ -53,8 +53,5 @@ "package.json", "LICENSE", "tsconfig.json" - ], - "dependencies": { - "crypto": "^1.0.1" - } + ] } diff --git a/yarn.lock b/yarn.lock index 5b6b59f..5794050 100644 --- a/yarn.lock +++ b/yarn.lock @@ -489,11 +489,6 @@ cross-spawn@^6.0.5: shebang-command "^1.2.0" which "^1.2.9" -crypto@^1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/crypto/-/crypto-1.0.1.tgz#2af1b7cad8175d24c8a1b0778255794a21803037" - integrity sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig== - dashdash@^1.12.0: version "1.14.1" resolved "https://registry.yarnpkg.com/dashdash/-/dashdash-1.14.1.tgz#853cfa0f7cbe2fed5de20326b8dd581035f6e2f0"