This commit is contained in:
Manav Rathi
2024-08-10 18:02:44 +05:30
parent 50d7d7e9a1
commit e29c9288c0

View File

@@ -1,52 +1,37 @@
/** Careful when adding add other imports! */
import * as libsodium from "./libsodium";
import type { DecryptB64, EncryptBytes, EncryptJSON } from "./types";
import type {
DecryptB64,
EncryptBytes,
EncryptedB64,
EncryptedBytes,
EncryptJSON,
} from "./types";
const EncryptedBytesToB64 = async ({
encryptedData,
decryptionHeaderB64,
}: EncryptedBytes): Promise<EncryptedB64> => ({
encryptedDataB64: await libsodium.toB64(encryptedData),
decryptionHeaderB64,
});
export const _encryptAssociatedData = libsodium.encryptChaChaOneShot;
export const _encryptThumbnail = _encryptAssociatedData;
export const _encryptFileEmbedding = async (r: EncryptBytes) => {
const { encryptedData, decryptionHeaderB64 } =
await _encryptAssociatedData(r);
return {
encryptedDataB64: await libsodium.toB64(encryptedData),
decryptionHeaderB64,
};
};
export const _encryptFileEmbedding = (r: EncryptBytes) =>
_encryptAssociatedData(r).then(EncryptedBytesToB64);
export const _encryptMetadata = async ({ jsonValue, keyB64 }: EncryptJSON) => {
const data = new TextEncoder().encode(JSON.stringify(jsonValue));
const { encryptedData, decryptionHeaderB64 } = await _encryptAssociatedData(
{ data, keyB64 },
);
return {
encryptedDataB64: await libsodium.toB64(encryptedData),
decryptionHeaderB64,
};
return EncryptedBytesToB64(await _encryptAssociatedData({ data, keyB64 }));
};
export const _decryptAssociatedData = libsodium.decryptChaChaOneShot;
export const _decryptThumbnail = _decryptAssociatedData;
export const _decryptFileEmbedding = async ({
encryptedDataB64,
decryptionHeaderB64,
keyB64,
}: DecryptB64) =>
_decryptAssociatedData({
encryptedData: await libsodium.fromB64(encryptedDataB64),
decryptionHeaderB64,
keyB64,
});
export const _decryptMetadata = async (r: DecryptB64) =>
JSON.parse(
new TextDecoder().decode(await _decryptMetadataBytes(r)),
) as unknown;
export const _decryptMetadataBytes = async ({
encryptedDataB64,
decryptionHeaderB64,
@@ -57,3 +42,10 @@ export const _decryptMetadataBytes = async ({
decryptionHeaderB64,
keyB64,
});
export const _decryptMetadata = async (r: DecryptB64) =>
JSON.parse(
new TextDecoder().decode(await _decryptMetadataBytes(r)),
) as unknown;
export const _decryptFileEmbedding = _decryptMetadataBytes;