123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- importScripts('../deps/sjcl.min.js')
- function parametersfrombits(seed) {
- var out = sjcl.hash.sha512.hash(seed)
- return {
- 'seed': seed,
- 'key': sjcl.bitArray.bitSlice(out, 0, 256),
- 'iv': sjcl.bitArray.bitSlice(out, 256, 384),
- 'ident': sjcl.bitArray.bitSlice(out, 384, 512)
- }
- }
- function parameters(seed) {
- if (typeof seed == 'string') {
- seed = sjcl.codec.base64url.toBits(seed)
- } else {
- seed = sjcl.codec.bytes.toBits(seed)
- }
- return parametersfrombits(seed)
- }
- function encrypt(file, seed, id) {
- var params = parameters(seed)
- var uarr = new Uint8Array(file)
- //
- // why encrypt twice, safenet-js will handle this
- //
- //var before = sjcl.codec.bytes.toBits(uarr)
- //var prp = new sjcl.cipher.aes(params.key)
- //var after = sjcl.mode.ccm.encrypt(prp, before, params.iv)
- //var afterarray = new Uint8Array(sjcl.codec.bytes.fromBits(after))
-
- //console.log ( 'uarr : ', uarr );
-
- postMessage({
- 'id': id,
- 'seed': sjcl.codec.base64url.fromBits(params.seed),
- 'ident': sjcl.codec.base64url.fromBits(params.ident),
- //'encrypted': new Blob([afterarray], { type: 'application/octet-stream' })
- 'encrypted': uarr
- })
- }
- var fileheader = [
- 85, 80, 49, 0
- ]
- function decrypt(file, seed, id) {
- var params = parameters(seed)
- var uarr = new Uint8Array(file)
-
- //console.log ( 'decrypt : file : ', uarr );
- // We support the servers jamming a header in to deter direct linking
- var hasheader = true
- for (var i = 0; i < fileheader.length; i++) {
- if (uarr[i] != fileheader[i]) {
- hasheader = false
- break
- }
- }
- if (hasheader) {
- uarr = uarr.subarray(fileheader.length)
- }
- //
- // why encrypt twice, safenet-js will handle this
- //
- //var before = sjcl.codec.bytes.toBits(uarr);
- //var prp = new sjcl.cipher.aes(params.key);
- //var after = sjcl.mode.ccm.decrypt(prp, before, params.iv);
- //var afterarray = new Uint8Array(sjcl.codec.bytes.fromBits(after));
- // Parse the header, which is a null-terminated UTF-16 string containing JSON
- var header = ''
- //var headerview = new DataView(afterarray.buffer)
- var headerview = new DataView(uarr.buffer)
- var i = 0;
- for (; ; i++) {
- var num = headerview.getUint16(i * 2, false)
- if (num == 0) {
- break;
- }
- header += String.fromCharCode(num);
- }
- var header = JSON.parse(header)
- //var data = new Blob([afterarray])
- var data = new Blob([uarr])
- postMessage({
- 'id': id,
- 'ident': sjcl.codec.base64url.fromBits(params.ident),
- 'header': header,
- 'decrypted': data.slice((i * 2) + 2, data.size, header.mime)
- })
- }
- function ident(seed, id) {
- var params = parameters(seed)
- postMessage({
- 'id': id,
- 'ident': sjcl.codec.base64url.fromBits(params.ident)
- })
- }
- function onprogress(id, progress) {
- postMessage({
- 'id': id,
- 'eventsource': 'encrypt',
- 'loaded': progress,
- 'total': 1,
- 'type': 'progress'
- })
- }
- onmessage = function (e) {
- var progress = onprogress.bind(undefined, e.data.id)
- sjcl.mode.ccm.listenProgress(progress)
- if (e.data.action == 'decrypt') {
- decrypt(e.data.data, e.data.seed, e.data.id)
- } else if (e.data.action == 'ident') {
- ident(e.data.seed, e.data.id)
- } else {
- sjcl.random.addEntropy(e.data.entropy, 2048, 'runtime')
- encrypt(e.data.data, e.data.seed, e.data.id)
- }
- sjcl.mode.ccm.unListenProgress(progress)
- }
|