From 55ebb509c2062a2d2f5e5bba30421535c7382953 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Thu, 23 Jun 2022 19:59:57 +0530 Subject: [PATCH] proxy file hashing through comlink --- src/utils/crypto/libsodium.ts | 33 +++++++++++++++------------------ src/worker/crypto.worker.js | 12 ++++++++++++ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/utils/crypto/libsodium.ts b/src/utils/crypto/libsodium.ts index cb760caec..90138ab48 100644 --- a/src/utils/crypto/libsodium.ts +++ b/src/utils/crypto/libsodium.ts @@ -252,33 +252,30 @@ export async function hash(input: string) { ); } -export async function cryptoGenericHash(stream: ReadableStream) { +export async function initChunkHashing() { await sodium.ready; - - const state = sodium.crypto_generichash_init( + const hashState = sodium.crypto_generichash_init( null, sodium.crypto_generichash_BYTES_MAX ); + return hashState; +} - const reader = stream.getReader(); - - let isDone = false; - while (!isDone) { - const { done, value: chunk } = await reader.read(); - if (done) { - isDone = true; - break; - } - const buffer = Uint8Array.from(chunk); - sodium.crypto_generichash_update(state, buffer); - } +export async function hashFileChunk( + hashState: sodium.StateAddress, + chunk: Uint8Array +) { + await sodium.ready; + sodium.crypto_generichash_update(hashState, chunk); +} +export async function completeChunkHashing(hashState: sodium.StateAddress) { + await sodium.ready; const hash = sodium.crypto_generichash_final( - state, + hashState, sodium.crypto_generichash_BYTES_MAX ); - const hashString = sodium.to_base64(hash, sodium.base64_variants.ORIGINAL); - + const hashString = toB64(hash); return hashString; } diff --git a/src/worker/crypto.worker.js b/src/worker/crypto.worker.js index dc7803b00..f2bce17a6 100644 --- a/src/worker/crypto.worker.js +++ b/src/worker/crypto.worker.js @@ -76,6 +76,18 @@ export class Crypto { return libsodium.verifyHash(hash, input); } + async initChunkHashing() { + return libsodium.initChunkHashing(); + } + + async hashFileChunk(hashState, chunk) { + return libsodium.hashFileChunk(hashState, chunk); + } + + async completeChunkHashing(hashState) { + return libsodium.completeChunkHashing(hashState); + } + async deriveKey(passphrase, salt, opsLimit, memLimit) { return libsodium.deriveKey(passphrase, salt, opsLimit, memLimit); }