-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
300 lines (242 loc) · 10.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
'use strict';
const chalk = require('chalk');
class ObjectEncrypt {
constructor() {}
static createKey(password) {
// console.log(` ~ Initalizing KEY OBJECT...`);
if (!password) {
throw new Error('Password hasnt provided in the params');
}
const encryptionLetters = '12345678901234567890qwertyuiopasdfghjklzxcvbnm'
.toUpperCase()
.split('');
const sectionNameEncryptionLetters = 'qwertyuiopasdfghjklzxcvbnm'
.toUpperCase()
.split('');
const letters = password.split('');
const complexity = 3;
const sectionsLengthComplexity = complexity * 3;
const sectionNameComplexity = complexity * 4;
let createdSections = [];
for (let i = 0; i < sectionsLengthComplexity; i++) {
let sectionNameEncryption = '';
for (let j = 0; j < sectionNameComplexity; j++) {
const randomSectionNameEncryptionIndex = Math.floor(
Math.random() * sectionNameEncryptionLetters.length
);
sectionNameEncryption +=
sectionNameEncryptionLetters[randomSectionNameEncryptionIndex];
}
for (let j = 0; j < sectionNameComplexity; j++) {
while (sectionNameEncryption === createdSections[j]) {
sectionNameEncryption = '';
for (let k = 0; k < sectionNameComplexity; k++) {
const randomSectionNameEncryptionIndex = Math.floor(
Math.random() * sectionNameEncryptionLetters.length
);
sectionNameEncryption +=
sectionNameEncryptionLetters[randomSectionNameEncryptionIndex];
}
}
}
createdSections.push(sectionNameEncryption);
}
const sections = [...createdSections];
createdSections = undefined;
const keyObject = {};
if (complexity <= 2 || complexity >= 5) {
for (let i = 0; i < letters.length; i++) {
keyObject[letters[i]] = {};
for (let j = 0; j < sections.length; j++) {
keyObject[letters[i]][sections[j]] = [];
}
}
console.error(
chalk.red(
' ! Complexity argument of the createKey cannot be belov 2 or above 4'
)
);
console.log(' ~ Creating an empty Key Object...');
console.log(
chalk.yellow(
' ! Recommended complexity number is 4 or 3, default complexity number is set to 3'
)
);
throw new Error(
' ! Recommended complexity number is 4 or 3, default complexity number is set to 3'
);
}
for (let i = 0; i < letters.length; i++) {
const letter = letters[i];
keyObject[letter] = {};
for (let j = 0; j < sections.length; j++) {
// j represents the section configuration, configure a specific section in the belove code.
const section = sections[j];
keyObject[letter][section] = [];
for (let k = 0; k < complexity * 2; k++) {
// k represents how many elements should a section have, example: sectionName: ['SDF', 'SDF', 'SDFD'];
let encryption = '';
for (let l = 0; l < complexity * 2; l++) {
const randomEncryptionLetter = Math.floor(
Math.random() * encryptionLetters.length
);
encryption = encryption + encryptionLetters[randomEncryptionLetter];
}
// in here we are iterating through all the sections index in all the properties of the object to avoid the same encryption in the object properties, doesnt matter if we get same encryption in the same properties array but we change it due to syntax
// but if we get same encryption in different properties of the representationObject password will be decrypted wrongly.
// because it will take the first property letter of the representaionObject when it is matched with the encryptedPassword so
// all the properties has to have unique encryptions in it.
if (keyObject[letter] && keyObject[letter][section]) {
const keyObjectProperties = Object.getOwnPropertyNames(keyObject);
for (let m = 0; m < keyObjectProperties.length; m++) {
for (let n = 0; n < sections.length; n++) {
for (
let sectionsIndex = 0;
sectionsIndex < 4;
sectionsIndex++
) {
if (keyObject[letters[m]][sections[n]]) {
while (
encryption ===
keyObject[letters[m]][sections[n]][sectionsIndex]
) {
// development purposes
console.log(
` ~ Same Encryption Found in: `,
chalk.yellow(letters[m])
);
console.log(` ~ Section: `, chalk.yellow(sections[n]));
console.log(
` ~ Array:`,
keyObject[letters[m]][sections[0]],
` Index:`,
chalk.yellow(sectionsIndex)
);
encryption = '';
for (let l = 0; l < complexity * 2; l++) {
const randomEncryptionLetter = Math.floor(
Math.random() * encryptionLetters.length
);
encryption =
encryption +
encryptionLetters[randomEncryptionLetter];
}
}
}
}
}
}
}
keyObject[letter][section] =
keyObject[letter][section].concat(encryption);
encryption = '';
}
}
}
// shuffle the properties of the keyObject to prevent the sequences of UTF-16 code units values attacks
// efffsane oldu lul
const shuffledKeyObjectProperties = Object.getOwnPropertyNames(
keyObject
).sort(() => Math.random() - 0.5);
const shuffledKeyObject = {};
// bind the properties of the keyObject to shuffledKeyObject
shuffledKeyObjectProperties.forEach((item, index) => {
shuffledKeyObject[item] = keyObject[item];
});
return Object.freeze({ ...shuffledKeyObject });
}
static encrypt(password, keyObject) {
if (!password || !keyObject) {
throw new Error('Password or key hasnt provided in the params');
}
if (typeof password !== 'string') {
throw new Error('Password must be string');
}
if (typeof keyObject !== 'object') {
throw new Error('Key must be an object');
}
password = password.toString();
//console.log(` ~ Encrypting password...`);
const keyObjectProperties = Object.getOwnPropertyNames(keyObject);
const sections = Object.getOwnPropertyNames(
keyObject[keyObjectProperties[0]]
); // apple, lemon, or banana section, for now it doesnt matter which property we take from the representationalkeyObject because they all have the same sections;
const randomSectionIndex = Math.floor(Math.random() * sections.length);
const choosenSection = sections[randomSectionIndex]; // choosen section for this encryption
let encryptedPassword = '';
for (let i = 0; i < keyObjectProperties.length; i++) {
for (let j = 0; j < sections.length; j++) {
if (keyObject[keyObjectProperties[i]][sections[j]].length === 0) {
console.error(
chalk.red(
' ! One or more array in sections of the keyObject is empty'
)
);
console.error(chalk.red(' ! Returning null...'));
return null;
}
}
}
for (let i = 0; i < password.length; i++) {
const passwordLetter = password[i];
for (let j = 0; j < keyObjectProperties.length; j++) {
const keyObjectProperty = keyObjectProperties[j];
if (passwordLetter === keyObjectProperty) {
// here we are choosing a random encryption from the choosenSection array of the passwordLetter of the keyObject
// the length of the choosen section
const choosenSectionLength =
keyObject[`${keyObjectProperty}`][choosenSection].length;
const randomIndex = Math.floor(Math.random() * choosenSectionLength);
encryptedPassword =
encryptedPassword +
keyObject[`${keyObjectProperty}`][choosenSection][randomIndex];
}
}
}
//console.log(` ~ Encrypting password is done...`);
return `${encryptedPassword}`;
}
static decrypt(encryptedPassword, keyObject) {
if (!keyObject || !encryptedPassword) {
throw new Error('Encrypted password or key hasnt provided in the params');
}
if (typeof encryptedPassword !== 'string') {
throw new Error('Password must be string');
}
if (typeof keyObject !== 'object') {
throw new Error('Key must be an object');
}
//console.log(` ~ Decrypting password...`);
let decryptedPassword = '';
const keyObjectProperties = Object.getOwnPropertyNames(keyObject);
if (keyObjectProperties.length <= 0) {
console.log(chalk.red(` ~ keyObject is empty in decrypt...`));
console.log(chalk.red(` ~ Returning null...`));
return null;
}
const sectionProperties = Object.getOwnPropertyNames(
keyObject[keyObjectProperties[0]]
);
for (let i = 0; i <= encryptedPassword.length; i++) {
if (i % 6 === 0) {
// the value 6 is static I think it is better that way if we hard code it.
// extract the encryption from the encryptedPassword by seeking through with the coefficients of the complexity
const extractedEncryption = encryptedPassword.substr(i, 6);
for (let j = 0; j < keyObjectProperties.length; j++) {
for (let k = 0; k < sectionProperties.length; k++) {
keyObject[keyObjectProperties[j]][sectionProperties[k]].find(
(item, index) => {
if (extractedEncryption === item) {
decryptedPassword += keyObjectProperties[j];
}
}
);
}
}
}
}
//console.log(` ~ Decrypting password is done...`);
return `${decryptedPassword}`;
}
}
module.exports = ObjectEncrypt;