make file crypto great again

pull/1/head
ansuz 8 years ago
parent 03da870a7a
commit 4ee42b87d4

@ -58,14 +58,14 @@ define([
}; };
var joinChunks = function (chunks) { var joinChunks = function (chunks) {
return new Uint8Array(chunks.reduce(function (A, B) { return new Blob(chunks);
return slice(A).concat(slice(B));
}, []));
}; };
var decrypt = function (u8, key, cb) { var decrypt = function (u8, key, done, progress) {
var fail = function (e) { var MAX = u8.length;
cb(e || "DECRYPTION_ERROR"); var _progress = function (offset) {
if (typeof(progress) !== 'function') { return; }
progress(Math.min(1, offset / MAX));
}; };
var nonce = createNonce(); var nonce = createNonce();
@ -86,16 +86,18 @@ define([
try { try {
res.metadata = JSON.parse(Nacl.util.encodeUTF8(metaChunk)); res.metadata = JSON.parse(Nacl.util.encodeUTF8(metaChunk));
} catch (e) { } catch (e) {
return fail('E_METADATA_DECRYPTION'); return window.setTimeout(function () {
done('E_METADATA_DECRYPTION');
});
} }
if (!res.metadata) { if (!res.metadata) {
return void setTimeout(function () { return void setTimeout(function () {
cb('NO_METADATA'); done('NO_METADATA');
}); });
} }
var takeChunk = function () { var takeChunk = function (cb) {
var start = i * cypherChunkLength + 2 + metadataLength; var start = i * cypherChunkLength + 2 + metadataLength;
var end = start + cypherChunkLength; var end = start + cypherChunkLength;
i++; i++;
@ -104,24 +106,36 @@ define([
// decrypt the chunk // decrypt the chunk
var plaintext = Nacl.secretbox.open(box, nonce, key); var plaintext = Nacl.secretbox.open(box, nonce, key);
increment(nonce); increment(nonce);
return plaintext;
if (!plaintext) { return cb('DECRYPTION_ERROR'); }
_progress(end);
cb(void 0, plaintext);
}; };
var chunks = []; var chunks = [];
// decrypt file contents
var chunk; var again = function () {
for (;i * cypherChunkLength < u8.length;) { takeChunk(function (e, plaintext) {
chunk = takeChunk(); if (e) {
if (!chunk) { return setTimeout(function () {
return window.setTimeout(fail); done(e);
});
} }
chunks.push(chunk); if (plaintext) {
if (i * cypherChunkLength < u8.length) { // not done
chunks.push(plaintext);
return setTimeout(again);
} }
chunks.push(plaintext);
// send chunks
res.content = joinChunks(chunks); res.content = joinChunks(chunks);
return done(void 0, res);
}
done('UNEXPECTED_ENDING');
});
};
cb(void 0, res); again();
}; };
// metadata // metadata

@ -273,6 +273,8 @@ define([
myDataType = data.metadata.type; myDataType = data.metadata.type;
Title.updateTitle(title || Title.defaultTitle); Title.updateTitle(title || Title.defaultTitle);
Cryptpad.removeLoadingScreen(); Cryptpad.removeLoadingScreen();
}, function (progress) {
console.error(progress);
}); });
}); });
} }

Loading…
Cancel
Save