Skip to content

Commit

Permalink
chore(ncrypt-js): add named exports
Browse files Browse the repository at this point in the history
- add commonjs named exports
- remove redundant dependency crypto
  • Loading branch information
ajimae committed Mar 30, 2024
1 parent e1a9b91 commit 23ac39c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
47 changes: 21 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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=
Expand All @@ -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!";
Expand All @@ -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";
Expand All @@ -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);
Expand All @@ -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 = {
Expand All @@ -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.
Expand All @@ -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

Expand Down
5 changes: 2 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import ncrypt from './src/ncrypt';

module.exports = ncrypt;
module.exports.ncrypt = ncrypt;
export default ncrypt;
export {
ncrypt
}
export { ncrypt }
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -53,8 +53,5 @@
"package.json",
"LICENSE",
"tsconfig.json"
],
"dependencies": {
"crypto": "^1.0.1"
}
]
}
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 23ac39c

Please sign in to comment.