forked from 521xueweihan/shadowsocks-heroku
-
Notifications
You must be signed in to change notification settings - Fork 1
/
encrypt.js
207 lines (187 loc) · 5.43 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
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
// Generated by CoffeeScript 1.10.0
(function() {
var EVP_BytesToKey, Encryptor, bytes_to_key_results, cachedTables, create_rc4_md5_cipher, crypto, getTable, int32Max, merge_sort, method_supported, substitute;
crypto = require("crypto");
merge_sort = require("./merge_sort").merge_sort;
int32Max = Math.pow(2, 32);
cachedTables = {};
getTable = function(key) {
var ah, al, decrypt_table, hash, i, md5sum, result, table;
if (cachedTables[key]) {
return cachedTables[key];
}
console.log("calculating ciphers");
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;
}
result = [table, decrypt_table];
cachedTables[key] = result;
return result;
};
substitute = function(table, buf) {
var i;
i = 0;
while (i < buf.length) {
buf[i] = table[buf[i]];
i++;
}
return buf;
};
bytes_to_key_results = {};
EVP_BytesToKey = function(password, key_len, iv_len) {
var count, d, data, i, iv, key, m, md5, ms;
if (bytes_to_key_results[password + ":" + key_len + ":" + iv_len]) {
return bytes_to_key_results[password + ":" + key_len + ":" + iv_len];
}
m = [];
i = 0;
count = 0;
while (count < key_len + iv_len) {
md5 = crypto.createHash('md5');
data = password;
if (i > 0) {
data = Buffer.concat([m[i - 1], password]);
}
md5.update(data);
d = md5.digest();
m.push(d);
count += d.length;
i += 1;
}
ms = Buffer.concat(m);
key = ms.slice(0, key_len);
iv = ms.slice(key_len, key_len + iv_len);
bytes_to_key_results[password] = [key, iv];
return [key, iv];
};
method_supported = {
'aes-128-cfb': [16, 16],
'aes-192-cfb': [24, 16],
'aes-256-cfb': [32, 16],
'bf-cfb': [16, 8],
'camellia-128-cfb': [16, 16],
'camellia-192-cfb': [24, 16],
'camellia-256-cfb': [32, 16],
'cast5-cfb': [16, 8],
'des-cfb': [8, 8],
'idea-cfb': [16, 8],
'rc2-cfb': [16, 8],
'rc4': [16, 0],
'rc4-md5': [16, 16],
'seed-cfb': [16, 16]
};
create_rc4_md5_cipher = function(key, iv, op) {
var md5, rc4_key;
md5 = crypto.createHash('md5');
md5.update(key);
md5.update(iv);
rc4_key = md5.digest();
if (op === 1) {
return crypto.createCipheriv('rc4', rc4_key, '');
} else {
return crypto.createDecipheriv('rc4', rc4_key, '');
}
};
Encryptor = (function() {
function Encryptor(key1, method1) {
var ref;
this.key = key1;
this.method = method1;
this.iv_sent = false;
if (this.method === 'table') {
this.method = null;
}
if (this.method != null) {
this.cipher = this.get_cipher(this.key, this.method, 1, crypto.randomBytes(32));
} else {
ref = getTable(this.key), this.encryptTable = ref[0], this.decryptTable = ref[1];
}
}
Encryptor.prototype.get_cipher_len = function(method) {
var m;
method = method.toLowerCase();
m = method_supported[method];
return m;
};
Encryptor.prototype.get_cipher = function(password, method, op, iv) {
var iv_, key, m, ref;
method = method.toLowerCase();
password = new Buffer(password, 'binary');
m = this.get_cipher_len(method);
if (m != null) {
ref = EVP_BytesToKey(password, m[0], m[1]), key = ref[0], iv_ = ref[1];
if (iv == null) {
iv = iv_;
}
if (op === 1) {
this.cipher_iv = iv.slice(0, m[1]);
}
iv = iv.slice(0, m[1]);
if (method === 'rc4-md5') {
return create_rc4_md5_cipher(key, iv, op);
} else {
if (op === 1) {
return crypto.createCipheriv(method, key, iv);
} else {
return crypto.createDecipheriv(method, key, iv);
}
}
}
};
Encryptor.prototype.encrypt = function(buf) {
var result;
if (this.method != null) {
result = this.cipher.update(buf);
if (this.iv_sent) {
return result;
} else {
this.iv_sent = true;
return Buffer.concat([this.cipher_iv, result]);
}
} else {
return substitute(this.encryptTable, buf);
}
};
Encryptor.prototype.decrypt = function(buf) {
var decipher_iv, decipher_iv_len, result;
if (this.method != null) {
if (this.decipher == null) {
decipher_iv_len = this.get_cipher_len(this.method)[1];
decipher_iv = buf.slice(0, decipher_iv_len);
this.decipher = this.get_cipher(this.key, this.method, 0, decipher_iv);
result = this.decipher.update(buf.slice(decipher_iv_len));
return result;
} else {
result = this.decipher.update(buf);
return result;
}
} else {
return substitute(this.decryptTable, buf);
}
};
return Encryptor;
})();
exports.Encryptor = Encryptor;
exports.getTable = getTable;
}).call(this);