Merge crypto workers into one
This commit is contained in:
parent
ce308a8e0e
commit
3559e68490
4 changed files with 49 additions and 54 deletions
|
@ -2,11 +2,8 @@ import { getEndpoint } from "utils/common/apiUtil";
|
|||
import HTTPService from "./HTTPService";
|
||||
import * as Comlink from "comlink";
|
||||
|
||||
const decryptMetadata: any = typeof window !== 'undefined'
|
||||
&& Comlink.wrap(new Worker("worker/decryptMetadata.worker.js", { type: 'module' }));
|
||||
const decryptThumbnail: any = typeof window !== 'undefined'
|
||||
&& Comlink.wrap(new Worker("worker/decryptThumbnail.worker.js", { type: 'module' }));
|
||||
|
||||
const Crypto = typeof window !== 'undefined'
|
||||
&& Comlink.wrap(new Worker("worker/crypto.worker.js", { type: 'module' }));
|
||||
const ENDPOINT = getEndpoint();
|
||||
|
||||
export interface decryptionParams {
|
||||
|
@ -37,12 +34,14 @@ export interface fileData {
|
|||
data?: string;
|
||||
};
|
||||
|
||||
const getFileMetaDataUsingWorker = (data: any, key: string) => {
|
||||
return decryptMetadata({ data, key });
|
||||
const getFileMetaDataUsingWorker = async (data: any, key: string) => {
|
||||
const worker = await new Crypto();
|
||||
return worker.decryptMetadata({ data, key });
|
||||
}
|
||||
|
||||
const getFileUsingWorker = (data: any, key: string) => {
|
||||
return decryptThumbnail({ data, key });
|
||||
const getFileUsingWorker = async (data: any, key: string) => {
|
||||
const worker = await new Crypto();
|
||||
return worker.decryptThumbnail({ data, key });
|
||||
}
|
||||
|
||||
export const getFiles = async (sinceTime: string, token: string, limit: string, key: string) => {
|
||||
|
|
41
src/worker/crypto.worker.js
Normal file
41
src/worker/crypto.worker.js
Normal file
|
@ -0,0 +1,41 @@
|
|||
import * as Comlink from 'comlink';
|
||||
import { base64ToUint8 } from "utils/crypto/common";
|
||||
import sodium from 'libsodium-wrappers';
|
||||
|
||||
class Crypto {
|
||||
async decryptMetadata(event) {
|
||||
const { data } = event;
|
||||
await sodium.ready;
|
||||
const key = sodium.crypto_secretbox_open_easy(
|
||||
base64ToUint8(data.metadata.decryptionParams.encryptedKey),
|
||||
base64ToUint8(data.metadata.decryptionParams.keyDecryptionNonce),
|
||||
base64ToUint8(event.key));
|
||||
const metadata = sodium.crypto_secretbox_open_easy(
|
||||
base64ToUint8(data.metadata.encryptedData),
|
||||
base64ToUint8(data.metadata.decryptionParams.nonce),
|
||||
key);
|
||||
return {
|
||||
...data,
|
||||
metadata: JSON.parse(new TextDecoder().decode(metadata))
|
||||
};
|
||||
}
|
||||
|
||||
async decryptThumbnail(event) {
|
||||
const { data } = event;
|
||||
await sodium.ready;
|
||||
const key = sodium.crypto_secretbox_open_easy(
|
||||
base64ToUint8(data.thumbnail.decryptionParams.encryptedKey),
|
||||
base64ToUint8(data.thumbnail.decryptionParams.keyDecryptionNonce),
|
||||
base64ToUint8(event.key));
|
||||
const thumbnail = sodium.crypto_secretbox_open_easy(
|
||||
new Uint8Array(data.file),
|
||||
base64ToUint8(data.thumbnail.decryptionParams.nonce),
|
||||
key);
|
||||
return {
|
||||
id: data.id,
|
||||
data: thumbnail,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Comlink.expose(Crypto);
|
|
@ -1,23 +0,0 @@
|
|||
import * as Comlink from 'comlink';
|
||||
import { base64ToUint8 } from "utils/crypto/common";
|
||||
import sodium from 'libsodium-wrappers';
|
||||
|
||||
async function decryptMetadata(event) {
|
||||
console.log(event);
|
||||
const { data } = event;
|
||||
await sodium.ready;
|
||||
const key = sodium.crypto_secretbox_open_easy(
|
||||
base64ToUint8(data.metadata.decryptionParams.encryptedKey),
|
||||
base64ToUint8(data.metadata.decryptionParams.keyDecryptionNonce),
|
||||
base64ToUint8(event.key));
|
||||
const metadata = sodium.crypto_secretbox_open_easy(
|
||||
base64ToUint8(data.metadata.encryptedData),
|
||||
base64ToUint8(data.metadata.decryptionParams.nonce),
|
||||
key);
|
||||
return {
|
||||
...data,
|
||||
metadata: JSON.parse(new TextDecoder().decode(metadata))
|
||||
};
|
||||
}
|
||||
|
||||
Comlink.expose(decryptMetadata, self);
|
|
@ -1,22 +0,0 @@
|
|||
import * as Comlink from 'comlink';
|
||||
import { base64ToUint8 } from "utils/crypto/common";
|
||||
import sodium from 'libsodium-wrappers';
|
||||
|
||||
async function decryptThumbnail(event) {
|
||||
const { data } = event;
|
||||
await sodium.ready;
|
||||
const key = sodium.crypto_secretbox_open_easy(
|
||||
base64ToUint8(data.thumbnail.decryptionParams.encryptedKey),
|
||||
base64ToUint8(data.thumbnail.decryptionParams.keyDecryptionNonce),
|
||||
base64ToUint8(event.key));
|
||||
const thumbnail = sodium.crypto_secretbox_open_easy(
|
||||
new Uint8Array(data.file),
|
||||
base64ToUint8(data.thumbnail.decryptionParams.nonce),
|
||||
key);
|
||||
return {
|
||||
id: data.id,
|
||||
data: thumbnail,
|
||||
};
|
||||
}
|
||||
|
||||
Comlink.expose(decryptThumbnail, self);
|
Loading…
Add table
Reference in a new issue