Skip to content

armgit / ente

Public Imported from GitHub
0

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

Cleanup

Manav Rathi890ed7dd4b9a
diff --git a/web/packages/base/crypto/ente-impl.ts b/web/packages/base/crypto/ente-impl.ts
index d6b395f758..71ecc88b7a 100644
--- a/web/packages/base/crypto/ente-impl.ts
+++ b/web/packages/base/crypto/ente-impl.ts
@@ -1,12 +1,10 @@
/** Careful when adding add other imports! */
import * as libsodium from "./libsodium";
import type {
- BytesOrB64,
DecryptBlobB64,
EncryptBytes,
EncryptedBlobB64,
EncryptedBlobBytes,
- EncryptedBox2,
EncryptJSON,
} from "./types";
@@ -33,10 +31,7 @@ export const _encryptMetadataJSON = ({ jsonValue, keyB64 }: EncryptJSON) =>
keyB64,
});
-export const _decryptBox = libsodium.decryptBox2;
-
-export const _decryptBoxB64 = (b: EncryptedBox2, k: BytesOrB64) =>
- _decryptBox(b, k).then(libsodium.toB64);
+export const _decryptBoxB64 = libsodium.decryptBoxB64;
export const _decryptAssociatedData = libsodium.decryptBlob;
diff --git a/web/packages/base/crypto/ente.ts b/web/packages/base/crypto/ente.ts
index db560d7bbd..afd7ceb606 100644
--- a/web/packages/base/crypto/ente.ts
+++ b/web/packages/base/crypto/ente.ts
@@ -64,9 +64,9 @@ import type {
* Some of these functions have not yet been needed on the main thread, and for
* these we don't have a corresponding sharedCryptoWorker method.
*
- * This assertion will let us know when we need to implement them (this'll
+ * This assertion will let us know when we need to implement them. This will
* gracefully degrade in production: the functionality will work, just that the
- * crypto will happen on the main thread itself).
+ * crypto operations will happen on the main thread itself.
*/
const assertInWorker = <T>(x: T): T => {
if (!inWorker()) assertionFailed("Currently only usable in a web worker");
@@ -74,12 +74,12 @@ const assertInWorker = <T>(x: T): T => {
};
/**
- * Encrypt the given data, returning a box containing encrypted data and a
- * randomly generated nonce.
+ * Encrypt the given data, returning a box containing the encrypted data and a
+ * randomly generated nonce that was used during encryption.
*
- * Use {@link decryptBoxB64} to decrypt the result.
+ * Both the encrypted data and the nonce are returned as base64 strings.
*
- * ee {@link encryptBox} for the implementation details.
+ * Use {@link decryptBoxB64} to decrypt the result.
*
* > The suffix "Box" comes from the fact that it uses the so called secretbox
* > APIs provided by libsodium under the hood.
@@ -142,16 +142,8 @@ export const encryptMetadataJSON = async (r: EncryptJSON) =>
: sharedCryptoWorker().then((w) => w.encryptMetadataJSON(r));
/**
- * Decrypt a box encrypted using {@link encryptBox}.
- */
-export const decryptBox = (b: EncryptedBox2, k: BytesOrB64) =>
- inWorker()
- ? ei._decryptBox(b, k)
- : sharedCryptoWorker().then((w) => w.decryptBox(b, k));
-
-/**
- * Variant of {@link decryptBox} that returns the decrypted data as a base64
- * string.
+ * Decrypt a box encrypted using {@link encryptBoxB64}, and return the result as
+ * a base64 string.
*/
export const decryptBoxB64 = (box: EncryptedBox2, key: BytesOrB64) =>
inWorker()
diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.ts
index f1b537e62e..2937d1a7ba 100644
--- a/web/packages/base/crypto/libsodium.ts
+++ b/web/packages/base/crypto/libsodium.ts
@@ -378,9 +378,14 @@ export const decryptBox = async ({
};
/**
- * Decrypt the result of {@link encryptBox}.
+ * Decrypt the result of {@link encryptBoxB64}.
*/
-export const decryptBox2 = async (
+export const decryptBoxB64 = (
+ box: EncryptedBox2,
+ key: BytesOrB64,
+): Promise<string> => _decryptBox(box, key).then(toB64);
+
+export const _decryptBox = async (
{ encryptedData, nonce }: EncryptedBox2,
key: BytesOrB64,
): Promise<Uint8Array> => {
diff --git a/web/packages/base/crypto/types.ts b/web/packages/base/crypto/types.ts
index 0824fa7286..8795267338 100644
--- a/web/packages/base/crypto/types.ts
+++ b/web/packages/base/crypto/types.ts
@@ -1,10 +1,11 @@
/**
- * Data provided either as bytes ({@link Uint8Array}) or their base64 string representation.
+ * Data provided either as bytes ({@link Uint8Array}) or their base64 string
+ * representation.
*/
export type BytesOrB64 = Uint8Array | string;
/**
- * An encryption request
+ * An encryption request.
*/
export interface EncryptBytesOrB64 {
/**
diff --git a/web/packages/base/crypto/worker.ts b/web/packages/base/crypto/worker.ts
index 13e36c546d..72ae1fb332 100644
--- a/web/packages/base/crypto/worker.ts
+++ b/web/packages/base/crypto/worker.ts
@@ -15,7 +15,6 @@ export class CryptoWorker {
encryptBoxB64 = ei._encryptBoxB64;
encryptThumbnail = ei._encryptThumbnail;
encryptMetadataJSON = ei._encryptMetadataJSON;
- decryptBox = ei._decryptBox;
decryptBoxB64 = ei._decryptBoxB64;
decryptThumbnail = ei._decryptThumbnail;
decryptAssociatedDataB64 = ei._decryptAssociatedDataB64;