-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptography.js
111 lines (97 loc) · 2.51 KB
/
cryptography.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
/*
* Author: Jim Harris
* Cryptography module for PannIRC
* Module that lets users use basic cryptography
*/
exports.convertToHex = function(number) {
var digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
var r = number < 0 ? '-' : '';
var n = Math.floor(Math.abs(number));
var mult16 = 1;
while (mult16 * 16 <= n) {
mult16 *= 16;
}
while (mult16 >= 1) {
var count = 0;
while (n >= mult16) {
n -= mult16;
count++;
}
r += digits[count];
mult16 /= 16;
}
if (r == '') r = '0';
return r;
}
exports.decimalToHex = function(data, args, info) {
data.bot.say(info.channel, '0x' + exports.convertToHex(args[0]));
}
exports.stringToHex = function(data, args, info) {
var r = '0x';
for (var i = 0; i < args[0].length; i++) {
if (args[0].charAt(i) == ' ') r += ' 0x';
else r += exports.convertToHex(args[0].charCodeAt(i));
}
data.bot.say(info.channel, r);
}
exports.convertToBinary = function(number) {
var r = number < 0 ? '-' : '';
var n = Math.floor(Math.abs(number));
var mult2 = 1;
while (mult2 * 2 <= n) {
mult2 *= 2;
}
while (mult2 >= 1) {
var count = 0;
while (n >= mult2) {
n -= mult2;
count++;
}
r += count;
mult2 /= 2;
}
if (r == '') r = '0';
return r;
}
exports.caesar = function(rot, phrase, oneChar) {
var str = phrase;
var key = rot%26;
var r = "";
for (var i = 0; i < str.length; i++) {
var n = (str.charCodeAt(i));
var a = n >= 65 && n <= 90 ? 65 : n >= 97 && n <= 122 ? 97 : -1;
if (a != -1) {
n += (key);
if (n > a + 25) {
n -= 26;
}
}
else if (oneChar) return 0;
r += String.fromCharCode(n);
}
return r;
}
exports.sayCaesar = function(data, args, info) {
data.bot.say(info.channel, exports.caesar(args[0], args[1], false));
}
exports.vigenere = function(data, args, info) {
var keyword = args[0];
var plainText = args[1];
var r = "";
var i = 0;
var j = 0;
while (j < plainText.length) {
var k = keyword.charCodeAt(i);
var z = (k >= 65 && k <= 90) ? 90 : (k >= 97 && k <= 122) ? 122 : k+25;
var p = plainText.charCodeAt(j);
//r += exports.caesar(keyword.charCodeAt(i), plainText.substring(j, j+1), false);
if (z == 90 || z == 122) {
//var z = keyword.charCodeAt(i) >= 65 && keyword.charCodeAt(i) <= 90 ? 90 : keyword.charCodeAt(i) >= 97 && keyword.charCodeAt(i) <= 122 ? 122 : -1;
r += exports.caesar(26-(z-keyword.charCodeAt(i))-1, plainText.substring(j, j+1), false);
i = i >= keyword.length-1 ? 0 : i+1;
}
else r += plainText.charAt(j);
j++;
}
data.bot.say(info.channel, r);
}