You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+34-22Lines changed: 34 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,25 +6,17 @@
6
6
7
7
**_NcryptJs_** is a light weight javascript data encryption and decryption library. This library implements the nodejs default crypto functionality as a mid-channel cipher in addition to a simple and elegant custom data encoding and encryption algorithm.
*[Using the `randomString()` methods](#using-randomstring-method)
18
+
*[Using `encrypt()` and `decrypt()` methods](#using-encrypt-and-decrypt-methods)
19
+
*[Using default imports](#using-default-imports)
28
20
*[Built With](#built-with)
29
21
*[Contribution](#contribution)
30
22
*[Version Management](#version-management)
@@ -82,23 +74,38 @@ However, if you are using ECMAScript 5 and older, use the require statement:
82
74
83
75
**_NcryptJs_** is a simple library with only two two exposed functions. This is all intentional mainly to keep everything simple. This is a complete documentation of the library and how to use it in your project. All examples work on both ECMAScript 6 (and later) and ECMAScript 5 (and older).
|[_static_]**randomString()**| Random String. |**size**: _number_ - An optional size of the generated `randomBytes`. <br/>**enc:**_base64/hex_ - Encoding used for encoding the `randomBytes` defaults to _`base64`_|**encoded**: _string_ - encoded string. |
94
87
|**encrypt()**| Encrypts data. |**data**: _object/string/number/boolean_ - The data to be encrypted. <br/>|**ciphered**: _string_ - encrypted data. |
95
88
| **decrypt()** | Decrypts the encrypted or ciphered data | **encodedData**: string - The encrypted data: _string_ to be decrypted. | **data**: _string/object/number/boolean_ - The decrypted or original data (it might be string or object, depends on the initial input data type).
96
89
97
90
91
+
### Using randomString method
92
+
93
+
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.
94
+
95
+
```ts
96
+
var ncrypt =require('ncrypt-js');
97
+
98
+
var randomStr =ncrypt.randomString(8, 'base64');
99
+
console.log(randomStr) // t78WcmYAFOY=
98
100
99
-
#### Using `encrypt()` and `decrypt()` functons - As of version 2.0.0 directly importing or invoking these functions is deprecated, an object must be created with a secret first, before the methods can now be invoked on the created object.
To encrypt and decrypt data, simply use `encrypt()` and `decrypt()` functions respectively. This will use `AES-256-CBC` encryption algorithm as the mid-channel cipher.
105
+
### 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.
107
+
108
+
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.
102
109
103
110
```diff
104
111
- var { encrypt, decrypt } = require("ncrypt-js");
@@ -108,7 +115,7 @@ To encrypt and decrypt data, simply use `encrypt()` and `decrypt()` functions re
108
115
var data = "Hello World!";
109
116
var _secretKey = "some-super-secret-key";
110
117
111
-
+ var { encodeData, decodeData } = new ncrypt(_secretKey);
118
+
+ var { encrypt, decrypt } = new ncrypt(_secretKey);
112
119
113
120
// encrypting super sensitive data here
114
121
- var encryptedData = encrypt(data, _secretKey);
@@ -180,11 +187,16 @@ console.log("...done.");
180
187
````
181
188
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.
182
189
183
-
```diff
184
-
// .env
190
+
```bash
191
+
# .env
192
+
193
+
# used internally to set the `key`
194
+
KEY='sshhhh this is a super secret key'
195
+
196
+
# used internally to set the `encoding` - ['base64' | 'binary' | 'hex' | 'ucs-2' | 'ucs2' | 'utf16le']
197
+
NCRPT_ENC='hex'
185
198
186
-
KEY=sshhhh this is a super secret key
187
-
SECRET=this is our hashing secret
199
+
SECRET='this is our hashing secret'
188
200
```
189
201
Then when creating your object, you can use the SECRET from your environment e.g:
0 commit comments