From 2884c0aedfc06696b4693d6534d7ea8bcc53dc5b Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Wed, 20 Nov 2024 18:27:56 +0530 Subject: [PATCH] Fwd --- web/packages/base/crypto/index.ts | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/web/packages/base/crypto/index.ts b/web/packages/base/crypto/index.ts index d267670a71..615ee362ac 100644 --- a/web/packages/base/crypto/index.ts +++ b/web/packages/base/crypto/index.ts @@ -46,6 +46,7 @@ * of going through this file. */ import { ComlinkWorker } from "@/base/worker/comlink-worker"; +import { type StateAddress } from "libsodium-wrappers-sumo"; import { assertionFailed } from "../assert"; import { inWorker } from "../env"; import * as ei from "./ente-impl"; @@ -163,6 +164,43 @@ export const encryptThumbnail = (data: BytesOrB64, key: BytesOrB64) => ? ei._encryptThumbnail(data, key) : sharedCryptoWorker().then((w) => w.encryptThumbnail(data, key)); +/** + * Encrypt the given data using chunked streaming encryption, but process all + * the chunks in one go. + * + * For more details, see {@link encryptStreamBytes} in `libsodium.ts`. + */ +export const encryptStreamBytes = async (data: Uint8Array, key: BytesOrB64) => + inWorker() + ? ei._encryptStreamBytes(data, key) + : sharedCryptoWorker().then((w) => w.encryptStreamBytes(data, key)); + +/** + * Prepare for chunked streaming encryption using {@link encryptStreamChunk}. + * + * For more details, see {@link initChunkEncryption} in `libsodium.ts`. + */ +export const initChunkEncryption = async (key: BytesOrB64) => + inWorker() + ? ei._initChunkEncryption(key) + : sharedCryptoWorker().then((w) => w.initChunkEncryption(key)); + +/** + * Encrypt a chunk as part of a chunked streaming encryption. + * + * For more details, see {@link encryptStreamChunk} in `libsodium.ts`. + */ +export const encryptStreamChunk = async ( + data: Uint8Array, + pushState: StateAddress, + isFinalChunk: boolean, +) => + inWorker() + ? ei._encryptStreamChunk(data, pushState, isFinalChunk) + : sharedCryptoWorker().then((w) => + w.encryptStreamChunk(data, pushState, isFinalChunk), + ); + /** * Encrypt the JSON metadata associated with an Ente object (file, collection or * entity) using the object's key.