-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptonode.js
649 lines (562 loc) · 20.1 KB
/
cryptonode.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
class cryptonodeJS {
default_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/**
* Simplify Switch condition
* @param {String} type
* @param {CallableFunction} callback_encode
* @param {CallableFunction} callback_decode
* @returns {String | Throw}
*/
_switcher(type, callback_encode, callback_decode) {
switch (type.toLowerCase()) {
case 'e':
case 'encrypt':
case 'encode':
return callback_encode();
case 'd':
case 'decrypt':
case 'decode':
return callback_decode();
default:
throw ("Type doesnt exist");
}
}
/**
* Reversing List
* @param {Array} original_list
* @returns {Array}
*/
_reverseList(original_list) {
let reverse_list = Object.entries(original_list);
reverse_list = reverse_list.map((element) => element.reverse());
reverse_list = Object.fromEntries(reverse_list);
return reverse_list;
}
/**
* Check character type
* @param {String} character
* @returns {Boolean}
*/
_isUpper = (character) => {
return (character === character.toUpperCase()) ? true : false;
}
/**
* Check variable has value or not
* @param {*} value
*/
_required(value) {
if (value === undefined) throw ("Parameter value cant be blank");
}
/**
* Encoding / Decoding ROT 13 Cipher
* @param {String} source
* @returns {String}
*/
rot13(source) {
this._required(source);
return this.caesar('decode', source, 13);
}
/**
* Encoding / Decoding Caesar Cipher
* @param {String} type
* @param {String} source
* @param {Integer} key
* @param {String} custom_letter
* @returns {String}
*/
caesar(type, source, key = 13, custom_letter = undefined) {
this._required(source);
this._required(type);
let letters = custom_letter ? custom_letter.toUpperCase() : this.default_letters;
source = source.split("");
key = key % letters.length; // limiting key
const getChar = (is_uppercase, index) => {
if (is_uppercase) {
return letters[index].toUpperCase();
} else {
return letters[index].toLowerCase();
}
};
const callback_encode = () => {
return source.map((character) => {
let index = letters.indexOf(character.toUpperCase());
if (index !== -1) {
let is_uppercase = this._isUpper(character);
let letters_length = letters.length;
index += key;
if (index >= letters_length) index -= letters_length;
return getChar(is_uppercase, index);
}
return character;
}).join("");
};
const callback_decode = () => {
return source.map((character) => {
let index = letters.indexOf(character.toUpperCase());
if (index !== -1) {
let is_uppercase = this._isUpper(character);
index -= key;
if (index < 0) index += letters.length;
return getChar(is_uppercase, index);
}
return character;
}).join("");
}
return this._switcher(type, callback_encode, callback_decode);
}
/**
* Encoding / Decoding NATO Spelling
* @param {String} type
* @param {String} source
* @returns {String}
*/
nato(type, source) {
this._required(type);
this._required(source);
let nato_table = {
'1': 'One',
'2': 'Two',
'3': 'Three',
'4': 'Four',
'5': 'Five',
'6': 'Six',
'7': 'Seven',
'8': 'Eight',
'9': 'Nine',
a: 'Alfa',
b: 'Bravo',
c: 'Charlie',
d: 'Delta',
e: 'Echo',
f: 'Foxtrot',
g: 'Golf',
h: 'Hotel',
i: 'India',
j: 'Juliett',
k: 'Kilo',
l: 'Lima',
m: 'Mike',
n: 'November',
o: 'Oscar',
p: 'Papa',
q: 'Quebec',
r: 'Romeo',
s: 'Sierra',
t: 'Tango',
v: 'Victor',
w: 'Whiskey',
x: 'X-ray',
y: 'Yankee',
z: 'Zulu',
'.': 'Stop',
' ': '(space)'
};
let callback_encode = () => {
source = source.toLowerCase().split("");
return source.map((character) => {
if (nato_table[character] !== undefined) return nato_table[character];
return character;
}).join(" ");
}
let callback_decode = () => {
source = source.split(" ");
let reverse_table = this._reverseList(nato_table); // generate reverse table
return source.map((character) => {
if (reverse_table[character] !== undefined) return reverse_table[character];
return character;
}).join("");
}
return this._switcher(type, callback_encode, callback_decode);
}
/**
* Encode / Decode Morse
* @param {String} type
* @param {String} source
* @param {Object} options
* @returns {String}
*/
morse(type, source, options = undefined) {
this._required(type);
this._required(source);
let table_morse = {
'0': 'longlonglonglonglong',
'1': 'shortlonglonglonglong',
'2': 'shortshortlonglonglong',
'3': 'shortshortshortlonglong',
'4': 'shortshortshortshortlong',
'5': 'shortshortshortshortshort',
'6': 'longshortshortshortshort',
'7': 'longlongshortshortshort',
'8': 'longlonglongshortshort',
'9': 'longlonglonglongshort',
'-': 'longshortshortshortshortlong',
'=': 'longshortshortshortlong',
'!': 'longshortlongshortlonglong',
'@': 'shortlonglongshortlongshort',
'$': 'shortshortshortshortshort',
'&': 'shortlongshortshortshort',
'(': 'longshortlonglongshort',
')': 'longshortlonglongshortlong',
_: 'shortshortlonglongshortlong',
'+': 'shortlongshortlongshort',
q: 'longlongshortlong',
w: 'shortlonglong',
e: 'short',
r: 'shortlongshort',
t: 'long',
y: 'longshortlonglong',
u: 'shortshortlong',
i: 'shortshort',
o: 'longlonglong',
p: 'shortlonglongshort',
a: 'shortlong',
s: 'shortshortshort',
d: 'longshortshort',
f: 'shortshortlongshort',
g: 'longlongshort',
h: 'shortshortshortshort',
j: 'shortlonglonglong',
k: 'longshortlong',
l: 'shortlongshortshort',
';': 'longshortlongshortlongshort',
"'": 'shortlonglonglonglongshort',
':': 'longlonglongshortshortshort',
'"': 'shortlongshortshortlongshort',
z: 'longlongshortshort',
x: 'longshortshortlong',
c: 'longshortlongshort',
v: 'shortshortshortlong',
b: 'longshortshortshort',
n: 'longshort',
m: 'longlong',
',': 'longlongshortshortlonglong',
'.': 'shortlongshortlongshortlong',
'/': 'longshortshortlongshort',
'?': 'shortshortlonglongshortshort',
' ': 'space'
};
let default_options = {
"space": "/",
"long": "-",
"short": "."
};
options = options ? Object.assign({}, default_options, options) : default_options;
/**
* Prepare Morse table
* @param {Object} table
* @param {Object} options
* @returns {Object}
*/
const generateTable = (table, options) => {
let reverse_list = Object.entries(table);
reverse_list = reverse_list.map((element) => {
options.forEach((option) => {
element[1] = element[1].split(option[0]).join(option[1]); // replace all [name] with symbol
});
return element;
});
reverse_list = Object.fromEntries(reverse_list);
return reverse_list;
}
table_morse = generateTable(table_morse, Object.entries(options));
const callback_encode = function () {
source = source.split("");
return source.map((character) => {
let value = table_morse[character];
if (value) return value;
}).join(" ");
};
const callback_decode = () => {
let reverse_table = this._reverseList(table_morse);
source = source.split(" ");
return source.map((character) => {
let value = reverse_table[character];
if (value) return value;
}).join("");
};
return this._switcher(type, callback_encode, callback_decode);
}
/**
*
* @param {*} type
* @param {*} source_text
* @param {*} keys
* @param {*} custom_letters
*/
affine(type, source_text, keys, custom_letters) {
this._required(type);
this._required(source_text);
this._required(keys);
/**
* Find Modular Inverse
* @param {Integer} a
* @param {Integer} b
* @returns {Integer}
*/
const modInverse = (a, b) => {
/**
* Find Extended Greatest Common Divisor
* @param {Integer} a
* @param {Integer} b
* @returns {Array}
*/
const egcd = (a, b) => {
let [x, y, _x, _y] = [0, 1, 1, 0];
let div, mod, nx, ny = undefined;
while (a !== 0) {
div = parseInt(b / a),
mod = b % a;
nx = x - _x * div,
ny = y - _y * div;
b = a,
a = mod,
x = _x,
y = _y,
_x = nx,
_y = ny;
}
let gcd = b;
return [gcd, x, y];
};
let [gcd, x, y] = egcd(a, b);
return (gcd === 1) ? x % b : undefined; // modular inverse doesnt exist
};
/**
* Encryption plain_text
* (a x + b) % len
*
* @param {*} plain_text
* @param {*} a
* @param {*} b
* @param {*} letters
* @returns {String}
*/
const encription = (plain_text, a, b, letters) => {
let is_uppercase = false;
let letters_length = letters.length;
return plain_text.split("").map(character => {
let index = letters.indexOf(character.toUpperCase());
if (index !== -1) {
let key = (a * index + b) % letters_length;
let character_encrypted = letters[key];
is_uppercase = this._isUpper(character);
if (!is_uppercase) character_encrypted = character_encrypted.toLowerCase();
return character_encrypted;
}
return character;
}).join("");
};
/**
* Decrypt cipher
* P = (mod_inv * (index - b)) % 26
*
* @param {*} cipher_text
* @param {*} a
* @param {*} b
* @param {*} letters
* @returns {String}
*/
const decription = (cipher_text, a, b, letters) => {
let is_uppercase = false;
let letters_length = letters.length;
return cipher_text.split("").map(character => {
let index = letters.indexOf(character.toUpperCase());
if (index !== -1) {
let key = (a * (index - b)) % letters_length;
is_uppercase = this._isUpper(character);
if (key < 0) key += 26; // handle negatif number
let char = letters[key];
if (!is_uppercase) char = char.toLowerCase();
return char;
}
return character;
}).join("");
};
let letters = (custom_letters) ? custom_letters.toUpperCase() : this.default_letters;
let mod_inv = modInverse(keys[0], letters.length);
if (mod_inv === undefined) throw ("Key isnt coprime !!");
const callback_encode = function () {
return encription(source_text, keys[0], keys[1], letters);
};
const callback_decode = function () {
return decription(source_text, mod_inv, keys[1], letters);
};
return this._switcher(type, callback_encode, callback_decode);
}
vigenere(type, source, keys, custom_letters = undefined) {
this._required(type);
this._required(source);
this._required(keys);
let letters = (custom_letters) ? custom_letters.toUpperCase() : this.default_letters;
let letters_length = letters.length;
let key_length = keys.length;
let index = 0;
let is_uppercase = false;
keys.split("").forEach(character => {
if (letters.indexOf(character.toUpperCase()) === -1) throw ("Char key doesnt exist in letters");
});
keys = keys.repeat(Math.ceil(letters_length / key_length)).toUpperCase();
let callback_e = () => {
return source.split("").map((character) => {
let character_index = letters.indexOf(character.toUpperCase());
is_uppercase = this._isUpper(character);
if (character_index !== -1) {
let another_character = keys[index];
let key_index = letters.indexOf(another_character.toUpperCase());
let key = (character_index + key_index) % letters_length;
character = letters[key];
if (!is_uppercase) character = character.toLowerCase();
index++;
return character;
}
return character;
}).join("");
};
let callback_d = () => {
return source.split("").map((character) => {
let character_index = letters.indexOf(character.toUpperCase());
is_uppercase = this._isUpper(character);
if (character_index !== -1) {
let another_character = keys[index];
let i_key = letters.indexOf(another_character.toUpperCase());
let key = (character_index - i_key) % letters_length;
if (key < 0) {
key += letters_length;
}
character = letters[key];
if (!is_uppercase) character = character.toLowerCase();
index++;
return character;
}
return character;
}).join("");
};
return this._switcher(type, callback_e, callback_d);
}
/**
* Encoding / Decoding Text to Base64 format
* @param {String} type
* @param {String} source
* @returns {String}
*/
b64 = (type, source) => {
this._required(type);
this._required(source);
let base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/**
* Convert Binary to Decimal
* @param {String} binary
* @returns {Integer}
*/
const convert2Decimal = (binary) => {
let decimal = 0;
let length = binary.length - 1;
binary.split("").forEach((value, index) => {
decimal += (value === "1") ? 2 ** (length - index) : 0;
});
return decimal;
};
/**
* Convert Floating number into Integer
* @param {Float} number
* @returns {Integer}
*/
const floor = (number) => {
return number - (number % 1);
};
/**
* Convert Decimal into Binary
* @param {Integer} number
* @returns {String}
*/
const convert2Binary = (number) => {
let binary = "";
while (number !== 0) {
binary = `${number % 2}` + binary;
number = floor(number / 2);
}
return binary;
};
/**
* Convert origin string to base64
* @returns {String}
*/
const encode = () => {
let cipher_text = "";
/**
* Convert Ascii string to binnary
* @param {String} source
* @returns {String}
*/
const string2Binary = (source) => {
return source.split("").map(character => {
let binary = convert2Binary(character.charCodeAt(0));
let b_length = binary.length;
if (b_length < 8) {
binary = "0".repeat(8 - b_length) + binary; // add padding
}
return binary;
}).join("");
};
source = string2Binary(source).split(/(.{24})/).filter(value => value !== "");
source.forEach((byte_list) => {
byte_list = byte_list.split(/(.{6})/).filter(value => value !== "");
let padding_length = byte_list.length % 2;
byte_list.forEach((byte) => {
if (byte.length < 6) {
if (6 - byte.length <= 2) {
padding_length = 1;
} else {
padding_length = 2;
}
byte += "0".repeat(6 - byte.length);
}
cipher_text += base64_table[convert2Decimal(byte)];
})
if (padding_length) {
cipher_text += "=".repeat(padding_length);
}
});
return cipher_text;
};
/**
* Convert base64 to origin string
* @returns {String}
*/
const decode = () => {
let plain_text = "";
/**
* Find Matching Character in table and return index as binary
* @param {String} source
* @returns {String}
*/
const matchWithTable = (source) => {
return source.split("").map(character => {
let character_position = base64_table.indexOf(character);
if (character_position === -1) {
throw ("Invalid character detected");
}
let binary = convert2Binary(character_position);
let b_length = binary.length;
if (b_length < 6) {
binary = "0".repeat(6 - b_length) + binary;
}
return binary;
}).join("");
};
source = source.split("=").join(""); // replace all = character
source = matchWithTable(source).split(/(.{24})/).filter(value => value !== "");
source.forEach((byte_list) => {
byte_list = byte_list.split(/(.{8})/).filter(value => value !== "");
byte_list.forEach((byte) => {
if (byte.length == 8) {
plain_text += String.fromCharCode(convert2Decimal(byte));
}
})
});
return plain_text;
};
return this._switcher(type, encode, decode);
}
}
module.exports = cryptonodeJS;