Boris K
Boris K

Reputation: 3580

How can I pipe streams in the browser to decrypt an encrypted file?

I am retrieving a file from IPFS and storing it as an array buffer in the browser. I want to decrypt it in a stream and save it locally. During storage, I encrypted it like this:

    const iv = crypto.randomBytes(16);
    const cipher = crypto.createCipheriv('aes-256-ctr', key, iv, null);
    const input = fs.createReadStream(inputFilePath);
    const storage = makeStorageClient();
    const keyString = new Int32Array(key).toString();
    const cid = await storage.put([{ name: fileName, stream: () => input.pipe(cipher) }]);

Now, I want to do the reverse in the browser.

I can't access the crypto module in the browser as far as I can tell, otherwise I would do this:

        const retrieved = await ipfs.cat(cid+'/'+link.name);
        const decipher = crypto.createDecipheriv('aes-256-ctr', key, iv, null);
        const input = new ReadableStream(retrieved);
        const output = streamSaver.createWriteStream(link.name);
        const finished = await input.pipe(decipher).pipe(output);

SubtleCrypto.decrypt(algorithm, key, data) seems promising, but to decrypt aes-256-ctr, I need to pass a params object into it. This object requires a counter and length. Does anyone know how I can get those? Is there a better way to decrypt in a stream in the browser?

Upvotes: 0

Views: 258

Answers (0)

Related Questions