mirror of
https://github.com/streetwriters/notesnook.git
synced 2026-09-03 12:41:45 +02:00
fix: cypto running encrypt before initialize
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user