-
Notifications
You must be signed in to change notification settings - Fork 1
/
secrets.js
72 lines (62 loc) · 1.95 KB
/
secrets.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
// Encodes and Decodes text
var secrets = (function() {
'use strict';
function decode(key, cipher) { // Primary decode operation
var arrKey = key.split('');
var arrCipher = cipher.split('');
var arrKeyNum = uniEncode(arrKey);
var arrCipherNum = uniEncode(arrCipher);
var arrKeystream = [];
for (var i = 0; i < arrCipherNum.length; i++) {
var mult = returnInt(i / arrKeyNum.length);
var pos = i % arrKeyNum.length;
arrKeystream.push(arrKeyNum[pos] + mult);
}
var arrTextNum = [];
for (var j = 0; j < arrCipherNum.length; j++) {
arrTextNum.push((arrCipherNum[j] + 65536 - arrKeystream[j]) % 65536);
}
var arrText = uniDecode(arrTextNum);
var text = arrText.join('');
return text;
}
function encode(key, text) { // Primary encode operation
var arrKey = key.split('');
var arrText = text.split('');
var arrKeyNum = uniEncode(arrKey);
var arrTextNum = uniEncode(arrText);
var arrKeystream = [];
for (var i = 0; i < arrTextNum.length; i++) {
var mult = returnInt(i / arrKeyNum.length);
var pos = i % arrKeyNum.length;
arrKeystream.push(arrKeyNum[pos] + mult);
}
var arrCipherNum = [];
for (var j = 0; j < arrTextNum.length; j++) {
arrCipherNum.push((arrTextNum[j] + arrKeystream[j]) % 65536);
}
var arrCipher = uniDecode(arrCipherNum);
var cipher = arrCipher.join('');
return cipher;
}
function returnInt(n) { // Staple function
return parseInt(n, 10);
}
function uniDecode(arr) { // Maps an array of Unicode values to their UTF-8 characters
return arr.map(function(x) {
return String.fromCharCode(x);
});
}
function uniEncode(arr) { // Maps an array of UTF-8 characters to their Unicode values
return arr.map(function(x) {
return x.charCodeAt(0);
});
}
// Public ----------
return {
decode: decode,
encode: encode,
uniDecode: uniDecode,
uniEncode: uniEncode
};
}());