-
Notifications
You must be signed in to change notification settings - Fork 803
/
Copy pathso-44303784.js
246 lines (213 loc) · 6.7 KB
/
so-44303784.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// https://stackoverflow.com/questions/44303784/javascript-aes-encryption-is-slow
// Test decryption with single update(), many update()s, and node
// Test chunk size for many update()s
const forge = require('../..');
const assert = require('assert');
const crypto = require('crypto');
const pwd = 'aStringPassword';
const iv = forge.random.getBytesSync(16);
const salt = forge.random.getBytesSync(16);
const key = forge.pkcs5.pbkdf2(pwd, salt, 100, 16);
function test_forge(bytes) {
const buf = forge.util.createBuffer(bytes);
const start = new Date();
const decipher = forge.cipher.createDecipher('AES-CBC', key);
decipher.start({iv: iv});
decipher.update(buf);
const result = decipher.finish();
assert(result);
const plain = decipher.output.getBytes();
const time = (new Date() - start) / 1000;
//console.log(`decrypted in ${time}s`);
return {
time,
plain
};
}
function test_forge_chunk(bytes, chunkSize) {
if(!chunkSize) {
chunkSize = 1024 * 16;
}
const start = new Date();
const decipher = forge.cipher.createDecipher('AES-CBC', key);
decipher.start({iv: iv});
const length = bytes.length;
let index = 0;
let plain = '';
do {
plain += decipher.output.getBytes();
const buf = forge.util.createBuffer(bytes.substr(index, chunkSize));
decipher.update(buf);
index += chunkSize;
} while(index < length);
const result = decipher.finish();
assert(result);
plain += decipher.output.getBytes();
const time = (new Date() - start) / 1000;
//console.log(`decrypted in ${time}s`);
return {
time,
plain
};
}
function test_node(bytes) {
const bufb = Buffer.from(bytes, 'binary');
const ivb = Buffer.from(iv, 'binary');
const keyb = Buffer.from(key, 'binary');
const start = new Date();
const decipher = crypto.createDecipheriv('aes-128-cbc', keyb, ivb);
let plain = decipher.update(bufb, 'utf8', 'utf8');
plain += decipher.final('utf8');
const time = (new Date() - start) / 1000;
//console.log(`decrypted in ${time}s`);
return {
time,
plain
};
}
function data(megs) {
// slower single chunk
const start = new Date();
var x = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef';
var plain = '';
const minlen = megs * 1024 * 1024;
while(plain.length < minlen) {
plain += x;
}
const cipher = forge.cipher.createCipher('AES-CBC', key);
cipher.start({iv: iv});
cipher.update(forge.util.createBuffer(plain));
const result = cipher.finish();
assert(result);
const encrypted = cipher.output.getBytes();
const time = (new Date() - start) / 1000;
//console.log(`data m:${megs} t:${time}s m/s:${megs/time}`);
return {
plain,
encrypted,
time
};
}
function data_chunk(megs, chunkSize) {
if(!chunkSize) {
chunkSize = 1024 * 16;
}
// faster with chunksize
const start = new Date();
// make some large plain text bigger than some size
var x = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef';
var plain = '';
const minlen = megs * 1024 * 1024;
while(plain.length < minlen) {
plain += x;
}
const cipher = forge.cipher.createCipher('AES-CBC', key);
cipher.start({iv: iv});
const length = plain.length;
let index = 0;
let encrypted = '';
do {
encrypted += cipher.output.getBytes();
const buf = forge.util.createBuffer(plain.substr(index, chunkSize));
cipher.update(buf);
index += chunkSize;
} while(index < length);
const result = cipher.finish();
assert(result);
encrypted += cipher.output.getBytes();
const time = (new Date() - start) / 1000;
//console.log(`data_chunk m:${megs} t:${time}s m/s:${megs/time}`);
return {
plain,
encrypted,
time
};
}
function compareImpl() {
const maxmegs = 20;
let csv = '';
// sweep input size
for(let i = 1; i <= maxmegs; ++i) {
//const input = data(i);
const input = data_chunk(i, 1024 * 64);
// forge w/ one chunk
const tfs = [
test_forge(input.encrypted),
test_forge(input.encrypted),
test_forge(input.encrypted)
];
tfs.forEach(res => assert(input.plain == res.plain));
const tf = tfs.reduce((prev, cur) => prev.time < cur.time ? prev : cur);
// forge w/ chunks
const chunkSize = 1024 * 64;
const tfcs = [
test_forge_chunk(input.encrypted, chunkSize),
test_forge_chunk(input.encrypted, chunkSize),
test_forge_chunk(input.encrypted, chunkSize)
];
tfcs.forEach(res => assert(input.plain == res.plain));
const tfc = tfcs.reduce((prev, cur) => prev.time < cur.time ? prev : cur);
// node
const tns = [
test_node(input.encrypted),
test_node(input.encrypted),
test_node(input.encrypted)
];
tns.forEach(res => assert(input.plain == res.plain));
const tn = tns.reduce((prev, cur) => prev.time < cur.time ? prev : cur);
/* eslint-disable max-len */
csv += `${i}\t${tf.time}\t${i / tf.time}\t${tfc.time}\t${i / tfc.time}\t${tn.time}\t${i / tn.time}\t${tf.time / tn.time}\t${tfc.time / tn.time}\n`;
console.log(`m:${i} tf:${tf.time} tf/s:${i / tf.time} tfc:${tfc.time} tfc/s:${i / tfc.time} tn:${tn.time} tn/s:${i / tn.time} sf:${tf.time / tn.time} sfc:${tfc.time / tn.time}`);
/* eslint-enable max-len */
}
console.log(csv);
}
function compareDecChunkSize() {
const megs = 10;
let csv = '';
const input = data_chunk(megs, 1024 * 64);
function _test(k) {
const chunkSize = 1024 * k;
const tfcs = [
test_forge_chunk(input.encrypted, chunkSize),
test_forge_chunk(input.encrypted, chunkSize),
test_forge_chunk(input.encrypted, chunkSize)
];
tfcs.forEach(res => assert(input.plain == res.plain));
const tfc = tfcs.reduce((prev, cur) => prev.time < cur.time ? prev : cur);
csv += `${k}\t${tfc.time}\t${megs / tfc.time}\n`;
console.log(`k:${k} tfc:${tfc.time} tfc/s:${megs / tfc.time}`);
}
// sweep KB chunkSize
const sweep = [
1, 2, 4, 8, 16, 32, 64, 96, 128, 160, 192, 256,
320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
];
sweep.forEach(k => _test(k));
console.log(csv);
}
function compareEncChunkSize() {
const megs = 10;
let csv = '';
function _test(k) {
const chunkSize = 1024 * k;
const dcs = [
data_chunk(megs, chunkSize),
data_chunk(megs, chunkSize),
data_chunk(megs, chunkSize)
];
const dc = dcs.reduce((prev, cur) => prev.time < cur.time ? prev : cur);
csv += `${k}\t${dc.time}\t${megs / dc.time}\n`;
console.log(`k:${k} dc:${dc.time} dc/s:${megs / dc.time}`);
}
// sweep KB chunkSize
const sweep = [
1, 2, 4, 8, 16, 32, 64, 96, 128, 160, 192, 256,
320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
];
sweep.forEach(k => _test(k));
console.log(csv);
}
compareImpl();
//compareDecChunkSize();
//compareEncChunkSize();