Skip to content

armgit / ente

Public Imported from GitHub
0

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

Fwd

Manav Rathi462aae8f3432
diff --git a/web/packages/base/crypto/ente-impl.ts b/web/packages/base/crypto/ente-impl.ts
index c538d05987..3ec10d2e12 100644
--- a/web/packages/base/crypto/ente-impl.ts
+++ b/web/packages/base/crypto/ente-impl.ts
@@ -1,6 +1,5 @@
/** Careful when adding add other imports! */
import * as libsodium from "./libsodium";
-import type { BytesOrB64, EncryptedBlob } from "./types";
export const _toB64 = libsodium.toB64;
@@ -22,15 +21,14 @@ export const _encryptBlob = libsodium.encryptBlob;
export const _encryptBlobBytes = libsodium.encryptBlobBytes;
+export const _encryptMetadataJSON = libsodium.encryptMetadataJSON;
+
export const _encryptStreamBytes = libsodium.encryptStreamBytes;
export const _initChunkEncryption = libsodium.initChunkEncryption;
export const _encryptStreamChunk = libsodium.encryptStreamChunk;
-export const _encryptMetadataJSON = (jsonValue: unknown, key: BytesOrB64) =>
- _encryptBlob(new TextEncoder().encode(JSON.stringify(jsonValue)), key);
-
export const _decryptBoxBytes = libsodium.decryptBoxBytes;
export const _decryptBox = libsodium.decryptBox;
@@ -39,20 +37,14 @@ export const _decryptBlobBytes = libsodium.decryptBlobBytes;
export const _decryptBlob = libsodium.decryptBlob;
+export const _decryptMetadataJSON = libsodium.decryptMetadataJSON;
+
export const _decryptStreamBytes = libsodium.decryptStreamBytes;
export const _initChunkDecryption = libsodium.initChunkDecryption;
export const _decryptStreamChunk = libsodium.decryptStreamChunk;
-export const _decryptMetadataJSON = async (
- blob: EncryptedBlob,
- key: BytesOrB64,
-) =>
- JSON.parse(
- new TextDecoder().decode(await _decryptBlobBytes(blob, key)),
- ) as unknown;
-
export const _chunkHashInit = libsodium.chunkHashInit;
export const _chunkHashUpdate = libsodium.chunkHashUpdate;
diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.ts
index 996c3d6c67..aacb263a8c 100644
--- a/web/packages/base/crypto/libsodium.ts
+++ b/web/packages/base/crypto/libsodium.ts
@@ -338,6 +338,16 @@ export const encryptBlob = async (
};
};
+/**
+ * Encrypt the provided JSON value (using {@link encryptBlob}) after converting
+ * it to a JSON string (and utf-8 encoding it to obtain bytes).
+ *
+ * Use {@link decryptMetadataJSON} to decrypt the result and convert it back to
+ * a JSON value.
+ */
+export const encryptMetadataJSON = (jsonValue: unknown, key: BytesOrB64) =>
+ 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
@@ -520,6 +530,22 @@ export const decryptBlob = (
key: BytesOrB64,
): Promise<string> => decryptBlobBytes(blob, key).then(toB64);
+/**
+ * Decrypt the result of {@link encryptMetadataJSON} and return the JSON value
+ * obtained by parsing the decrypted JSON string (which is obtained by utf-8
+ * decoding the decrypted bytes).
+ *
+ * Since TypeScript doesn't currently have a native JSON type, the returned
+ * value is casted as an `unknown`.
+ */
+export const decryptMetadataJSON = async (
+ blob: EncryptedBlob,
+ key: BytesOrB64,
+) =>
+ JSON.parse(
+ new TextDecoder().decode(await decryptBlobBytes(blob, key)),
+ ) as unknown;
+
/**
* Decrypt the result of {@link encryptStreamBytes}.
*/