Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Tweak

Manav Rathid409935cebf1
diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.ts
index 2e125ba093..e81514d2f5 100644
--- a/web/packages/base/crypto/libsodium.ts
+++ b/web/packages/base/crypto/libsodium.ts
@@ -10,19 +10,20 @@
*/
import { mergeUint8Arrays } from "ente-utils/array";
import sodium from "libsodium-wrappers-sumo";
-import type {
- BytesOrB64,
- DerivedKey,
- EncryptedBlob,
- EncryptedBlobB64,
- EncryptedBlobBytes,
- EncryptedBox,
- EncryptedBoxB64,
- EncryptedFile,
- InitChunkDecryptionResult,
- InitChunkEncryptionResult,
- KeyPair,
- SodiumStateAddress,
+import {
+ streamEncryptionChunkSize,
+ type BytesOrB64,
+ type DerivedKey,
+ type EncryptedBlob,
+ type EncryptedBlobB64,
+ type EncryptedBlobBytes,
+ type EncryptedBox,
+ type EncryptedBoxB64,
+ type EncryptedFile,
+ type InitChunkDecryptionResult,
+ type InitChunkEncryptionResult,
+ type KeyPair,
+ type SodiumStateAddress,
} from "./types";
/**
@@ -350,22 +351,12 @@ export const encryptBlob = async (
* Use {@link decryptMetadataJSON} to decrypt the result and convert it back to
* a JSON value.
*/
-export const encryptMetadataJSON = (jsonValue: unknown, key: BytesOrB64) =>
+export const encryptMetadataJSON = (
+ jsonValue: unknown,
+ key: BytesOrB64,
+): Promise<EncryptedBlobB64> =>
encryptBlob(new TextEncoder().encode(JSON.stringify(jsonValue)), key);
-/**
- * The various *Stream encryption functions break up the input into chunks of
- * {@link streamEncryptionChunkSize} bytes during encryption (except the last
- * chunk which can be smaller since a file would rarely align exactly to a
- * {@link streamEncryptionChunkSize} multiple).
- *
- * The various *Stream decryption functions also assume that each potential
- * chunk is {@link streamEncryptionChunkSize} long.
- *
- * This value of this constant is 4 MB (and is unlikely to change).
- */
-export const streamEncryptionChunkSize = 4 * 1024 * 1024;
-
/**
* Encrypt the given data using libsodium's secretstream APIs after breaking it
* into {@link streamEncryptionChunkSize} chunks.
@@ -548,7 +539,7 @@ export const decryptBlob = (
export const decryptMetadataJSON = async (
blob: EncryptedBlob,
key: BytesOrB64,
-) =>
+): Promise<unknown> =>
JSON.parse(
new TextDecoder().decode(await decryptBlobBytes(blob, key)),
) as unknown;
diff --git a/web/packages/base/crypto/types.ts b/web/packages/base/crypto/types.ts
index 0ceb9be1a2..2e20f5978b 100644
--- a/web/packages/base/crypto/types.ts
+++ b/web/packages/base/crypto/types.ts
@@ -6,6 +6,19 @@ import { type StateAddress } from "libsodium-wrappers-sumo";
*/
export type SodiumStateAddress = StateAddress;
+/**
+ * The various *Stream encryption functions break up the input into chunks of
+ * {@link streamEncryptionChunkSize} bytes during encryption (except the last
+ * chunk which can be smaller since a file would rarely align exactly to a
+ * {@link streamEncryptionChunkSize} multiple).
+ *
+ * The various *Stream decryption functions also assume that each potential
+ * chunk is {@link streamEncryptionChunkSize} long.
+ *
+ * This value of this constant is 4 MB (and is unlikely to change).
+ */
+export const streamEncryptionChunkSize = 4 * 1024 * 1024;
+
/**
* Data provided either as bytes ({@link Uint8Array}) or their base64 string
* representation.
diff --git a/web/packages/gallery/services/upload/upload-service.ts b/web/packages/gallery/services/upload/upload-service.ts
index dd00f3cca7..1e71f1bf1a 100644
--- a/web/packages/gallery/services/upload/upload-service.ts
+++ b/web/packages/gallery/services/upload/upload-service.ts
@@ -1,8 +1,8 @@
// TODO: Audit this file
/* eslint-disable @typescript-eslint/ban-ts-comment */
-import { streamEncryptionChunkSize } from "ente-base/crypto/libsodium";
import type { BytesOrB64 } from "ente-base/crypto/types";
+import { streamEncryptionChunkSize } from "ente-base/crypto/types";
import { type CryptoWorker } from "ente-base/crypto/worker";
import { ensureElectron } from "ente-base/electron";
import { basename, nameAndExtension } from "ente-base/file-name";