|
|
|
@ -1,8 +1,9 @@
|
|
|
|
|
define([
|
|
|
|
|
'/file/file-crypto.js',
|
|
|
|
|
'/common/common-hash.js',
|
|
|
|
|
'/bower_components/nthen/index.js',
|
|
|
|
|
'/bower_components/tweetnacl/nacl-fast.min.js',
|
|
|
|
|
], function (FileCrypto, Hash) {
|
|
|
|
|
], function (FileCrypto, Hash, nThen) {
|
|
|
|
|
var Nacl = window.nacl;
|
|
|
|
|
var module = {};
|
|
|
|
|
|
|
|
|
@ -14,13 +15,29 @@ define([
|
|
|
|
|
var path = file.path;
|
|
|
|
|
|
|
|
|
|
var password = file.password;
|
|
|
|
|
var hash = Hash.createRandomHash('file', password);
|
|
|
|
|
var secret = Hash.getSecrets('file', hash, password);
|
|
|
|
|
var key = secret.keys.cryptKey;
|
|
|
|
|
var id = secret.channel;
|
|
|
|
|
var hash, secret, key, id, href;
|
|
|
|
|
|
|
|
|
|
var getNewHash = function () {
|
|
|
|
|
hash = Hash.createRandomHash('file', password);
|
|
|
|
|
secret = Hash.getSecrets('file', hash, password);
|
|
|
|
|
key = secret.keys.cryptKey;
|
|
|
|
|
id = secret.channel;
|
|
|
|
|
href = '/file/#' + hash;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// XXX check id here (getFileSize)
|
|
|
|
|
var getValidHash = function (cb) {
|
|
|
|
|
getNewHash();
|
|
|
|
|
common.getFileSize(href, password, function (err, size) {
|
|
|
|
|
if (err || typeof(size) !== "number") { throw new Error(err || "Invalid size!"); }
|
|
|
|
|
if (size === 0) { return void cb(); }
|
|
|
|
|
getValidHash();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
nThen(function (waitFor) {
|
|
|
|
|
// Generate a hash and check if the resulting id is valid (not already used)
|
|
|
|
|
getValidHash(waitFor());
|
|
|
|
|
}).nThen(function () {
|
|
|
|
|
var next = FileCrypto.encrypt(u8, metadata, key);
|
|
|
|
|
|
|
|
|
|
var estimate = FileCrypto.computeEncryptedSize(u8.length, metadata);
|
|
|
|
@ -56,7 +73,6 @@ define([
|
|
|
|
|
var uri = ['', 'blob', id.slice(0,2), id].join('/');
|
|
|
|
|
console.log("encrypted blob is now available as %s", uri);
|
|
|
|
|
|
|
|
|
|
var href = '/file/#' + hash;
|
|
|
|
|
|
|
|
|
|
var title = metadata.name;
|
|
|
|
|
|
|
|
|
@ -97,6 +113,8 @@ define([
|
|
|
|
|
}
|
|
|
|
|
next(again);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
return module;
|
|
|
|
|
});
|
|
|
|
|