Skip to content

Commit

Permalink
saves stream to file
Browse files Browse the repository at this point in the history
  • Loading branch information
emily-hou1 committed Jul 6, 2018
1 parent 62ed0a4 commit f98bc08
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 201 deletions.
19 changes: 9 additions & 10 deletions app/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { arrayToB64, b64ToArray, delay } from './utils';
import { ReadableStream as PolyRS} from 'web-streams-polyfill';
import { ReadableStream as PolyRS } from 'web-streams-polyfill';
import { createReadableStreamWrapper } from '@mattiasbuelens/web-streams-adapter';
const RS = createReadableStreamWrapper(PolyRS);

Expand Down Expand Up @@ -202,9 +202,10 @@ export function uploadWs(encrypted, info, metadata, verifierB64, onprogress) {

////////////////////////

async function downloadS(id, keychain, onprogress, signal) {
async function downloadS(id, keychain, signal) {
const auth = await keychain.authHeader();

//this will be already funneled through serviceworker
const response = await fetch(`/api/download/${id}`, {
signal: signal,
method: 'GET',
Expand All @@ -223,22 +224,20 @@ async function downloadS(id, keychain, onprogress, signal) {
const fileSize = response.headers.get('Content-Length');

//right now only chrome allows obtaining a stream from fetch
//for other browsers we fetch as a blob and convert to polyfill stream later
//for other browsers we fetch as a blob and convert to polyfill stream later
if (response.body) {
console.log("STREAM")
return RS(response.body);
}
return response.blob();

}

async function tryDownloadStream(id, keychain, onprogress, signal, tries = 1) {
async function tryDownloadStream(id, keychain, signal, tries = 1) {
try {
const result = await downloadS(id, keychain, onprogress, signal);
const result = await downloadS(id, keychain, signal);
return result;
} catch (e) {
if (e.message === '401' && --tries > 0) {
return tryDownloadStream(id, keychain, onprogress, signal, tries);
return tryDownloadStream(id, keychain, signal, tries);
}
if (e.name === 'AbortError') {
throw new Error('0');
Expand All @@ -247,14 +246,14 @@ async function tryDownloadStream(id, keychain, onprogress, signal, tries = 1) {
}
}

export function downloadStream(id, keychain, onprogress) {
export function downloadStream(id, keychain) {
const controller = new AbortController();
function cancel() {
controller.abort();
}
return {
cancel,
result: tryDownloadStream(id, keychain, onprogress, controller.signal, 2)
result: tryDownloadStream(id, keychain, controller.signal, 2)
};
}

Expand Down
62 changes: 33 additions & 29 deletions app/ece.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
require('buffer');
import { TransformStream as PolyTS, ReadableStream as PolyRS } from 'web-streams-polyfill';
import { createReadableStreamWrapper, createTransformStreamWrapper } from '@mattiasbuelens/web-streams-adapter';
/*
import {
TransformStream as PolyTS,
ReadableStream as PolyRS
} from 'web-streams-polyfill';
import {
createReadableStreamWrapper,
createTransformStreamWrapper
} from '@mattiasbuelens/web-streams-adapter';
const toTS = createTransformStreamWrapper(PolyTS);
const toRS = createReadableStreamWrapper(PolyRS);
*/

const NONCE_LENGTH = 12;
const TAG_LENGTH = 16;
Expand All @@ -15,7 +23,7 @@ const encoder = new TextEncoder();

function generateSalt(len) {
const randSalt = new Uint8Array(len);
window.crypto.getRandomValues(randSalt);
crypto.getRandomValues(randSalt);
return randSalt.buffer;
}

Expand All @@ -31,15 +39,15 @@ class ECETransformer {
}

async generateKey() {
const inputKey = await window.crypto.subtle.importKey(
const inputKey = await crypto.subtle.importKey(
'raw',
this.ikm,
'HKDF',
false,
['deriveKey']
);

return window.crypto.subtle.deriveKey(
return crypto.subtle.deriveKey(
{
name: 'HKDF',
salt: this.salt,
Expand All @@ -57,17 +65,17 @@ class ECETransformer {
}

async generateNonceBase() {
const inputKey = await window.crypto.subtle.importKey(
const inputKey = await crypto.subtle.importKey(
'raw',
this.ikm,
'HKDF',
false,
['deriveKey']
);

const base = await window.crypto.subtle.exportKey(
const base = await crypto.subtle.exportKey(
'raw',
await window.crypto.subtle.deriveKey(
await crypto.subtle.deriveKey(
{
name: 'HKDF',
salt: this.salt,
Expand Down Expand Up @@ -156,7 +164,7 @@ class ECETransformer {

async encryptRecord(buffer, seq, isLast) {
const nonce = this.generateNonce(seq);
const encrypted = await window.crypto.subtle.encrypt(
const encrypted = await crypto.subtle.encrypt(
{ name: 'AES-GCM', iv: nonce },
this.key,
this.pad(buffer, isLast)
Expand All @@ -166,7 +174,7 @@ class ECETransformer {

async decryptRecord(buffer, seq, isLast) {
const nonce = this.generateNonce(seq);
const data = await window.crypto.subtle.decrypt(
const data = await crypto.subtle.decrypt(
{
name: 'AES-GCM',
iv: nonce,
Expand Down Expand Up @@ -266,7 +274,7 @@ class StreamSlicer {
constructor(rs, mode) {
this.mode = mode;
this.rs = rs;
this.chunkSize = (mode === MODE_ENCRYPT) ? (rs - 17) : 21;
this.chunkSize = mode === MODE_ENCRYPT ? rs - 17 : 21;
this.partialChunk = new Uint8Array(this.chunkSize); //where partial chunks are saved
this.offset = 0;
}
Expand All @@ -285,7 +293,7 @@ class StreamSlicer {
let i = 0;

if (this.offset > 0) {
const len = Math.min(chunk.byteLength, (this.chunkSize - this.offset));
const len = Math.min(chunk.byteLength, this.chunkSize - this.offset);
this.partialChunk.set(chunk.slice(0, len), this.offset);
this.offset += len;
i += len;
Expand All @@ -297,7 +305,7 @@ class StreamSlicer {
}

while (i < chunk.byteLength) {
if ((chunk.byteLength - i) >= this.chunkSize) {
if (chunk.byteLength - i >= this.chunkSize) {
const record = chunk.slice(i, i + this.chunkSize);
i += this.chunkSize;
this.send(record, controller);
Expand All @@ -318,17 +326,6 @@ class StreamSlicer {
}
}

async function stream2blob(stream) {
const chunks = [];
const reader = stream.getReader();
let state = await reader.read();
while (!state.done) {
chunks.push(state.value);
state = await reader.read();
}
return new Blob(chunks);
}

/*
input: a blob or a ReadableStream containing data to be transformed
key: Uint8Array containing key of size KEY_LENGTH
Expand All @@ -354,21 +351,28 @@ export default class ECE {
info() {
return {
recordSize: this.rs,
fileSize: 21 + this.input.size + 16 * Math.floor(this.input.size / (this.rs - 17))
fileSize:
21 + this.input.size + 16 * Math.floor(this.input.size / (this.rs - 17))
};
}

transform() {
let inputStream;

if (this.input instanceof Blob) {
inputStream = toRS(new ReadableStream(new BlobSlicer(this.input, this.rs, this.mode)));
inputStream = new ReadableStream(
new BlobSlicer(this.input, this.rs, this.mode)
); //inputStream = toRS(new ReadableStream(new BlobSlicer(this.input, this.rs, this.mode)));
} else {
const sliceStream = toTS(new TransformStream(new StreamSlicer(this.rs, this.mode)));
const sliceStream = new TransformStream(
new StreamSlicer(this.rs, this.mode)
); //const sliceStream = toTS(new TransformStream(new StreamSlicer(this.rs, this.mode)));
inputStream = this.input.pipeThrough(sliceStream);
}

const cryptoStream = toTS(new TransformStream(new ECETransformer(this.mode, this.key, this.rs, this.salt)));
return inputStream.pipeThrough(cryptoStream);
const cryptoStream = new TransformStream(
new ECETransformer(this.mode, this.key, this.rs, this.salt)
); //const cryptoStream = toTS(new TransformStream(new ECETransformer(this.mode, this.key, this.rs, this.salt)));
return inputStream.pipeThrough(cryptoStream); //return toRS(inputStream.pipeThrough(cryptoStream));
}
}
13 changes: 1 addition & 12 deletions app/fileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ export default function(state, emitter) {
}
}

function register() {
navigator.serviceWorker.register('/serviceWorker.js')
.then( reg => console.log("registration successful or already installed"))
.catch( e => console.log(e) );
}

function updateProgress() {
if (updateTitle) {
emitter.emit('DOMTitleChange', percent(state.transfer.progressRatio));
Expand Down Expand Up @@ -156,6 +150,7 @@ export default function(state, emitter) {

emitter.on('getMetadata', async () => {
const file = state.fileInfo;

const receiver = new FileReceiver(file);
try {
await receiver.getMetadata();
Expand All @@ -169,12 +164,6 @@ export default function(state, emitter) {
}
}

const info = {
key: file.secretKey,
nonce: file.nonce
}
navigator.serviceWorker.controller.postMessage(info);

render();
});

Expand Down
Loading

0 comments on commit f98bc08

Please sign in to comment.