forked from shadowsocks/shadowsocks-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.js
executable file
·50 lines (44 loc) · 1.09 KB
/
encrypt.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
// Generated by CoffeeScript 1.3.3
(function() {
var crypto, int32Max, merge_sort;
crypto = require("crypto");
merge_sort = require("./merge_sort").merge_sort;
int32Max = Math.pow(2, 32);
exports.getTable = function(key) {
var ah, al, decrypt_table, hash, i, md5sum, table;
table = new Array(256);
decrypt_table = new Array(256);
md5sum = crypto.createHash("md5");
md5sum.update(key);
hash = new Buffer(md5sum.digest(), "binary");
al = hash.readUInt32LE(0);
ah = hash.readUInt32LE(4);
i = 0;
while (i < 256) {
table[i] = i;
i++;
}
i = 1;
while (i < 1024) {
table = merge_sort(table, function(x, y) {
return ((ah % (x + i)) * int32Max + al) % (x + i) - ((ah % (y + i)) * int32Max + al) % (y + i);
});
i++;
}
i = 0;
while (i < 256) {
decrypt_table[table[i]] = i;
++i;
}
return [table, decrypt_table];
};
exports.encrypt = function(table, buf) {
var i;
i = 0;
while (i < buf.length) {
buf[i] = table[buf[i]];
i++;
}
return buf;
};
}).call(this);