Skip to content

Commit e1a9b91

Browse files
committed
feat(randomStr): add random string generated
- add randomString static method - remove ncrypt.d.ts file - add ci.yml file to test on master and pr - move all functions from utils.ts to ncrypt.ts file - remove redundant travis.yml file - refactor source code
1 parent ded0a51 commit e1a9b91

File tree

11 files changed

+240
-223
lines changed

11 files changed

+240
-223
lines changed

.github/workflows/ci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Test
2+
on:
3+
push:
4+
branches: [ master ]
5+
pull_request:
6+
branches: [ master ]
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
- uses: actions/setup-node@v3
14+
with:
15+
node-version: 12
16+
cache: 'yarn'
17+
- run: yarn install --production=false
18+
- run: yarn test

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: node setup
1818
uses: actions/setup-node@v4
1919
with:
20-
node-version: 14
20+
node-version: 12
2121
cache: 'yarn'
2222

2323
- name: install dependencies
@@ -39,7 +39,7 @@ jobs:
3939
- name: node setup
4040
uses: actions/setup-node@v4
4141
with:
42-
node-version: 14
42+
node-version: 12
4343
registry-url: https://registry.npmjs.org
4444
cache: 'yarn'
4545

.travis.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

README.md

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,17 @@
66

77
**_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.
88

9-
<!-- ```diff
10-
- const ReduxThunk = require('redux-thunk')
11-
+ const ReduxThunk = require('redux-thunk').default
12-
``` -->
13-
149
## Contents
1510

1611
* [NcryptJs](#NcryptJs)
1712
* [Contents](#contents)
18-
<!-- * [Changes Log (What's New)](#changes-log-whats-new) -->
1913
* [Getting Started](#getting-started)
2014
* [Installation](#installation)
2115
* [Documentation](#documentation)
22-
* [NcryptJs Functions](#ncryptjs-functions)
23-
* [Using `encrypt()` and `decrypt()`](#using-encrypt-and-decrypt)
24-
* [Using default imports](#Using-default-imports)
25-
<!-- * [Change the Secret Key](#change-the-secret-key)
26-
* [Object Encryption](#object-encryption)
27-
* [Random Key Generator](#random-key-generator) -->
16+
* [NcryptJs Methods](#ncryptjs-methods)
17+
* [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)
2820
* [Built With](#built-with)
2921
* [Contribution](#contribution)
3022
* [Version Management](#version-management)
@@ -82,23 +74,38 @@ However, if you are using ECMAScript 5 and older, use the require statement:
8274

8375
**_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).
8476

85-
### NcryptJs Functions
77+
### NcryptJs Methods
8678

8779

88-
### List of **_NcryptJs_** functions.
80+
### List of **_NcryptJs_** Methods.
8981

9082

9183

92-
| Functions | Description | Parameters | Return |
84+
| Methods | Description | Parameters | Return |
9385
| ------------------------------------------------------------- | -------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
86+
| [_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. |
9487
| **encrypt()** | Encrypts data. |**data**: _object/string/number/boolean_ - The data to be encrypted. <br/>|**ciphered**: _string_ - encrypted data. |
9588
| **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).
9689

9790

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=
98100

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.
101+
// signature
102+
ncrypt.randomString(size?: number, enc?: 'base64' | 'hex');
103+
```
100104

101-
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.
102109

103110
```diff
104111
- var { encrypt, decrypt } = require("ncrypt-js");
@@ -108,7 +115,7 @@ To encrypt and decrypt data, simply use `encrypt()` and `decrypt()` functions re
108115
var data = "Hello World!";
109116
var _secretKey = "some-super-secret-key";
110117

111-
+ var { encodeData, decodeData } = new ncrypt(_secretKey);
118+
+ var { encrypt, decrypt } = new ncrypt(_secretKey);
112119

113120
// encrypting super sensitive data here
114121
- var encryptedData = encrypt(data, _secretKey);
@@ -180,11 +187,16 @@ console.log("...done.");
180187
````
181188
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.
182189

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'
185198
186-
KEY=sshhhh this is a super secret key
187-
SECRET=this is our hashing secret
199+
SECRET='this is our hashing secret'
188200
```
189201
Then when creating your object, you can use the SECRET from your environment e.g:
190202
```

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ncrypt-js",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
44
"description": "a light weight javascript data encryption and decryption library",
55
"main": "dist/index.js",
66
"scripts": {
@@ -28,7 +28,7 @@
2828
"decryption",
2929
"javascript-library"
3030
],
31-
"author": "meeky",
31+
"author": "ajimae",
3232
"license": "MIT",
3333
"bugs": {
3434
"url": "https://github.com/ajimae/ncrypt-js/issues"
@@ -46,7 +46,7 @@
4646
"mocha-lcov-reporter": "^1.3.0",
4747
"nyc": "^14.1.1",
4848
"ts-node": "^8.6.2",
49-
"typescript": "^3.8.3"
49+
"typescript": "3.8.3"
5050
},
5151
"files": [
5252
"dist",

src/ncrypt.d.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)