fix: cypto running encrypt before initialize

This commit is contained in:
thecodrr
2020-12-07 14:54:30 +05:00
parent d122f9a569
commit 66ad0a1bfa
2 changed files with 22 additions and 5 deletions

View File

@@ -47,15 +47,34 @@ class Crypto {
class CryptoWorker {
constructor() {
this.isReady = false;
this.initializing = false;
this.promiseQueue = [];
}
async _initialize() {
if (this.isReady) return;
if (this.isReady || this.initializing)
return await new Promise((resolve, reject) => {
this.promiseQueue.push({ resolve, reject });
});
this.initializing = true;
this.worker = new Worker("crypto.worker.js");
const buffer = Buffer.allocUnsafe(32);
crypto.getRandomValues(buffer);
const message = { seed: buffer.buffer };
await this._communicate("load", message, [message.seed], false);
this.isReady = true;
try {
await this._communicate("load", message, [message.seed], false);
this.isReady = true;
this.promiseQueue.forEach(({ resolve }) => resolve());
} catch (e) {
this.isReady = false;
this.promiseQueue.forEach(({ reject }) => reject(e));
} finally {
this.initializing = false;
this.promiseQueue = [];
}
}
_communicate(type, data, transferables = [], init = true) {

View File

@@ -41,12 +41,10 @@ async function deriveCryptoKey(name, data) {
if (!password) throw new Error("Invalid data provided to deriveCryptoKey.");
const keyData = await crypto.deriveKey(password, salt, true);
console.log("keyData", keyData);
if (window.indexedDB) {
const pbkdfKey = await derivePBKDF2Key(password);
await write(name, pbkdfKey);
const cipheredKey = await aesEncrypt(pbkdfKey, keyData);
console.log("cipheredKey", cipheredKey);
await write(`${name}@_k`, cipheredKey);
} else {
await write(`${name}@_k`, keyData);