@mhmdhammoud/meritt-utils 1.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/.eslintignore ADDED
@@ -0,0 +1,2 @@
1
+ *.d.ts
2
+ dist
package/.eslintrc.js ADDED
@@ -0,0 +1,28 @@
1
+ module.exports = {
2
+ plugins: ['@typescript-eslint/eslint-plugin', 'eslint-plugin-tsdoc'],
3
+ extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
4
+ parser: '@typescript-eslint/parser',
5
+ parserOptions: {
6
+ project: './tsconfig.json',
7
+ tsconfigRootDir: __dirname,
8
+ ecmaVersion: 2018,
9
+ sourceType: 'module',
10
+ },
11
+ rules: {
12
+ 'tsdoc/syntax': 'warn',
13
+ '@typescript-eslint/ban-ts-comment': 'off',
14
+ '@typescript-eslint/ban-types': 'off',
15
+ '@typescript-eslint/no-namespace': 'off',
16
+ '@typescript-eslint/no-unused-vars': [
17
+ 'warn',
18
+ {
19
+ vars: 'all',
20
+ args: 'after-used',
21
+ ignoreRestSiblings: true,
22
+ },
23
+ ],
24
+ '@typescript-eslint/no-explicit-any': 'off',
25
+ 'no-async-promise-executor': 'off',
26
+ },
27
+ root: true,
28
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "trailingComma": "es5",
3
+ "tabWidth": 2,
4
+ "semi": false,
5
+ "singleQuote": true,
6
+ "useTabs": true,
7
+ "bracketSpacing": false
8
+ }
package/ISSUES.md ADDED
File without changes
package/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # Meritt Utility functions
2
+
3
+ This is a collection of utility functions that are often used in Meritt projects.
4
+
5
+ ## Installation
6
+
7
+ For npm:
8
+
9
+ ```bash
10
+ npm install @mhmdhammoud/meritt-utils
11
+ ```
12
+
13
+ or for yarn:
14
+
15
+ ```bash
16
+ yarn add @mhmdhammoud/meritt-utils
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```typescript
22
+ import {Crypto} from '@mhmdhammoud/meritt-utils'
23
+
24
+ // Example of getting all Crypto methods
25
+ const encryptedMessage = Crypto.encrypt('Hello World', 7)
26
+ const decryptedMessage = Crypto.decrypt(encryptedMessage, 7)
27
+ ```
@@ -0,0 +1 @@
1
+ export { Crypto } from './lib';
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Crypto = void 0;
4
+ var lib_1 = require("./lib");
5
+ Object.defineProperty(exports, "Crypto", { enumerable: true, get: function () { return lib_1.Crypto; } });
@@ -0,0 +1,68 @@
1
+ /**
2
+ * The Crypto class
3
+ * @class Crypto
4
+ * @example
5
+ * ```typescript
6
+ * import crypto from './lib/crypto'
7
+ * ```
8
+ * */
9
+ declare class Crypto {
10
+ private _primeNumberP;
11
+ private _primeNumberQ;
12
+ private _moduloDenominator;
13
+ /**
14
+ * Set the P prime number for the RSA algorithm
15
+ * @param {string} primeP - The prime number P
16
+ * @memberof Crypto
17
+ * @example
18
+ * ```typescript
19
+ * crypto.setPrimeP(17)
20
+ * ```
21
+ * */
22
+ setPrimeP(primeP: number): void;
23
+ /**
24
+ * Set the Q prime number for the RSA algorithm
25
+ * @param {string} primeQ - The prime number Q
26
+ * @memberof Crypto
27
+ * @example
28
+ * ```typescript
29
+ * crypto.setPrimeQ(19)
30
+ * ```
31
+ * */
32
+ setPrimeQ(primeQ: number): void;
33
+ /**
34
+ *
35
+ * @param {string} base - The base number
36
+ * @param {string} exponent - The exponent number
37
+ * @memberof Crypto
38
+ * @example
39
+ * ```typescript
40
+ * this.modularExponentiation(2, 3)
41
+ * ```
42
+ * */
43
+ private _modularExponentiation;
44
+ /**
45
+ * Encrypt the message using the public key
46
+ * @param {string} message - The message to encrypt
47
+ * @param {string} publicKey - The public key
48
+ * @memberof Crypto
49
+ * @example
50
+ * ```typescript
51
+ * crypto.encrypt('Hello World', 7)
52
+ * ```
53
+ * */
54
+ encrypt: (message: string, publicKey: number) => number[];
55
+ /**
56
+ * Decrypt the message using the private key
57
+ * @param {string} encryptedMessage - The ciphertext to decrypt
58
+ * @param {string} privateKey - The private key
59
+ * @memberof Crypto
60
+ * @example
61
+ * ```typescript
62
+ * crypto.decrypt([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100], 23)
63
+ * ```
64
+ * */
65
+ decrypt: (encryptedMessage: number[], privateKey: number) => string;
66
+ }
67
+ declare const crypto: Crypto;
68
+ export default crypto;
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * The Crypto class
5
+ * @class Crypto
6
+ * @example
7
+ * ```typescript
8
+ * import crypto from './lib/crypto'
9
+ * ```
10
+ * */
11
+ class Crypto {
12
+ constructor() {
13
+ // Default variables
14
+ this._primeNumberP = 17;
15
+ this._primeNumberQ = 19;
16
+ this._moduloDenominator = this._primeNumberP * this._primeNumberQ;
17
+ /**
18
+ *
19
+ * @param {string} base - The base number
20
+ * @param {string} exponent - The exponent number
21
+ * @memberof Crypto
22
+ * @example
23
+ * ```typescript
24
+ * this.modularExponentiation(2, 3)
25
+ * ```
26
+ * */
27
+ this._modularExponentiation = (base, exponent) => {
28
+ if (this._moduloDenominator === 1) {
29
+ return 0;
30
+ }
31
+ let result = 1;
32
+ base = base % this._moduloDenominator;
33
+ while (exponent > 0) {
34
+ if (exponent % 2 === 1) {
35
+ result = (result * base) % this._moduloDenominator;
36
+ }
37
+ exponent = Math.floor(exponent / 2);
38
+ base = (base * base) % this._moduloDenominator;
39
+ }
40
+ return result;
41
+ };
42
+ /**
43
+ * Encrypt the message using the public key
44
+ * @param {string} message - The message to encrypt
45
+ * @param {string} publicKey - The public key
46
+ * @memberof Crypto
47
+ * @example
48
+ * ```typescript
49
+ * crypto.encrypt('Hello World', 7)
50
+ * ```
51
+ * */
52
+ this.encrypt = (message, publicKey) => {
53
+ const b = [];
54
+ for (let i = 0; i < message.length; i++) {
55
+ const a = message.charCodeAt(i);
56
+ b.push(this._modularExponentiation(a, publicKey));
57
+ }
58
+ return b;
59
+ };
60
+ /**
61
+ * Decrypt the message using the private key
62
+ * @param {string} encryptedMessage - The ciphertext to decrypt
63
+ * @param {string} privateKey - The private key
64
+ * @memberof Crypto
65
+ * @example
66
+ * ```typescript
67
+ * crypto.decrypt([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100], 23)
68
+ * ```
69
+ * */
70
+ this.decrypt = (encryptedMessage, privateKey) => {
71
+ const decryptedAsciiMessage = [];
72
+ for (let i = 0; i < encryptedMessage.length; i++) {
73
+ const a = encryptedMessage[i];
74
+ decryptedAsciiMessage.push(this._modularExponentiation(a, privateKey));
75
+ }
76
+ let finalString = '';
77
+ for (let i = 0; i < decryptedAsciiMessage.length; i++) {
78
+ finalString += String.fromCharCode(decryptedAsciiMessage[i]);
79
+ }
80
+ return finalString;
81
+ };
82
+ }
83
+ /**
84
+ * Set the P prime number for the RSA algorithm
85
+ * @param {string} primeP - The prime number P
86
+ * @memberof Crypto
87
+ * @example
88
+ * ```typescript
89
+ * crypto.setPrimeP(17)
90
+ * ```
91
+ * */
92
+ setPrimeP(primeP) {
93
+ let isPrime = true;
94
+ for (let i = 2; i < primeP; i++) {
95
+ if (primeP % i === 0) {
96
+ isPrime = false;
97
+ break;
98
+ }
99
+ }
100
+ if (!isPrime) {
101
+ throw new Error('The number is not prime');
102
+ }
103
+ this._primeNumberP = primeP;
104
+ }
105
+ /**
106
+ * Set the Q prime number for the RSA algorithm
107
+ * @param {string} primeQ - The prime number Q
108
+ * @memberof Crypto
109
+ * @example
110
+ * ```typescript
111
+ * crypto.setPrimeQ(19)
112
+ * ```
113
+ * */
114
+ setPrimeQ(primeQ) {
115
+ let isPrime = true;
116
+ for (let i = 2; i < primeQ; i++) {
117
+ if (primeQ % i === 0) {
118
+ isPrime = false;
119
+ break;
120
+ }
121
+ }
122
+ if (!isPrime) {
123
+ throw new Error('The number is not prime');
124
+ }
125
+ this._primeNumberQ = primeQ;
126
+ }
127
+ }
128
+ const crypto = new Crypto();
129
+ exports.default = crypto;
@@ -0,0 +1 @@
1
+ export { default as Crypto } from './cypto';
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Crypto = void 0;
7
+ var cypto_1 = require("./cypto");
8
+ Object.defineProperty(exports, "Crypto", { enumerable: true, get: function () { return __importDefault(cypto_1).default; } });
@@ -0,0 +1,8 @@
1
+ export declare namespace Crypto {
2
+ class Crypto {
3
+ setPrimeP(primeP: number): void;
4
+ setPrimeQ(primeQ: number): void;
5
+ encrypt: (message: string, publicKey: number) => number[];
6
+ decrypt: (encryptedMessage: number[], privateKey: number) => string;
7
+ }
8
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@mhmdhammoud/meritt-utils",
3
+ "version": "1.0.1",
4
+ "description": "",
5
+ "main": "./dist/index.js",
6
+ "private":false,
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git@github.com:Mhmdhammoud/meritt-utils.git"
10
+ },
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "scripts": {
15
+ "dev-ts": "ts-node-dev --respawn --transpile-only src/index.ts",
16
+ "dev": "nodemon ./dist/index.js",
17
+ "watch": "tsc --watch",
18
+ "build": "tsc",
19
+ "types": "tsc --noemit",
20
+ "prepare": "husky install",
21
+ "lint": "eslint src --fix",
22
+ "prepublish": "yarn build",
23
+ "postinstall": "yarn prepare"
24
+ },
25
+ "dependencies": {
26
+ "dotenv": "^10.0.0"
27
+ },
28
+ "devDependencies": {
29
+ "ts-node-dev": "^1.1.8",
30
+ "typescript": "^4.6.3",
31
+ "@types/jest": "^27.5.1",
32
+ "@typescript-eslint/eslint-plugin": "^5.17.0",
33
+ "@typescript-eslint/parser": "^5.17.0",
34
+ "eslint": "^8.12.0",
35
+ "eslint-plugin-tsdoc": "^0.2.14",
36
+ "husky": "^8.0.0"
37
+ },
38
+ "author": "Mhmdhammoud",
39
+ "license": "ISC"
40
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export {Crypto} from './lib'
@@ -0,0 +1,130 @@
1
+ /**
2
+ * The Crypto class
3
+ * @class Crypto
4
+ * @example
5
+ * ```typescript
6
+ * import crypto from './lib/crypto'
7
+ * ```
8
+ * */
9
+ class Crypto {
10
+ // Default variables
11
+ private _primeNumberP = 17
12
+ private _primeNumberQ = 19
13
+ private _moduloDenominator = this._primeNumberP * this._primeNumberQ
14
+
15
+ /**
16
+ * Set the P prime number for the RSA algorithm
17
+ * @param {string} primeP - The prime number P
18
+ * @memberof Crypto
19
+ * @example
20
+ * ```typescript
21
+ * crypto.setPrimeP(17)
22
+ * ```
23
+ * */
24
+ public setPrimeP(primeP: number) {
25
+ let isPrime = true
26
+ for (let i = 2; i < primeP; i++) {
27
+ if (primeP % i === 0) {
28
+ isPrime = false
29
+ break
30
+ }
31
+ }
32
+ if (!isPrime) {
33
+ throw new Error('The number is not prime')
34
+ }
35
+ this._primeNumberP = primeP
36
+ }
37
+
38
+ /**
39
+ * Set the Q prime number for the RSA algorithm
40
+ * @param {string} primeQ - The prime number Q
41
+ * @memberof Crypto
42
+ * @example
43
+ * ```typescript
44
+ * crypto.setPrimeQ(19)
45
+ * ```
46
+ * */
47
+ public setPrimeQ(primeQ: number) {
48
+ let isPrime = true
49
+ for (let i = 2; i < primeQ; i++) {
50
+ if (primeQ % i === 0) {
51
+ isPrime = false
52
+ break
53
+ }
54
+ }
55
+ if (!isPrime) {
56
+ throw new Error('The number is not prime')
57
+ }
58
+ this._primeNumberQ = primeQ
59
+ }
60
+
61
+ /**
62
+ *
63
+ * @param {string} base - The base number
64
+ * @param {string} exponent - The exponent number
65
+ * @memberof Crypto
66
+ * @example
67
+ * ```typescript
68
+ * this.modularExponentiation(2, 3)
69
+ * ```
70
+ * */
71
+ private _modularExponentiation = (base: number, exponent: number) => {
72
+ if (this._moduloDenominator === 1) {
73
+ return 0
74
+ }
75
+ let result = 1
76
+ base = base % this._moduloDenominator
77
+ while (exponent > 0) {
78
+ if (exponent % 2 === 1) {
79
+ result = (result * base) % this._moduloDenominator
80
+ }
81
+ exponent = Math.floor(exponent / 2)
82
+ base = (base * base) % this._moduloDenominator
83
+ }
84
+ return result
85
+ }
86
+
87
+ /**
88
+ * Encrypt the message using the public key
89
+ * @param {string} message - The message to encrypt
90
+ * @param {string} publicKey - The public key
91
+ * @memberof Crypto
92
+ * @example
93
+ * ```typescript
94
+ * crypto.encrypt('Hello World', 7)
95
+ * ```
96
+ * */
97
+ public encrypt = (message: string, publicKey: number): number[] => {
98
+ const b: number[] = []
99
+ for (let i = 0; i < message.length; i++) {
100
+ const a = message.charCodeAt(i)
101
+ b.push(this._modularExponentiation(a, publicKey))
102
+ }
103
+ return b
104
+ }
105
+
106
+ /**
107
+ * Decrypt the message using the private key
108
+ * @param {string} encryptedMessage - The ciphertext to decrypt
109
+ * @param {string} privateKey - The private key
110
+ * @memberof Crypto
111
+ * @example
112
+ * ```typescript
113
+ * crypto.decrypt([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100], 23)
114
+ * ```
115
+ * */
116
+ public decrypt = (encryptedMessage: number[], privateKey: number): string => {
117
+ const decryptedAsciiMessage: number[] = []
118
+ for (let i = 0; i < encryptedMessage.length; i++) {
119
+ const a = encryptedMessage[i]
120
+ decryptedAsciiMessage.push(this._modularExponentiation(a, privateKey))
121
+ }
122
+ let finalString = ''
123
+ for (let i = 0; i < decryptedAsciiMessage.length; i++) {
124
+ finalString += String.fromCharCode(decryptedAsciiMessage[i])
125
+ }
126
+ return finalString
127
+ }
128
+ }
129
+ const crypto = new Crypto()
130
+ export default crypto
@@ -0,0 +1 @@
1
+ export {default as Crypto} from './cypto'
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2018",
4
+ "module": "commonjs",
5
+ "lib": ["es2018", "esnext.asynciterable", "DOM"],
6
+ "resolveJsonModule": false,
7
+ "declaration": true,
8
+ "outDir": "./dist",
9
+ "rootDir": "./src",
10
+ "strict": true,
11
+ "strictPropertyInitialization": false,
12
+ "esModuleInterop": true,
13
+ "skipLibCheck": true,
14
+ "forceConsistentCasingInFileNames": true
15
+ }
16
+ }