Conv and simplify

This commit is contained in:
Manav Rathi
2025-06-06 09:40:35 +05:30
parent eed1341003
commit 7c0450c364
5 changed files with 19 additions and 41 deletions

View File

@@ -74,7 +74,10 @@ const createCollection = async (
const { encryptedData: encryptedKey, nonce: keyDecryptionNonce } =
await cryptoWorker.encryptBox(collectionKey, encryptionKey);
const { encryptedData: encryptedName, nonce: nameDecryptionNonce } =
await cryptoWorker.encryptBoxUTF8(collectionName, collectionKey);
await cryptoWorker.encryptBox(
new TextEncoder().encode(collectionName),
collectionKey,
);
let encryptedMagicMetadata: EncryptedMagicMetadata;
if (magicMetadataProps) {
@@ -477,7 +480,10 @@ export const renameCollection = async (
const token = getToken();
const cryptoWorker = await sharedCryptoWorker();
const { encryptedData: encryptedName, nonce: nameDecryptionNonce } =
await cryptoWorker.encryptBoxUTF8(newCollectionName, collection.key);
await cryptoWorker.encryptBox(
new TextEncoder().encode(newCollectionName),
collection.key,
);
const collectionRenameRequest = {
collectionID: collection.id,
encryptedName,

View File

@@ -13,7 +13,6 @@ export const _fromHex = libsodium.fromHex;
export const _generateKey = libsodium.generateKey;
export const _generateBlobOrStreamKey = libsodium.generateBlobOrStreamKey;
export const _encryptBox = libsodium.encryptBox;
export const _encryptBoxUTF8 = libsodium.encryptBoxUTF8;
export const _encryptBlob = libsodium.encryptBlob;
export const _encryptBlobBytes = libsodium.encryptBlobBytes;
export const _encryptMetadataJSON = libsodium.encryptMetadataJSON;

View File

@@ -62,26 +62,24 @@
*
* There are two primary types used when exchanging data with these functions:
*
* 1. Base64 strings. Unqualified strings are taken as base64 encoded
* representations of the underlying data. Usually, the unqualified "base"
* function deals with Base64 strings, since they also are the data type in
* which we usually send the encryted data etc to remote.
* 1. Base64 strings. Unless stated otherwise, all strings are taken as base64
* encoded representations of the underlying data. Usually, the unqualified
* function deals with base64 strings, since they also are the data type in
* which we usually store and send the data.
*
* 2. Raw bytes. Uint8Arrays are byte arrays. The functions that deal with bytes
* are indicated by a *Bytes suffix in their name.
* are usually indicated by a *Bytes suffix in their name, but not always
* since it might also be the natural choice for functions that deal with
* larger amounts of data.
*
* Where possible and useful, functions also accept a union of these two - a
* Where relevant and useful, functions also accept a union of these two - a
* {@link BytesOrB64} where the implementation will automatically convert
* to/from base64 to bytes if needed, thus saving on unnecessary conversions at
* the caller side.
*
* Apart from these two, there are other secondary and one off types.
* Apart from these two, there are other secondary, one off types.
*
* 1. "Regular" JavaScript strings. These are indicated by the *UTF8 suffix on
* the function that deals with them. These strings will be obtained by UTF-8
* encoding (or decoding) the underlying bytes.
*
* 2. Hex representations of the bytes. These are indicated by the *Hex suffix
* 1. Hex representations of the bytes. These are indicated by the *Hex suffix
* on the functions dealing with them.
*
* 2. JSON values. These are indicated by the *JSON suffix on the functions
@@ -208,18 +206,6 @@ export const encryptBox = (
? ei._encryptBox(data, key)
: sharedWorker().then((w) => w.encryptBox(data, key));
/**
* A variant of {@link encryptBox} that first UTF-8 encodes the input string to
* obtain bytes, which it then encrypts.
*/
export const encryptBoxUTF8 = (
data: string,
key: BytesOrB64,
): Promise<EncryptedBoxB64> =>
inWorker()
? ei._encryptBoxUTF8(data, key)
: sharedWorker().then((w) => w.encryptBoxUTF8(data, key));
/**
* Encrypt the given data, returning a blob containing the encrypted data and a
* decryption header as base64 strings.

View File

@@ -287,18 +287,6 @@ export const encryptBox = async (
};
};
/**
* A variant of {@link encryptBox} that first converts the input string into
* bytes using a UTF-8 encoding, and then encrypts those bytes.
*/
export const encryptBoxUTF8 = async (
data: string,
key: BytesOrB64,
): Promise<EncryptedBoxB64> => {
await sodium.ready;
return encryptBox(sodium.from_string(data), key);
};
/**
* Encrypt the given data using libsodium's secretstream APIs without chunking.
*
@@ -550,7 +538,7 @@ export const decryptBlob = (
/**
* Decrypt the result of {@link encryptMetadataJSON} and return the JSON value
* obtained by parsing the decrypted JSON string (which is obtained by utf-8
* 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

View File

@@ -21,7 +21,6 @@ export class CryptoWorker {
generateKey = ei._generateKey;
generateBlobOrStreamKey = ei._generateBlobOrStreamKey;
encryptBox = ei._encryptBox;
encryptBoxUTF8 = ei._encryptBoxUTF8;
encryptBlob = ei._encryptBlob;
encryptBlobBytes = ei._encryptBlobBytes;
encryptMetadataJSON = ei._encryptMetadataJSON;