-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOne_node.js
118 lines (115 loc) · 4.31 KB
/
DOne_node.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
function str2Arr(str) {
var result = [];
for (let i = 0; i < str.length; i++) {
result.push(str.charCodeAt(i))
}
return result;
}
function arr2str(arr) {
var result = "";
arr.forEach(charcode => {
result+=String.fromCharCode(charcode);
});
return result;
}
var alphabetBaseMain = str2Arr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
const DOne = {
encrypt: function (key, plain) {
if (typeof plain != 'object')
throw new Error("Plain type must be a Buffer")
var result = [];
var alphabetBase1 = DOne_CreateAlphabetBase(plain);
alphabetBaseMain = alphabetBase1.alphabet;
var rest = alphabetBase1.rest;
for (let i = 0; i < str2Arr(key).length; i++) {
if (![...alphabetBaseMain].includes(str2Arr(key)[i]))
throw new Error("Key contain byte that are not in alphabetBase")
}
var formatedkey = DOne_FormatKey(key);
var alphabetList = [];
for (let i = 0; i < formatedkey.length; i++)
alphabetList.push(DOne_CreateAlphabet(formatedkey[i]));
for (let i = 0, o = 0; i < plain.length; i++, o++) {
var at = alphabetBaseMain.indexOf(plain[i]);
if (i >= 32)
o = 0;
result.push(alphabetList[o][at]);
}
return Buffer.from(arr2str(result)).toString("base64") + (rest == "" ? "" : ".") + rest;
},
decrypt: function (key, encrypted_b64) {
var rest = "";
var encrypted = null;
if (encrypted_b64.split('.').length == 2) {
rest = encrypted_b64.split('.')[1];
encrypted = str2Arr(Buffer.from(encrypted_b64.split('.')[0], "base64").toString("utf16le"));
} else {
encrypted = str2Arr(Buffer.from(encrypted_b64, "base64").toString("utf16le"));
}
var result = [];
alphabetBaseMain = DOne_CreateAlphabetBaseFromRest(rest).alphabet;
for (let i = 0; i < str2Arr(key).length; i++) {
if (![...alphabetBaseMain].includes(str2Arr(key)[i]))
throw new Error("Key contain byte that are not in alphabetBase")
}
var formatedkey = DOne_FormatKey(key);
var alphabetList = [];
for (let i = 0; i < formatedkey.length; i++)
alphabetList.push(DOne_CreateAlphabet(formatedkey[i]));
for (let i = 0, o = 0; i < encrypted.length; i++, o++) {
if (i >= 32)
o = 0;
var at = alphabetList[o].indexOf(encrypted[i]);
result.push(alphabetBaseMain[at]);
}
return arr2str(result);
},
str2Arr(str) {
var result = [];
for (let i = 0; i < str.length; i++) {
result.push(str.charCodeAt(i))
}
return result;
}
};
function DOne_FormatKey(key) {
var result2 = [];
for (let i = 0; result2.length < 32; i++) {
for (let keyI = 0; keyI < key.length; keyI++) {
result2.push(str2Arr(key)[keyI]);
}
result2 = result2.reverse();
}
return result2.slice(0, 32);
}
function DOne_CreateAlphabetBase(plain) {
var rest = [];
for (let i = 0; i < plain.length; i++) {
if (!alphabetBaseMain.includes(plain[i])) {
rest.push(plain[i]);
alphabetBaseMain.push(plain[i]);
}
}
return {
alphabet: alphabetBaseMain,
rest: Buffer.from(rest).toString("base64")
};
}
function DOne_CreateAlphabetBaseFromRest(rest) {
for (let i = 0; i < str2Arr(Buffer.from(rest, "base64").toString("utf16le")).length; i++) {
if (!alphabetBaseMain.includes(str2Arr(Buffer.from(rest, "base64").toString("utf16le"))[i])) {
alphabetBaseMain = alphabetBaseMain.concat(str2Arr(Buffer.from(rest, "base64").toString("utf16le"))[i]);
}
}
return {
alphabet: alphabetBaseMain,
rest: ""
};
}
function DOne_CreateAlphabet(keybyte) {
var result = [...alphabetBaseMain];
var result2 = result.splice(alphabetBaseMain.indexOf(keybyte), result.length - 1);
// console.log(Uint16Array.from(result.splice(alphabetBaseMain.indexOf(keybyte), result.length-1)).toString());
return result2.concat(result)
}
module.exports = DOne;