From d05dd416caa9953cbc1a6ec5d63fa8e7fb8d8a09 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Wed, 20 Nov 2024 18:36:32 +0530 Subject: [PATCH] Fwd --- web/packages/base/crypto/index.ts | 46 ++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/web/packages/base/crypto/index.ts b/web/packages/base/crypto/index.ts index 615ee362ac..f3f69e4565 100644 --- a/web/packages/base/crypto/index.ts +++ b/web/packages/base/crypto/index.ts @@ -50,7 +50,12 @@ import { type StateAddress } from "libsodium-wrappers-sumo"; import { assertionFailed } from "../assert"; import { inWorker } from "../env"; import * as ei from "./ente-impl"; -import type { BytesOrB64, EncryptedBlob, EncryptedBox } from "./types"; +import type { + BytesOrB64, + EncryptedBlob, + EncryptedBox, + EncryptedFile, +} from "./types"; import type { CryptoWorker } from "./worker"; /** @@ -192,13 +197,13 @@ export const initChunkEncryption = async (key: BytesOrB64) => */ export const encryptStreamChunk = async ( data: Uint8Array, - pushState: StateAddress, + state: StateAddress, isFinalChunk: boolean, ) => inWorker() - ? ei._encryptStreamChunk(data, pushState, isFinalChunk) + ? ei._encryptStreamChunk(data, state, isFinalChunk) : sharedCryptoWorker().then((w) => - w.encryptStreamChunk(data, pushState, isFinalChunk), + w.encryptStreamChunk(data, state, isFinalChunk), ); /** @@ -281,6 +286,39 @@ export const decryptThumbnail = (blob: EncryptedBlob, key: BytesOrB64) => ? ei._decryptThumbnail(blob, key) : sharedCryptoWorker().then((w) => w.decryptThumbnail(blob, key)); +/** + * Decrypt the result of {@link encryptStreamBytes}. + */ +export const decryptStreamBytes = async ( + file: EncryptedFile, + key: BytesOrB64, +) => + inWorker() + ? ei._decryptStreamBytes(file, key) + : sharedCryptoWorker().then((w) => w.decryptStreamBytes(file, key)); + +/** + * Prepare to decrypt the encrypted result produced using {@link initChunkEncryption} and + * {@link encryptStreamChunk}. + */ +export const initChunkDecryption = async (header: string, key: BytesOrB64) => + inWorker() + ? ei._initChunkDecryption(header, key) + : sharedCryptoWorker().then((w) => w.initChunkDecryption(header, key)); + +/** + * Decrypt an individual chunk produced by {@link encryptStreamChunk}. + * + * This function is used in tandem with {@link initChunkDecryption}. + */ +export const decryptStreamChunk = async ( + data: Uint8Array, + state: StateAddress, +) => + inWorker() + ? ei._decryptStreamChunk(data, state) + : sharedCryptoWorker().then((w) => w.decryptStreamChunk(data, state)); + /** * Decrypt the metadata JSON encrypted using {@link encryptMetadataJSON}. *