fox-send/public/file.js

149 lines
4.4 KiB
JavaScript
Raw Normal View History

2017-05-30 16:24:16 +00:00
function download() {
2017-05-30 16:24:16 +00:00
var xhr = new XMLHttpRequest();
xhr.open('get', '/assets' + location.pathname.slice(0, -1), true);
2017-05-30 16:24:16 +00:00
xhr.responseType = 'blob';
xhr.onload = function(e) {
if (this.status == 200) {
let self = this;
var blob = new Blob([this.response]);
var arrayBuffer;
var fileReader = new FileReader();
fileReader.onload = function() {
arrayBuffer = this.result;
var array = new Uint8Array(arrayBuffer);
salt = strToIv(location.pathname.slice(10, -1));
2017-05-30 21:41:31 +00:00
2017-05-30 16:24:16 +00:00
window.crypto.subtle.importKey(
2017-05-30 21:41:31 +00:00
"jwk",
{
2017-05-30 16:24:16 +00:00
kty: "oct",
k: location.hash.slice(1),
2017-05-30 16:24:16 +00:00
alg: "A128CBC",
ext: true,
},
2017-05-30 21:41:31 +00:00
{
2017-05-30 16:24:16 +00:00
name: "AES-CBC",
},
2017-05-30 21:41:31 +00:00
true,
["encrypt", "decrypt"]
2017-05-30 16:24:16 +00:00
)
2017-05-30 21:41:31 +00:00
.then(function(key){
2017-05-30 16:24:16 +00:00
window.crypto.subtle.decrypt(
{
name: "AES-CBC",
2017-05-30 21:41:31 +00:00
iv: salt,
2017-05-30 16:24:16 +00:00
},
2017-05-30 21:41:31 +00:00
key,
array
2017-05-30 16:24:16 +00:00
)
.then(function(decrypted){
var dataView = new DataView(decrypted);
var blob = new Blob([dataView]);
var downloadUrl = URL.createObjectURL(blob);
var a = document.createElement("a");
a.href = downloadUrl;
a.download = xhr.getResponseHeader('Content-Disposition').match(/filename="(.+)"/)[1];
console.log(xhr.getResponseHeader('Content-Disposition'));
2017-05-30 16:24:16 +00:00
document.body.appendChild(a);
a.click();
})
.catch(function(err){
alert('This link is either invalid or has expired.');
2017-05-30 16:24:16 +00:00
console.error(err);
});
})
.catch(function(err){
console.error(err);
});
};
fileReader.readAsArrayBuffer(blob);
} else {
alert('Unable to download excel.')
}
};
xhr.send();
}
function onChange(event) {
var file = event.target.files[0];
var reader = new FileReader();
reader.onload = function(event) {
let self = this;
window.crypto.subtle.generateKey({
name: "AES-CBC",
length: 128
},
2017-05-30 21:41:31 +00:00
true,
2017-05-30 16:24:16 +00:00
["encrypt", "decrypt"])
.then(function(key){
var arrayBuffer = self.result;
var array = new Uint8Array(arrayBuffer);
var random_iv = window.crypto.getRandomValues(new Uint8Array(16));
window.crypto.subtle.encrypt({
name: "AES-CBC",
2017-05-30 21:41:31 +00:00
iv: random_iv },
key,
array)
2017-05-30 16:24:16 +00:00
.then(function(encrypted){
console.log('Send this salt to a friend: [' + random_iv.toString() + ']');
2017-05-30 21:41:31 +00:00
2017-05-30 16:24:16 +00:00
var dataView = new DataView(encrypted);
var blob = new Blob([dataView], { type: file.type });
2017-05-30 21:41:31 +00:00
2017-05-30 16:24:16 +00:00
var fd = new FormData();
fd.append('fname', file.name);
fd.append('data', blob, file.name);
2017-05-30 21:41:31 +00:00
2017-05-30 16:24:16 +00:00
var xhr = new XMLHttpRequest();
var hex = ivToStr(random_iv);
xhr.open('post', '/upload/' + hex, true);
2017-05-30 16:24:16 +00:00
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
window.crypto.subtle.exportKey("jwk", key).then(function(keydata){
console.log('Go to this URL: http://localhost:3000/download/' + hex + '/#' + keydata.k);
2017-05-30 21:41:31 +00:00
alert('Go to this URL: http://localhost:3000/download/' + hex + '/#' + keydata.k);
})
2017-05-30 16:24:16 +00:00
}
};
xhr.send(fd);
})
.catch(function(err){
console.error(err);
});
})
.catch(function(err){
console.error(err);
});
};
reader.readAsArrayBuffer(file);
}
function ivToStr(iv) {
let hexStr = '';
for (var i in iv) {
if (iv[i] < 16) {
hexStr += '0' + iv[i].toString(16);
} else {
hexStr += iv[i].toString(16);
}
}
window.hexStr = hexStr;
return hexStr;
}
function strToIv(str) {
var iv = new Uint8Array(16);
for (var i = 0; i < str.length; i += 2) {
iv[i/2] = parseInt((str.charAt(i) + str.charAt(i + 1)), 16);
}
return iv;
}