up1/static/js/loadencryption.js

112 lines
2.5 KiB
JavaScript
Raw Normal View History

2015-06-06 02:02:44 +02:00
var crypt = {}
$(function () {
2015-06-11 08:36:34 +02:00
var crypto = window.crypto || window.msCrypto;
2015-06-06 02:02:44 +02:00
function getEntropy() {
var entropy = new Uint32Array(256)
2015-06-11 08:36:34 +02:00
crypto.getRandomValues(entropy)
2015-06-06 02:02:44 +02:00
return entropy
}
function getSeed() {
var seed = new Uint8Array(16)
2015-06-11 08:36:34 +02:00
crypto.getRandomValues(seed)
2015-06-06 02:02:44 +02:00
return seed
}
var worker = new Worker("/static/js/encryption.js")
var promises = {}
function str2ab(str) {
var buf = new ArrayBuffer(str.length * 2);
2015-06-06 04:29:56 +02:00
var bufView = new DataView(buf);
2015-06-06 02:02:44 +02:00
for (var i = 0, strLen = str.length; i < strLen; i++) {
2015-06-06 04:29:56 +02:00
bufView.setUint16(i * 2, str.charCodeAt(i), false)
2015-06-06 02:02:44 +02:00
}
return buf;
}
worker.onmessage = function (e) {
promises[e.data.id].resolve(e.data)
delete promises[e.data.id]
}
var counter = 0
function getpromise() {
var promise = $.Deferred()
var promiseid = counter
counter += 1
promise.id = promiseid
promises[promiseid] = promise;
return promise
}
2015-06-11 00:36:29 +02:00
crypt.encrypt = function (file, name) {
2015-06-06 02:02:44 +02:00
2015-06-06 05:16:03 +02:00
var extension = file.type.split('/')
2015-06-06 02:02:44 +02:00
var header = JSON.stringify({
'mime': file.type,
2015-06-11 00:36:29 +02:00
'name': name ? name : (file.name ? file.name : ('Pasted ' + extension[0] + '.' + (extension[1] == 'plain' ? 'txt' : extension[1])))
2015-06-06 02:02:44 +02:00
})
2015-06-06 05:05:09 +02:00
var zero = new Uint8Array([0, 0]);
2015-06-06 04:29:56 +02:00
2015-06-06 05:05:09 +02:00
var blob = new Blob([str2ab(header), zero, file])
2015-06-06 02:02:44 +02:00
var promise = getpromise()
var fr = new FileReader()
fr.onload = function () {
worker.postMessage({
'data': this.result,
'entropy': getEntropy(),
'seed': getSeed(),
'id': promise.id
})
}
fr.readAsArrayBuffer(blob)
return promise
}
crypt.ident = function (seed) {
var promise = getpromise()
worker.postMessage({
'seed': seed,
'action': 'ident',
'id': promise.id
})
return promise
}
crypt.decrypt = function (file, seed) {
var promise = getpromise()
var fr = new FileReader()
fr.onload = function () {
worker.postMessage({
'data': this.result,
'action': 'decrypt',
'seed': seed,
'id': promise.id
})
}
2015-06-11 08:36:34 +02:00
2015-06-06 02:02:44 +02:00
fr.readAsArrayBuffer(file)
return promise
}
})