Add the variant

This commit is contained in:
Manav Rathi
2024-08-17 21:41:07 +05:30
parent 0240b37032
commit 76da94cf78
4 changed files with 39 additions and 42 deletions

View File

@@ -1,6 +1,6 @@
/** Careful when adding add other imports! */
import * as libsodium from "./libsodium";
import type { BytesOrB64, EncryptedBlob_2, EncryptJSON } from "./types";
import type { BytesOrB64, EncryptedBlob_2 } from "./types";
export const _encryptBoxB64 = libsodium.encryptBoxB64;
@@ -16,16 +16,18 @@ export const _encryptThumbnail = async (data: BytesOrB64, key: BytesOrB64) => {
};
};
export const _encryptMetadataJSON_New = ({ jsonValue, keyB64 }: EncryptJSON) =>
_encryptBlobB64(
new TextEncoder().encode(JSON.stringify(jsonValue)),
keyB64,
);
export const _encryptMetadataJSON_New = (jsonValue: unknown, key: BytesOrB64) =>
_encryptBlobB64(new TextEncoder().encode(JSON.stringify(jsonValue)), key);
export const _encryptMetadataJSON = async (r: EncryptJSON) => {
// Deprecated. Keep the old API for now.
const { encryptedData, decryptionHeader } =
await _encryptMetadataJSON_New(r);
// Deprecated, translates to the old API for now.
export const _encryptMetadataJSON = async (r: {
jsonValue: unknown;
keyB64: string;
}) => {
const { encryptedData, decryptionHeader } = await _encryptMetadataJSON_New(
r.jsonValue,
r.keyB64,
);
return {
encryptedDataB64: encryptedData,
decryptionHeaderB64: decryptionHeader,

View File

@@ -51,12 +51,7 @@ import { sharedCryptoWorker } from ".";
import { assertionFailed } from "../assert";
import { inWorker } from "../env";
import * as ei from "./ente-impl";
import type {
BytesOrB64,
EncryptedBlob_2,
EncryptedBox2,
EncryptJSON,
} from "./types";
import type { BytesOrB64, EncryptedBlob_2, EncryptedBox2 } from "./types";
/**
* Some of these functions have not yet been needed on the main thread, and for
@@ -129,18 +124,36 @@ export const encryptThumbnail = (data: BytesOrB64, key: BytesOrB64) =>
* Encrypt the JSON metadata associated with an Ente object (file, collection or
* entity) using the object's key.
*
* This is a variant of {@link encryptAssociatedData} tailored for encrypting
* any arbitrary metadata associated with an Ente object. For example, it is
* used for encrypting the various metadata fields (See: [Note: Metadatum])
* associated with a file, using that file's key.
* This is a variant of {@link encryptBlobB64} tailored for encrypting any
* arbitrary metadata associated with an Ente object. For example, it is used
* for encrypting the various metadata fields associated with a file, using that
* file's key.
*
* Instead of raw bytes, it takes as input an arbitrary JSON object which it
* encodes into a string, and encrypts that. And instead of returning the raw
* encrypted bytes, it returns their base64 string representation.
* encodes into a string, and encrypts that.
*
* Use {@link decryptMetadataJSON} to decrypt the result.
*
* @param jsonValue The JSON value to encrypt. This can be an arbitrary JSON
* value, but since TypeScript currently doesn't have a native JSON type, it is
* typed as {@link unknown}.
*
* @param key The encryption key.
*/
export const encryptMetadataJSON = async (r: EncryptJSON) =>
export const encryptMetadataJSON_New = (jsonValue: unknown, key: BytesOrB64) =>
inWorker()
? ei._encryptMetadataJSON_New(jsonValue, key)
: sharedCryptoWorker().then((w) =>
w.encryptMetadataJSON_New(jsonValue, key),
);
/**
* Deprecated, use {@link encryptMetadataJSON_New} instead.
*/
export const encryptMetadataJSON = async (r: {
jsonValue: unknown;
keyB64: string;
}) =>
inWorker()
? ei._encryptMetadataJSON(r)
: sharedCryptoWorker().then((w) => w.encryptMetadataJSON(r));

View File

@@ -4,25 +4,6 @@
*/
export type BytesOrB64 = Uint8Array | string;
/**
* Deprecated.
*
* A variant of {@link EncryptBytes} with the data as a JSON value.
*/
export interface EncryptJSON {
/**
* The JSON value to encrypt.
*
* This can be an arbitrary JSON value, but since TypeScript currently
* doesn't have a native JSON type, it is typed as {@link unknown}.
*/
jsonValue: unknown;
/**
* A base64 string containing the encryption key.
*/
keyB64: string;
}
/**
* The result of encryption using the secretbox APIs.
*

View File

@@ -14,6 +14,7 @@ import * as libsodium from "./libsodium";
export class CryptoWorker {
encryptBoxB64 = ei._encryptBoxB64;
encryptThumbnail = ei._encryptThumbnail;
encryptMetadataJSON_New = ei._encryptMetadataJSON_New;
encryptMetadataJSON = ei._encryptMetadataJSON;
decryptBox = ei._decryptBox;
decryptBoxB64 = ei._decryptBoxB64;