Skip to content

armgit / ente

Public Imported from GitHub
0

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

Shorten

Manav Rathif9623ecf758c
diff --git a/web/packages/base/crypto/index.ts b/web/packages/base/crypto/index.ts
index 92dba8fbb9..bea8f82169 100644
--- a/web/packages/base/crypto/index.ts
+++ b/web/packages/base/crypto/index.ts
@@ -71,6 +71,9 @@ let _comlinkWorker: ComlinkWorker<typeof CryptoWorker> | undefined;
export const sharedCryptoWorker = async () =>
(_comlinkWorker ??= createComlinkCryptoWorker()).remote;
+/** A shorter alias of {@link sharedCryptoWorker} for use within this file. */
+const sharedWorker = sharedCryptoWorker;
+
/**
* Create a new instance of a comlink worker that wraps a {@link CryptoWorker}
* web worker.
@@ -102,7 +105,7 @@ const assertInWorker = <T>(x: T): T => {
export const generateBoxKey = () =>
inWorker()
? ei._generateBoxKey()
- : sharedCryptoWorker().then((w) => w.generateBoxKey());
+ : sharedWorker().then((w) => w.generateBoxKey());
/**
* Return a new randomly generated 256-bit key (as a base64 string) suitable for
@@ -111,7 +114,7 @@ export const generateBoxKey = () =>
export const generateBlobOrStreamKey = () =>
inWorker()
? ei._generateBlobOrStreamKey()
- : sharedCryptoWorker().then((w) => w.generateBlobOrStreamKey());
+ : sharedWorker().then((w) => w.generateBlobOrStreamKey());
/**
* Encrypt the given data, returning a box containing the encrypted data and a
@@ -129,7 +132,7 @@ export const generateBlobOrStreamKey = () =>
export const encryptBoxB64 = (data: BytesOrB64, key: BytesOrB64) =>
inWorker()
? ei._encryptBoxB64(data, key)
- : sharedCryptoWorker().then((w) => w.encryptBoxB64(data, key));
+ : sharedWorker().then((w) => w.encryptBoxB64(data, key));
/**
* Encrypt the given data, returning a blob containing the encrypted data and a
@@ -155,7 +158,7 @@ export const encryptBlob = (data: BytesOrB64, key: BytesOrB64) =>
export const encryptBlobB64 = (data: BytesOrB64, key: BytesOrB64) =>
inWorker()
? ei._encryptBlobB64(data, key)
- : sharedCryptoWorker().then((w) => w.encryptBlobB64(data, key));
+ : sharedWorker().then((w) => w.encryptBlobB64(data, key));
/**
* Encrypt the thumbnail for a file.
@@ -169,7 +172,7 @@ export const encryptBlobB64 = (data: BytesOrB64, key: BytesOrB64) =>
export const encryptThumbnail = (data: BytesOrB64, key: BytesOrB64) =>
inWorker()
? ei._encryptThumbnail(data, key)
- : sharedCryptoWorker().then((w) => w.encryptThumbnail(data, key));
+ : sharedWorker().then((w) => w.encryptThumbnail(data, key));
/**
* Encrypt the given data using chunked streaming encryption, but process all
@@ -178,7 +181,7 @@ export const encryptThumbnail = (data: BytesOrB64, key: BytesOrB64) =>
export const encryptStreamBytes = async (data: Uint8Array, key: BytesOrB64) =>
inWorker()
? ei._encryptStreamBytes(data, key)
- : sharedCryptoWorker().then((w) => w.encryptStreamBytes(data, key));
+ : sharedWorker().then((w) => w.encryptStreamBytes(data, key));
/**
* Prepare for chunked streaming encryption using {@link encryptStreamChunk}.
@@ -186,7 +189,7 @@ export const encryptStreamBytes = async (data: Uint8Array, key: BytesOrB64) =>
export const initChunkEncryption = async (key: BytesOrB64) =>
inWorker()
? ei._initChunkEncryption(key)
- : sharedCryptoWorker().then((w) => w.initChunkEncryption(key));
+ : sharedWorker().then((w) => w.initChunkEncryption(key));
/**
* Encrypt a chunk as part of a chunked streaming encryption.
@@ -198,7 +201,7 @@ export const encryptStreamChunk = async (
) =>
inWorker()
? ei._encryptStreamChunk(data, state, isFinalChunk)
- : sharedCryptoWorker().then((w) =>
+ : sharedWorker().then((w) =>
w.encryptStreamChunk(data, state, isFinalChunk),
);
@@ -225,9 +228,7 @@ export const encryptStreamChunk = async (
export const encryptMetadataJSON_New = (jsonValue: unknown, key: BytesOrB64) =>
inWorker()
? ei._encryptMetadataJSON_New(jsonValue, key)
- : sharedCryptoWorker().then((w) =>
- w.encryptMetadataJSON_New(jsonValue, key),
- );
+ : sharedWorker().then((w) => w.encryptMetadataJSON_New(jsonValue, key));
/**
* Deprecated, use {@link encryptMetadataJSON_New} instead.
@@ -238,7 +239,7 @@ export const encryptMetadataJSON = async (r: {
}) =>
inWorker()
? ei._encryptMetadataJSON(r)
- : sharedCryptoWorker().then((w) => w.encryptMetadataJSON(r));
+ : sharedWorker().then((w) => w.encryptMetadataJSON(r));
/**
* Decrypt a box encrypted using {@link encryptBoxB64} and returns the decrypted
@@ -247,7 +248,7 @@ export const encryptMetadataJSON = async (r: {
export const decryptBox = (box: EncryptedBox, key: BytesOrB64) =>
inWorker()
? ei._decryptBox(box, key)
- : sharedCryptoWorker().then((w) => w.decryptBox(box, key));
+ : sharedWorker().then((w) => w.decryptBox(box, key));
/**
* Variant of {@link decryptBox} that returns the result as a base64 string.
@@ -255,7 +256,7 @@ export const decryptBox = (box: EncryptedBox, key: BytesOrB64) =>
export const decryptBoxB64 = (box: EncryptedBox, key: BytesOrB64) =>
inWorker()
? ei._decryptBoxB64(box, key)
- : sharedCryptoWorker().then((w) => w.decryptBoxB64(box, key));
+ : sharedWorker().then((w) => w.decryptBoxB64(box, key));
/**
* Decrypt a blob encrypted using either {@link encryptBlob} or
@@ -264,7 +265,7 @@ export const decryptBoxB64 = (box: EncryptedBox, key: BytesOrB64) =>
export const decryptBlob = (blob: EncryptedBlob, key: BytesOrB64) =>
inWorker()
? ei._decryptBlob(blob, key)
- : sharedCryptoWorker().then((w) => w.decryptBlob(blob, key));
+ : sharedWorker().then((w) => w.decryptBlob(blob, key));
/**
* A variant of {@link decryptBlob} that returns the result as a base64 string.
@@ -272,7 +273,7 @@ export const decryptBlob = (blob: EncryptedBlob, key: BytesOrB64) =>
export const decryptBlobB64 = (blob: EncryptedBlob, key: BytesOrB64) =>
inWorker()
? ei._decryptBlobB64(blob, key)
- : sharedCryptoWorker().then((w) => w.decryptBlobB64(blob, key));
+ : sharedWorker().then((w) => w.decryptBlobB64(blob, key));
/**
* Decrypt the thumbnail encrypted using {@link encryptThumbnail}.
@@ -280,7 +281,7 @@ export const decryptBlobB64 = (blob: EncryptedBlob, key: BytesOrB64) =>
export const decryptThumbnail = (blob: EncryptedBlob, key: BytesOrB64) =>
inWorker()
? ei._decryptThumbnail(blob, key)
- : sharedCryptoWorker().then((w) => w.decryptThumbnail(blob, key));
+ : sharedWorker().then((w) => w.decryptThumbnail(blob, key));
/**
* Decrypt the result of {@link encryptStreamBytes}.
@@ -291,7 +292,7 @@ export const decryptStreamBytes = async (
) =>
inWorker()
? ei._decryptStreamBytes(file, key)
- : sharedCryptoWorker().then((w) => w.decryptStreamBytes(file, key));
+ : sharedWorker().then((w) => w.decryptStreamBytes(file, key));
/**
* Prepare to decrypt the encrypted result produced using {@link initChunkEncryption} and
@@ -300,7 +301,7 @@ export const decryptStreamBytes = async (
export const initChunkDecryption = async (header: string, key: BytesOrB64) =>
inWorker()
? ei._initChunkDecryption(header, key)
- : sharedCryptoWorker().then((w) => w.initChunkDecryption(header, key));
+ : sharedWorker().then((w) => w.initChunkDecryption(header, key));
/**
* Decrypt an individual chunk produced by {@link encryptStreamChunk}.
@@ -313,7 +314,7 @@ export const decryptStreamChunk = async (
) =>
inWorker()
? ei._decryptStreamChunk(data, state)
- : sharedCryptoWorker().then((w) => w.decryptStreamChunk(data, state));
+ : sharedWorker().then((w) => w.decryptStreamChunk(data, state));
/**
* Decrypt the metadata JSON encrypted using {@link encryptMetadataJSON}.
@@ -327,9 +328,7 @@ export const decryptMetadataJSON_New = (
) =>
inWorker()
? ei._decryptMetadataJSON_New(blob, key)
- : sharedCryptoWorker().then((w) =>
- w.decryptMetadataJSON_New(blob, key),
- );
+ : sharedWorker().then((w) => w.decryptMetadataJSON_New(blob, key));
/**
* Deprecated, retains the old API.
@@ -341,7 +340,7 @@ export const decryptMetadataJSON = (r: {
}) =>
inWorker()
? ei._decryptMetadataJSON(r)
- : sharedCryptoWorker().then((w) => w.decryptMetadataJSON(r));
+ : sharedWorker().then((w) => w.decryptMetadataJSON(r));
/**
* Generate a new public/private keypair.
@@ -349,7 +348,7 @@ export const decryptMetadataJSON = (r: {
export const generateKeyPair = async () =>
inWorker()
? ei._generateKeyPair()
- : sharedCryptoWorker().then((w) => w.generateKeyPair());
+ : sharedWorker().then((w) => w.generateKeyPair());
/**
* Public key encryption.
@@ -357,7 +356,7 @@ export const generateKeyPair = async () =>
export const boxSeal = async (data: string, publicKey: string) =>
inWorker()
? ei._boxSeal(data, publicKey)
- : sharedCryptoWorker().then((w) => w.boxSeal(data, publicKey));
+ : sharedWorker().then((w) => w.boxSeal(data, publicKey));
/**
* Decrypt the result of {@link boxSeal}.
@@ -369,7 +368,7 @@ export const boxSealOpen = async (
) =>
inWorker()
? ei._boxSealOpen(encryptedData, publicKey, secretKey)
- : sharedCryptoWorker().then((w) =>
+ : sharedWorker().then((w) =>
w.boxSealOpen(encryptedData, publicKey, secretKey),
);
@@ -384,7 +383,7 @@ export const deriveKey = async (
) =>
inWorker()
? ei._deriveKey(passphrase, salt, opsLimit, memLimit)
- : sharedCryptoWorker().then((w) =>
+ : sharedWorker().then((w) =>
w.deriveKey(passphrase, salt, opsLimit, memLimit),
);
@@ -394,9 +393,7 @@ export const deriveKey = async (
export const deriveSensitiveKey = async (passphrase: string, salt: string) =>
inWorker()
? ei._deriveSensitiveKey(passphrase, salt)
- : sharedCryptoWorker().then((w) =>
- w.deriveSensitiveKey(passphrase, salt),
- );
+ : sharedWorker().then((w) => w.deriveSensitiveKey(passphrase, salt));
/**
* Derive an interactive key from the given {@link passphrase}.
@@ -404,6 +401,4 @@ export const deriveSensitiveKey = async (passphrase: string, salt: string) =>
export const deriveInteractiveKey = async (passphrase: string, salt: string) =>
inWorker()
? ei._deriveInteractiveKey(passphrase, salt)
- : sharedCryptoWorker().then((w) =>
- w.deriveInteractiveKey(passphrase, salt),
- );
+ : sharedWorker().then((w) => w.deriveInteractiveKey(passphrase, salt));