remove some unnecessary checks for file-crypto and make encryption properly async

pull/1/head
ansuz 7 years ago
parent 8c4ed76fa7
commit ee46a4e862

@ -2,7 +2,7 @@ define([
'/bower_components/tweetnacl/nacl-fast.min.js', '/bower_components/tweetnacl/nacl-fast.min.js',
], function () { ], function () {
var Nacl = window.nacl; var Nacl = window.nacl;
var PARANOIA = true; //var PARANOIA = true;
var plainChunkLength = 128 * 1024; var plainChunkLength = 128 * 1024;
var cypherChunkLength = 131088; var cypherChunkLength = 131088;
@ -36,24 +36,11 @@ define([
var increment = function (N) { var increment = function (N) {
var l = N.length; var l = N.length;
while (l-- > 1) { while (l-- > 1) {
if (PARANOIA) {
if (typeof(N[l]) !== 'number') {
throw new Error('E_UNSAFE_TYPE');
}
if (N[l] > 255) {
throw new Error('E_OUT_OF_BOUNDS');
}
}
/* jshint probably suspects this is unsafe because we lack types /* jshint probably suspects this is unsafe because we lack types
but as long as this is only used on nonces, it should be safe */ but as long as this is only used on nonces, it should be safe */
if (N[l] !== 255) { return void N[l]++; } // jshint ignore:line if (N[l] !== 255) { return void N[l]++; } // jshint ignore:line
if (l === 0) { throw new Error('E_NONCE_TOO_LARGE'); }
N[l] = 0; N[l] = 0;
// you don't need to worry about this running out.
// you'd need a REAAAALLY big file
if (l === 0) {
throw new Error('E_NONCE_TOO_LARGE');
}
} }
}; };
@ -160,19 +147,21 @@ define([
} }
var takeChunk = function (cb) { var takeChunk = function (cb) {
var start = i * cypherChunkLength + 2 + metadataLength; setTimeout(function () {
var end = start + cypherChunkLength; var start = i * cypherChunkLength + 2 + metadataLength;
i++; var end = start + cypherChunkLength;
var box = new Uint8Array(u8.subarray(start, end)); i++;
var box = new Uint8Array(u8.subarray(start, end));
// decrypt the chunk
var plaintext = Nacl.secretbox.open(box, nonce, key); // decrypt the chunk
increment(nonce); var plaintext = Nacl.secretbox.open(box, nonce, key);
increment(nonce);
if (!plaintext) { return cb('DECRYPTION_ERROR'); } if (!plaintext) { return cb('DECRYPTION_ERROR'); }
_progress(end); _progress(end);
cb(void 0, plaintext); cb(void 0, plaintext);
});
}; };
var chunks = []; var chunks = [];
@ -219,7 +208,7 @@ define([
var state = 0; var state = 0;
var next = function (cb) { var next = function (cb) {
if (state === 2) { return void cb(); } if (state === 2) { return void setTimeout(cb); }
var start; var start;
var end; var end;
@ -238,7 +227,9 @@ define([
.concat(slice(box))); .concat(slice(box)));
state++; state++;
return void cb(void 0, prefixed); return void setTimeout(function () {
cb(void 0, prefixed);
});
} }
// encrypt the rest of the file... // encrypt the rest of the file...
@ -253,7 +244,9 @@ define([
// regular data is done // regular data is done
if (i * plainChunkLength >= u8.length) { state = 2; } if (i * plainChunkLength >= u8.length) { state = 2; }
return void cb(void 0, box); setTimeout(function () {
cb(void 0, box);
});
}; };
return next; return next;

Loading…
Cancel
Save