123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- window.crypt = {}
- var crypto = window.crypto || window.msCrypto;
- function getEntropy() {
- var entropy = new Uint32Array(256)
- crypto.getRandomValues(entropy)
- return entropy
- }
- function getSeed() {
- var seed = new Uint8Array(16)
- crypto.getRandomValues(seed)
- return seed
- }
- var worker = new Worker("./js/encryption.js")
- var promises = {}
- 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)
- }
- 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]
- }
- }
- var counter = 0
- function getpromise() {
- var promise = $.Deferred()
- var promiseid = counter
- counter += 1
- promise.id = promiseid
- promises[promiseid] = promise;
- return promise
- }
- crypt.encrypt = function (file, name) {
- var extension = file.type.split('/')
- var header = JSON.stringify({
- 'mime': file.type,
- 'name': name ? name : (file.name ? file.name : ('Pasted ' + extension[0] + '.' + (extension[1] == 'plain' ? 'txt' : extension[1])))
- })
- 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
- })
- }
- 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
- })
- }
- //console.log ( 'crypt.decrypt' );
- fr.readAsArrayBuffer(file)
- return promise
- }
|