diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
new file mode 100644
index 0000000..3ebc903
--- /dev/null
+++ b/.github/workflows/ci.yml
@@ -0,0 +1,18 @@
+name: Test
+on:
+ push:
+ branches: [ master ]
+ pull_request:
+ branches: [ master ]
+
+jobs:
+ build:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v3
+ - uses: actions/setup-node@v3
+ with:
+ node-version: 12
+ cache: 'yarn'
+ - run: yarn install --production=false
+ - run: yarn test
\ No newline at end of file
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 5ea0d00..b0eb8cf 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -17,7 +17,7 @@ jobs:
- name: node setup
uses: actions/setup-node@v4
with:
- node-version: 14
+ node-version: 12
cache: 'yarn'
- name: install dependencies
@@ -39,7 +39,7 @@ jobs:
- name: node setup
uses: actions/setup-node@v4
with:
- node-version: 14
+ node-version: 12
registry-url: https://registry.npmjs.org
cache: 'yarn'
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index 94e08af..0000000
--- a/.travis.yml
+++ /dev/null
@@ -1,11 +0,0 @@
-language: node_js
-node_js:
- - "node"
-cache:
- directories:
- - "node_modules"
-script:
- - npm test
-after_success:
- - npm run coverage
- - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
diff --git a/README.md b/README.md
index 2f4cfe4..cfa9b92 100644
--- a/README.md
+++ b/README.md
@@ -6,25 +6,17 @@
**_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.
-
-
## Contents
* [NcryptJs](#NcryptJs)
* [Contents](#contents)
-
* [Getting Started](#getting-started)
* [Installation](#installation)
* [Documentation](#documentation)
- * [NcryptJs Functions](#ncryptjs-functions)
- * [Using `encrypt()` and `decrypt()`](#using-encrypt-and-decrypt)
- * [Using default imports](#Using-default-imports)
-
+ * [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)
* [Built With](#built-with)
* [Contribution](#contribution)
* [Version Management](#version-management)
@@ -82,23 +74,38 @@ However, if you are using ECMAScript 5 and older, use the require statement:
**_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).
-### NcryptJs Functions
+### NcryptJs Methods
-### List of **_NcryptJs_** functions.
+### List of **_NcryptJs_** Methods.
-| Functions | Description | Parameters | Return |
+| Methods | Description | Parameters | Return |
| ------------------------------------------------------------- | -------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
+| [_static_] **randomString()** | Random String. |**size**: _number_ - An optional size of the generated `randomBytes`. **enc:** _base64/hex_ - Encoding used for encoding the `randomBytes` defaults to _`base64`_ |**encoded**: _string_ - encoded string. |
| **encrypt()** | Encrypts data. |**data**: _object/string/number/boolean_ - The data to be encrypted. |**ciphered**: _string_ - encrypted data. |
| **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).
+### Using randomString method
+
+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 randomStr = ncrypt.randomString(8, 'base64');
+console.log(randomStr) // t78WcmYAFOY=
-#### 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.
+// signature
+ncrypt.randomString(size?: number, enc?: 'base64' | 'hex');
+```
-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.
+### 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.
+
+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");
@@ -108,7 +115,7 @@ To encrypt and decrypt data, simply use `encrypt()` and `decrypt()` functions re
var data = "Hello World!";
var _secretKey = "some-super-secret-key";
-+ var { encodeData, decodeData } = new ncrypt(_secretKey);
++ var { encrypt, decrypt } = new ncrypt(_secretKey);
// encrypting super sensitive data here
- var encryptedData = encrypt(data, _secretKey);
@@ -180,11 +187,16 @@ 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.
-```diff
-// .env
+```bash
+# .env
+
+# used internally to set the `key`
+KEY='sshhhh this is a super secret key'
+
+# used internally to set the `encoding` - ['base64' | 'binary' | 'hex' | 'ucs-2' | 'ucs2' | 'utf16le']
+NCRPT_ENC='hex'
-KEY=sshhhh this is a super secret key
-SECRET=this is our hashing secret
+SECRET='this is our hashing secret'
```
Then when creating your object, you can use the SECRET from your environment e.g:
```
diff --git a/package.json b/package.json
index c1974b1..52a006d 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "ncrypt-js",
- "version": "2.0.1",
+ "version": "2.1.0",
"description": "a light weight javascript data encryption and decryption library",
"main": "dist/index.js",
"scripts": {
@@ -28,7 +28,7 @@
"decryption",
"javascript-library"
],
- "author": "meeky",
+ "author": "ajimae",
"license": "MIT",
"bugs": {
"url": "/~https://github.com/ajimae/ncrypt-js/issues"
@@ -46,7 +46,7 @@
"mocha-lcov-reporter": "^1.3.0",
"nyc": "^14.1.1",
"ts-node": "^8.6.2",
- "typescript": "^3.8.3"
+ "typescript": "3.8.3"
},
"files": [
"dist",
diff --git a/src/ncrypt.d.ts b/src/ncrypt.d.ts
deleted file mode 100644
index 7829c04..0000000
--- a/src/ncrypt.d.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-
-export interface INcrypt {
- convertTextToDecimal(text: string): number[];
- applySecretToCharacters(charCodes: number[] | number): number;
- convertByteToHexadecimal(number: number): string;
- encrypt(text: object | string | number | boolean): string;
- decrypt(data: string): string;
-}
diff --git a/src/ncrypt.ts b/src/ncrypt.ts
index c67714e..f45146d 100644
--- a/src/ncrypt.ts
+++ b/src/ncrypt.ts
@@ -1,64 +1,147 @@
-import { INcrypt } from './ncrypt.d';
-import { encode, decode } from './utils';
+import * as crypto from 'crypto';
-export default class Ncrypt implements INcrypt {
+type TNCRYPT_ENC = 'base64' | 'binary' | 'hex' | 'ucs-2' | 'ucs2' | 'utf16le';
+/**
+ * @class Ncrypt
+ * @type {Ncrypt.