up1/client/js/loadencryption.js

114 lines
2.2 KiB
JavaScript
Raw Normal View History

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