Skip to content

Commit aa743e9

Browse files
committed
chore(ncrypt-js): add named exports
- add commonjs named exports - remove redundant dependency crypto
1 parent e1a9b91 commit aa743e9

File tree

4 files changed

+26
-39
lines changed

4 files changed

+26
-39
lines changed

README.md

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
* [NcryptJs Methods](#ncryptjs-methods)
1717
* [Using the `randomString()` methods](#using-randomstring-method)
1818
* [Using `encrypt()` and `decrypt()` methods](#using-encrypt-and-decrypt-methods)
19-
* [Using default imports](#using-default-imports)
19+
* [Stirng Encryption](#string-encryption)
20+
* [Object Encryption](#object-encryption)
2021
* [Built With](#built-with)
2122
* [Contribution](#contribution)
2223
* [Version Management](#version-management)
@@ -51,23 +52,17 @@ yarn add ncrypt-js
5152

5253
To include **_ncrypt-js_** in your project. use one of these:
5354

54-
```diff
55+
```js
5556
// ES6 and later
56-
+ import ncrypt from "ncrypt-js";
57-
- import * as ncrypt from "ncrypt-js";
58-
59-
// or
60-
- import { encrypt, decrypt } from "ncrypt-js";
57+
import ncrypt from "ncrypt-js";
58+
// or import { ncrypt } from "ncrypt-js"
6159
```
6260

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

65-
```diff
63+
```js
6664
// ES5 and older
67-
+ var ncrypt = require("ncrypt-js");
68-
69-
// or
70-
- var { encrypt, decrypt } = require("ncrypt-js");
65+
var { ncrypt } = require("ncrypt-js");
7166
```
7267

7368
## Documentation
@@ -93,7 +88,7 @@ However, if you are using ECMAScript 5 and older, use the require statement:
9388
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.
9489

9590
```ts
96-
var ncrypt = require('ncrypt-js');
91+
var { ncrypt } = require('ncrypt-js'); // or import ncrypt from 'ncrypt-js'
9792

9893
var randomStr = ncrypt.randomString(8, 'base64');
9994
console.log(randomStr) // t78WcmYAFOY=
@@ -103,13 +98,13 @@ ncrypt.randomString(size?: number, enc?: 'base64' | 'hex');
10398
```
10499

105100
### Using encrypt() and decrypt() methods
106-
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.
101+
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.
107102

108103
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.
109104

110105
```diff
111106
- var { encrypt, decrypt } = require("ncrypt-js");
112-
+ var ncrypt = require("ncrypt-js");
107+
+ var { ncrypt } = require("ncrypt-js");
113108

114109

115110
var data = "Hello World!";
@@ -132,10 +127,10 @@ console.log("Decipher Text : " + decryptedData);
132127
console.log("...done.");
133128
```
134129

135-
### Using default imports
130+
### String Encryption
136131

137132
```javascript
138-
var ncrypt = require("ncrypt-js");
133+
var { ncrypt } = require("ncrypt-js");
139134

140135
var data = "Hello World!";
141136
var _secretKey = "some-super-secret-key";
@@ -148,7 +143,7 @@ console.log("Encryption process...");
148143
console.log("Plain Text : " + data);
149144
console.log("Cipher Text : " + encryptedData);
150145

151-
// decrypted super encrypted string here
146+
// decrypted super encrypted data here
152147
var decryptedData = ncryptObject.decrypt(encryptedData);
153148
console.log("... and then decryption...");
154149
console.log("Decipher Text : " + decryptedData);
@@ -163,7 +158,7 @@ To encrypt and decrypt JavaScript object literal, simply use `encrypt()` and `de
163158

164159

165160
```javascript
166-
var ncrypt = require("ncrypt-js");
161+
var { ncrypt } = require("ncrypt-js");
167162

168163
var _secretKey = "some-super-secret-key";
169164
var object = {
@@ -176,13 +171,13 @@ var ncryptObject = new ncrypt('ncrypt-js');
176171
// encrypting super sensitive data here
177172
var encryptedObject = ncryptObject.encrypt(object);
178173
console.log("Encryption process...");
179-
console.log("Plain Object : " + object);
174+
console.log("Plain Object : ", object);
180175
console.log("Encrypted Object : " + encryptedObject);
181176

182-
// decrypted super encrypted string here
177+
// decrypted super sensitive data here
183178
var decryptedObject = ncryptObject.decrypt(encryptedObject);
184179
console.log("... and then decryption...");
185-
console.log("Decipher Text : " + decryptedObject);
180+
console.log("Decipher Text : ", decryptedObject);
186181
console.log("...done.");
187182
````
188183
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 +193,14 @@ NCRPT_ENC='hex'
198193
199194
SECRET='this is our hashing secret'
200195
```
201-
Then when creating your object, you can use the SECRET from your environment e.g:
202-
```
203-
...
204-
var ncrypt = require('ncrypt-js');
196+
When creating your object, you can use the `SECRET` from your environment e.g:
197+
198+
```js
199+
var { ncrypt } = require('ncrypt-js');
205200
var { encrypt, decrypt } = new ncrypt(process.env.SECRET);
206201
...
207202
```
203+
_**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._
208204

209205
## Built With
210206

index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import ncrypt from './src/ncrypt';
22

33
module.exports = ncrypt;
4+
module.exports.ncrypt = ncrypt;
45
export default ncrypt;
5-
export {
6-
ncrypt
7-
}
6+
export { ncrypt }

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ncrypt-js",
3-
"version": "2.1.0",
3+
"version": "2.1.1",
44
"description": "a light weight javascript data encryption and decryption library",
55
"main": "dist/index.js",
66
"scripts": {
@@ -53,8 +53,5 @@
5353
"package.json",
5454
"LICENSE",
5555
"tsconfig.json"
56-
],
57-
"dependencies": {
58-
"crypto": "^1.0.1"
59-
}
56+
]
6057
}

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -489,11 +489,6 @@ cross-spawn@^6.0.5:
489489
shebang-command "^1.2.0"
490490
which "^1.2.9"
491491

492-
crypto@^1.0.1:
493-
version "1.0.1"
494-
resolved "https://registry.yarnpkg.com/crypto/-/crypto-1.0.1.tgz#2af1b7cad8175d24c8a1b0778255794a21803037"
495-
integrity sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==
496-
497492
dashdash@^1.12.0:
498493
version "1.14.1"
499494
resolved "https://registry.yarnpkg.com/dashdash/-/dashdash-1.14.1.tgz#853cfa0f7cbe2fed5de20326b8dd581035f6e2f0"

0 commit comments

Comments
 (0)