ncrypt-js 1.1.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # NcryptJs
2
2
 
3
- [![Build Status](https://travis-ci.com/ajimae/ncrypt-js.svg?branch=master)](https://travis-ci.com/ajimae/ncrypt-js) [![Coverage Status](https://coveralls.io/repos/github/ajimae/ncrypt-js/badge.svg)](https://coveralls.io/github/ajimae/ncrypt-js)
3
+ [![Build Status](https://travis-ci.com/ajimae/ncrypt-js.svg?branch=master)](https://travis-ci.com/ajimae/ncrypt-js) [![Coverage Status](https://coveralls.io/repos/github/ajimae/ncrypt-js/badge.svg)](https://coveralls.io/github/ajimae/ncrypt-js) [![NPM](https://img.shields.io/npm/l/ncrypt-js)](https://www.npmjs.com/package/ncrypt-js/v/2.0.0#license)
4
+
5
+ [![GitHub release (latest by date)](https://img.shields.io/github/v/release/ajimae/ncrypt-js)](https://github.com/ajimae/ncrypt-js/releases) [![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/ajimae/ncrypt-js)](https://github/languages/code-size/ajimae/ncrypt-js) [![GitHub issues](https://img.shields.io/github/issues/ajimae/ncrypt-js)](https://github.com/ajimae/ncrypt-js/issues)
4
6
 
5
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.
6
8
 
@@ -94,7 +96,7 @@ However, if you are using ECMAScript 5 and older, use the require statement:
94
96
 
95
97
 
96
98
 
97
- ### Using `encrypt()` and `decrypt()` functons - As of version 1.1.0 this is deprecated, an object must be created first.
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.
98
100
 
99
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.
100
102
 
@@ -175,6 +177,21 @@ var decryptedObject = ncryptObject.decrypt(encryptedObject);
175
177
  console.log("... and then decryption...");
176
178
  console.log("Decipher Text : " + decryptedObject);
177
179
  console.log("...done.");
180
+ ````
181
+ 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
+
183
+ ```diff
184
+ // .env
185
+
186
+ KEY=sshhhh this is a super secret key
187
+ SECRET=this is our hashing secret
188
+ ```
189
+ Then when creating your object, you can use the SECRET from your environment e.g:
190
+ ```
191
+ ...
192
+ var ncrypt = require('ncrypt-js');
193
+ var { encrypt, decrypt } = new ncrypt(process.env.SECRET);
194
+ ...
178
195
  ```
179
196
 
180
197
  ## Built With
package/dist/index.js CHANGED
@@ -3,8 +3,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ncrypt = void 0;
6
7
  const ncrypt_1 = __importDefault(require("./src/ncrypt"));
7
8
  exports.ncrypt = ncrypt_1.default;
8
9
  module.exports = ncrypt_1.default;
9
10
  exports.default = ncrypt_1.default;
10
- //# sourceMappingURL=index.js.map
@@ -73,4 +73,3 @@ class Ncrypt {
73
73
  }
74
74
  }
75
75
  exports.default = Ncrypt;
76
- //# sourceMappingURL=ncrypt.js.map
package/dist/src/utils.js CHANGED
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.decode = exports.encode = void 0;
6
7
  const crypto_1 = __importDefault(require("crypto")); // this is necessary for some version of nodejs without crypto module
7
8
  const algorithm = 'aes-256-cbc';
8
9
  /**
@@ -46,4 +47,3 @@ exports.decode = (text) => {
46
47
  decrypted = Buffer.concat([decrypted, decipher.final()]);
47
48
  return decrypted.toString();
48
49
  };
49
- //# sourceMappingURL=utils.js.map
package/package.json CHANGED
@@ -1,14 +1,12 @@
1
1
  {
2
2
  "name": "ncrypt-js",
3
- "version": "1.1.0",
3
+ "version": "2.0.1",
4
4
  "description": "a light weight javascript data encryption and decryption library",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
- "start": "npm run build && node dist/index",
8
- "start:dev": "ts-node index.ts",
9
7
  "clean": "rm -rf dist",
10
- "prepare": "npm run build",
11
- "build": "tsc --outDir dist",
8
+ "prepare": "yarn build",
9
+ "build": "yarn clean;tsc --outDir dist",
12
10
  "test": "cross-env TS_NODE_FILES=true nyc mocha --exit --require ts-node/register --colors test/**/*.ts",
13
11
  "coverage": "nyc report --reporter=text-lcov | coveralls"
14
12
  },
@@ -50,10 +48,14 @@
50
48
  "ts-node": "^8.6.2",
51
49
  "typescript": "^3.8.3"
52
50
  },
51
+ "files": [
52
+ "dist",
53
+ "package.json",
54
+ "LICENSE",
55
+ "tsconfig.json"
56
+ ],
53
57
  "dependencies": {
54
58
  "crypto": "^1.0.1",
55
- "dotenv": "^8.2.0",
56
- "handlebars": "^4.7.3",
57
- "simple-crypto-js": "^2.2.0"
59
+ "dotenv": "^16.4.5"
58
60
  }
59
61
  }
package/tsconfig.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "esModuleInterop": true,
7
7
  "noImplicitAny": true,
8
8
  "moduleResolution": "node",
9
- "sourceMap": true,
9
+ "sourceMap": false,
10
10
  "outDir": "dist",
11
11
  "baseUrl": ".",
12
12
  "paths": {
package/.nycrc DELETED
@@ -1,29 +0,0 @@
1
- {
2
- "cache": false,
3
- "check-coverage": false,
4
- "extension": [
5
- ".ts"
6
- ],
7
- "require": [
8
- "ts-node/register"
9
- ],
10
- "include": [
11
- "**/*js",
12
- "**/*ts"
13
- ],
14
- "exclude": [
15
- "dist/**",
16
- "coverage/**",
17
- "node_modules/**",
18
- "**/*.d.ts",
19
- "**/*.test.ts"
20
- ],
21
- "sourceMap": true,
22
- "reporter": [
23
- "html",
24
- "text",
25
- "text-summary"
26
- ],
27
- "all": true,
28
- "instrument": true
29
- }
package/.travis.yml DELETED
@@ -1,11 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - "node"
4
- cache:
5
- directories:
6
- - "node_modules"
7
- script:
8
- - npm test
9
- after_success:
10
- - npm run coverage
11
- - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;AAAA,0DAAkC;AAKhC,iBALK,gBAAM,CAKL;AAHR,MAAM,CAAC,OAAO,GAAG,gBAAM,CAAC;AACxB,kBAAe,gBAAM,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"ncrypt.js","sourceRoot":"","sources":["../../src/ncrypt.ts"],"names":[],"mappings":";;AACA,mCAAyC;AAEzC,MAAqB,MAAM;IAazB;;;;OAIG;IACH,YAAY,MAAc;QAI1B;;;;WAIG;QACH,yBAAoB,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;QAE5F;;;WAGG;QACH,4BAAuB,GAAG,CAAC,SAAqC,EAAE,EAAE,CAAC,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,MAAM,CAAC;aACxG,MAAM,CAAC,CAAC,UAAe,EAAE,WAAgB,EAAE,EAAE,CAAC,CAAC,UAAU,GAAG,WAAW,CAAC,EAAE,SAAS,CAAC,CAAA;QAEvF;;;;WAIG;QACH,6BAAwB,GAAG,CAAC,MAAc,EAAE,EAAE;YAC5C,OAAO,CAAC,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;QACx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AM,CAAC,IAAI,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;IACjC,IAAI,aAAa,GAAG,MAAM,CAAC,IAAI,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC;IACtD,IAAI,QAAQ,GAAG,gBAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC;IACzE,IAAI,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;IAE/C,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IACzD,OAAO,SAAS,CAAC,QAAQ,EAAE,CAAC;AAC9B,CAAC,CAAA"}
package/index.ts DELETED
@@ -1,7 +0,0 @@
1
- import ncrypt from './src/ncrypt';
2
-
3
- module.exports = ncrypt;
4
- export default ncrypt;
5
- export {
6
- ncrypt
7
- }
package/src/ncrypt.d.ts DELETED
@@ -1,8 +0,0 @@
1
-
2
- export interface INcrypt {
3
- convertTextToDecimal(text: string): number[];
4
- applySecretToCharacters(charCodes: number[] | number): number;
5
- convertByteToHexadecimal(number: number): string;
6
- encrypt(text: object | string | number | boolean): string;
7
- decrypt(data: string): string;
8
- }
package/src/ncrypt.ts DELETED
@@ -1,94 +0,0 @@
1
- import { INcrypt } from './ncrypt.d';
2
- import { encode, decode } from './utils';
3
-
4
- export default class Ncrypt implements INcrypt {
5
-
6
- /**
7
- * ncrypt namespace.
8
- * @type {string.<*>}
9
- */
10
- private secret: string;
11
-
12
- /**
13
- * ncrypt namespace.
14
- * @type {string.<*>}
15
- */
16
- private text: string;
17
- /**
18
- * object constructor
19
- * @param text
20
- * @param secret
21
- */
22
- constructor(secret: string) {
23
- this.secret = secret;
24
- }
25
-
26
- /**
27
- * convert all entered text to decimal equivalent character codes
28
- * @param {data.<string>} data to be converted
29
- * @return {Array.<number>} array of character codes
30
- */
31
- convertTextToDecimal = (data: string) => data.split('').map((value) => value.charCodeAt(0));
32
-
33
- /**
34
- * encode provided secret on decimal character codes
35
- * @param {charCode<number[], *>} character codes
36
- */
37
- applySecretToCharacters = (charCodes: number[] | number | string) => this.convertTextToDecimal(this.secret)
38
- .reduce((firstValue: any, secondValue: any) => (firstValue ^ secondValue), charCodes)
39
-
40
- /**
41
- * convert character bytes to hexadecimal equivalent
42
- * @param {number.<number>}
43
- * @returns {string} hexadecimal string
44
- */
45
- convertByteToHexadecimal = (number: number) => {
46
- return ("0" + Number(number).toString(16)).substr(-2);
47
- }
48
-
49
- /**
50
- * process data to be encrypted
51
- * @param {}
52
- * @returns {string.<string>} encoded string data
53
- */
54
- encrypt = (data: object | string | number | boolean) => {
55
- /**
56
- * this does the actual processing return a string
57
- * resulting from charCode conversion, salting and
58
- * hexadecimal mapping
59
- *
60
- */
61
- // if (data == void 0) throw new Error('invalid data was entered, enter data of type object, number, string or boolean to be encrypted.');
62
- try {
63
- const encodedMessage = JSON.stringify(data).split('')
64
- .map(this.convertTextToDecimal)
65
- .map(this.applySecretToCharacters)
66
- .map(this.convertByteToHexadecimal)
67
- .join('');
68
-
69
- return encode(encodedMessage);
70
- } catch (error) {
71
- throw new Error('invalid data was entered, enter data of type object, number, string or boolean to be encrypted.');
72
- }
73
- }
74
-
75
- /**
76
- * decodes encoded string resulting from util encryption
77
- * @param {string.<stirng>} encodeData
78
- * @returns {decodedData.<string>} decoded data
79
- */
80
- decrypt = (text: string) => {
81
- const encodeData = decode(text);
82
-
83
- const data = encodeData.match(/.{1,2}/g)
84
- .map((hex: any) => parseInt(hex, 16))
85
- .map(this.applySecretToCharacters)
86
- .map((charCode: any) => String.fromCharCode(charCode))
87
- .join('');
88
-
89
- const arr = [];
90
- arr.push(data);
91
-
92
- return JSON.parse(data);
93
- }
94
- }
package/src/utils.ts DELETED
@@ -1,53 +0,0 @@
1
- import crypto from 'crypto'; // this is necessary for some version of nodejs without crypto module
2
-
3
- const algorithm = 'aes-256-cbc';
4
-
5
- /**
6
- * crypto random initial vector generated from core node {crypto} module
7
- */
8
- const initialVector: Buffer = crypto.randomBytes(16);
9
-
10
- /**
11
- * crypto random key generated from core node {crypto} module
12
- *
13
- * {note}: please read the value for KEY from your app's environment
14
- */
15
- const _key = process.env.KEY || 'please provide a KEY in your .env file or config';
16
- const key: Buffer = crypto.scryptSync(_key, 'salt', 32);
17
-
18
- /**
19
- * intermediate data encoder function
20
- * @param {string.<any>} text
21
- * @param secret
22
- * @returns {string} encrypted or cipher text
23
- */
24
- export const encode = (text: string) => {
25
- let cipher = crypto.createCipheriv(algorithm, Buffer.from(key), initialVector);
26
- let encrypted = cipher.update(text);
27
-
28
- encrypted = Buffer.concat([encrypted, cipher.final()]);
29
- return `${initialVector.toString('hex')}.${encrypted.toString('hex')}`;
30
- }
31
-
32
- /**
33
- * intermediate data decoder function
34
- * @param {string.<any>} text
35
- * @returns {string.<string>} decrypted data
36
- */
37
- export const decode = (text: string) => {
38
-
39
- if (typeof text !== 'string') {
40
- throw new TypeError('argument must be a string, or a string-like object');
41
- }
42
-
43
- const iv = text.split('.')[0];
44
- const encryptedData = text.split('.')[1];
45
-
46
- let _iv = Buffer.from(iv, 'hex');
47
- let encryptedText = Buffer.from(encryptedData, 'hex');
48
- let decipher = crypto.createDecipheriv(algorithm, Buffer.from(key), _iv);
49
- let decrypted = decipher.update(encryptedText);
50
-
51
- decrypted = Buffer.concat([decrypted, decipher.final()]);
52
- return decrypted.toString();
53
- }
package/src/utils.ts.BAK DELETED
@@ -1,96 +0,0 @@
1
- import crypto from 'crypto';
2
- import Ncrypt from './ncrypt';
3
- import { config } from 'dotenv';
4
-
5
- config();
6
-
7
- /**
8
- * crypto random initial vector generated from core node {crypto} module
9
- */
10
- const initialVector: Buffer = crypto.randomBytes(16);
11
-
12
- /**
13
- * crypto random key generated from core node {crypto} module
14
- */
15
- const _key = process.env.KEY || 'please provide a KEY in your .env file or config';
16
- const key: Buffer = crypto.scryptSync(_key, 'salt', 32);
17
-
18
- /**
19
- * intermediate data encoder function
20
- * @param string
21
- * @param secret
22
- * @returns {object} encrypted, cipher
23
- */
24
- const encoder = (string: string, secret: string) => {
25
- const encryptObject: Ncrypt = new Ncrypt(string, secret);
26
- const encoded: string = encryptObject.encodeData();
27
- const cipher: crypto.Cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), initialVector);
28
- let encrypted: Buffer = cipher.update(encoded);
29
- return { encrypted, cipher };
30
- }
31
-
32
- /**
33
- * intermediate data decoder function
34
- * @param string
35
- * @param secret
36
- * @returns {object} encrypted, cipher
37
- * @throws {Error} Error
38
- */
39
- const decoder = (decryptionInitialVector: string, encodedText: string) => {
40
- try {
41
- const _iv: Buffer = Buffer.from(decryptionInitialVector, 'hex');
42
- const encryptedText: Buffer = Buffer.from(encodedText, 'hex');
43
- const decipher: crypto.Decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(key), _iv);
44
- let decrypted: Buffer = decipher.update(encryptedText);
45
- return { decrypted, decipher };
46
- } catch (error) {
47
- throw new Error('argument must be a string, or a string-like object');
48
- }
49
- }
50
-
51
- /**
52
- *
53
- * @param {text.<object, string, *>} text
54
- * @param {secret.<string>} secret
55
- * @returns {data.<string>} crypto-cipher encrypted data and concatenated initial vector string
56
- */
57
- const encrypt = (data: object | string | number | boolean, secret: string = process.env.SECRET) => {
58
- if (data === null) throw new Error('no data was entered, enter data of type object, number, string or boolean to be encrypted.');
59
- const string: string = JSON.stringify(data);
60
- if (typeof secret !== 'string') throw new Error('your .env or env config file is missing a SECRET key.');
61
-
62
- /**
63
- * ncrypt constructor with initial data and secret {secret}
64
- */
65
- let { encrypted, cipher }: { encrypted: Buffer; cipher: crypto.Cipher } = encoder(string, secret);
66
- encrypted = Buffer.concat([encrypted, cipher.final()]);
67
-
68
- /**
69
- * concatenated data and vector string
70
- */
71
- return `${initialVector.toString('hex')}.${encrypted.toString('hex')}`;
72
- }
73
-
74
- /**
75
- * decrypt crypto-ciphered data and vector
76
- * @param {encodedData.<any>} encodedData
77
- * @returns {string.<string>} decrypted data
78
- */
79
- const decrypt = (encodedData: string) => {
80
- if (typeof encodedData !== 'string') {
81
- throw new TypeError('argument must be a string, or a string-like object');
82
- }
83
- const encryptObject: Ncrypt = new Ncrypt(encodedData, process.env.SECRET);
84
- const decryptionInitialVector = encodedData.split('.')[0];
85
- const encodedText = encodedData.split('.')[1];
86
-
87
- let { decrypted, decipher }: { decrypted: Buffer; decipher: crypto.Decipher; } = decoder(decryptionInitialVector, encodedText);
88
- decrypted = Buffer.concat([decrypted, decipher.final()]);
89
-
90
- /**
91
- * JSON parsed decrypted text buffer
92
- */
93
- return JSON.parse(encryptObject.decodeData(decrypted.toString()));
94
- }
95
-
96
- export { encrypt, decrypt };
@@ -1,135 +0,0 @@
1
- import * as chai from "chai";
2
-
3
- import ncrypt from '../index';
4
-
5
- const expect = chai.expect;
6
-
7
- const object = {
8
- NcryptJs: "is great.",
9
- You: "should try it!"
10
- };
11
- const string: string = "ncrypt-js is great.";
12
- const number: number = 19960404;
13
- const boolean: boolean = false;
14
- const _nullData: any = null;
15
-
16
- const _secret = 'shhh its a secret';
17
-
18
- const { encrypt, decrypt } = new ncrypt(_secret);
19
-
20
- const encryptString = encrypt(string);
21
- const encryptNumber = encrypt(number);
22
- const encryptObject = encrypt(object);
23
- const encryptBoolean = encrypt(boolean);
24
- const encryptNullData = encrypt(_nullData);
25
-
26
- const decryptString = decrypt(encryptString);
27
- const decryptNumber = decrypt(encryptNumber);
28
- const decryptObject = decrypt(encryptObject);
29
- const decryptBoolean = decrypt(encryptBoolean);
30
- const decryptNullData = encrypt(_nullData);
31
-
32
- describe('Encrytion', () => {
33
- it('should be able to encrypt a string', () => {
34
- expect(string).to.be.a('string');
35
- expect(typeof encryptString).to.eql('string');
36
- });
37
-
38
- it('should be able to encrypt an object', () => {
39
- expect(object).to.be.a('object');
40
- expect(typeof encryptObject).to.eql('string');
41
- });
42
-
43
- it('should be able to encrypt a number', () => {
44
- expect(number).to.be.a('number');
45
- expect(typeof encryptNumber).to.eql('string');
46
- });
47
-
48
- it('should be able to encrypt a boolean', () => {
49
- expect(boolean).to.be.a('boolean');
50
- expect(typeof encryptBoolean).to.eql('string');
51
- });
52
- });
53
-
54
- describe('Decrytion', () => {
55
- it('should be able to decrypt original string', () => {
56
- expect(decryptString).to.be.eql(string);
57
- expect(typeof decryptString).to.eql('string');
58
- });
59
-
60
- it('should be able to decrypt original object', () => {
61
- expect(decryptObject).to.be.eql(object);
62
- expect(typeof decryptObject).to.eql('object');
63
- });
64
-
65
- it('should be able to decrypt original number', () => {
66
- expect(decryptNumber).to.be.eql(number);
67
- expect(typeof decryptNumber).to.eql('number');
68
- });
69
-
70
- it('should be able to decrypt original boolean', () => {
71
- expect(decryptBoolean).to.be.eql(boolean);
72
- expect(typeof decryptBoolean).to.eql('boolean');
73
- });
74
- });
75
-
76
- describe('Error handling and validations', () => {
77
- it('should error when secret is not provided', () => {
78
- try {
79
- encrypt('nullSecret');
80
- } catch (error) {
81
- expect(error.message).equal('must be initialized with a secret key of type string');
82
- }
83
- });
84
-
85
- it('should error when non-string data is passed as decryption string', () => {
86
- try {
87
- const nonStringData = 12345;
88
- //@ts-ignore
89
- decrypt(nonStringData);
90
- } catch (error) {
91
- expect(error.message).equal('argument must be a string, or a string-like object');
92
- }
93
- });
94
-
95
- it('should error when a non string data type is to be decrypted', () => {
96
- try {
97
- const nonStringData: any = void(0);
98
- decrypt(nonStringData);
99
- } catch (error) {
100
- expect(error.message).equal('argument must be a string, or a string-like object');
101
- }
102
- });
103
-
104
- it('should error when a non string data type is to be decrypted', () => {
105
- try {
106
- decrypt(decryptNullData);
107
- } catch (error) {
108
- expect(error.message).equal('argument must be a string, or a string-like object');
109
- }
110
- });
111
-
112
- it('should throw an error when an undefined data is to be encrypted', () => {
113
- try {
114
- encrypt(undefined);
115
- } catch (error) {
116
- expect(error.message).equal('invalid data was entered, enter data of type object, number, string or boolean to be encrypted.');
117
- }
118
- });
119
-
120
- it('should throw an error when an undefined data is to be encrypted', () => {
121
- try {
122
- encrypt(null);
123
- } catch (error) {
124
- expect(error.message).equal('no data was entered, enter data of type object, number, string or boolean to be encrypted.');
125
- }
126
- });
127
-
128
- it('should throw an error when an null data is to be encrypted', () => {
129
- try {
130
- encrypt(encryptNullData);
131
- } catch (error) {
132
- expect(error.message).equal('invalid data was entered, enter data of type object, number, string or boolean to be encrypted.');
133
- }
134
- });
135
- });