Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

refactor: seperate crypto and crypto web worker

thecodrr7888246c99ce
diff --git a/apps/web/public/crypto.worker.js b/apps/web/public/crypto.worker.js
index 7beb82c84..bb374e3f8 100644
--- a/apps/web/public/crypto.worker.js
+++ b/apps/web/public/crypto.worker.js
@@ -238,7 +238,7 @@ const webCryptoPolyfill = (seed, sodium) => {
self.crypto = crypto;
};
-function getAlgorithm(base64Variant, compress) {
- //Template: encryptionAlgorithm-kdfAlgorithm-compressionFlag-base64variant
- return `xcha-argon2i13-${compress}-none-${base64Variant}`;
+function getAlgorithm(base64Variant) {
+ //Template: encryptionAlgorithm-kdfAlgorithm-base64variant
+ return `xcha-argon2i13-${base64Variant}`;
}
diff --git a/apps/web/src/interfaces/nncrypto/index.js b/apps/web/src/interfaces/nncrypto/index.js
new file mode 100644
index 000000000..281b46a96
--- /dev/null
+++ b/apps/web/src/interfaces/nncrypto/index.js
@@ -0,0 +1,6 @@
+import NNCrypto from "./nncrypto";
+import NNCryptoWorker from "./nncryptoworker";
+
+export default "Worker" in window || "Worker" in global
+ ? NNCryptoWorker
+ : NNCrypto;
diff --git a/apps/web/src/interfaces/nncrypto/nncrypto.js b/apps/web/src/interfaces/nncrypto/nncrypto.js
new file mode 100644
index 000000000..01d835a61
--- /dev/null
+++ b/apps/web/src/interfaces/nncrypto/nncrypto.js
@@ -0,0 +1,73 @@
+export default class NNCrypto {
+ isReady = false;
+ constructor() {
+ this.sodium = undefined;
+ }
+ async _initialize() {
+ if (this.isReady) return;
+ return new Promise(async (resolve) => {
+ window.sodium = {
+ onload: (_sodium) => {
+ if (this.isReady) return;
+ this.isReady = true;
+ this.sodium = _sodium;
+ loadScript("/crypto.worker.js").then(resolve);
+ },
+ };
+ await loadScript("sodium.js");
+ });
+ }
+
+ /**
+ *
+ * @param {{password: string}|{key:string, salt: string}} passwordOrKey - password or derived key
+ * @param {string} plainData - the plaintext data
+ * @param {boolean}
+ */
+ encrypt = async (passwordOrKey, plainData) => {
+ await this._initialize();
+ return global.ncrypto.encrypt.call(this, passwordOrKey, {
+ type: "plain",
+ data: plainData,
+ });
+ };
+
+ /**
+ *
+ * @param {{password: string}|{key:string, salt: string}} passwordOrKey - password or derived key
+ * @param {{alg: string, salt: string, iv: string, cipher: string}} cipherData - the cipher data
+ */
+ decrypt = async (passwordOrKey, cipherData) => {
+ await this._initialize();
+ cipherData.output = "text";
+ return global.ncrypto.decrypt.call(this, passwordOrKey, cipherData);
+ };
+
+ deriveKey = async (password, salt, exportKey = false) => {
+ await this._initialize();
+ return global.ncrypto.deriveKey.call(this, password, salt, exportKey);
+ };
+
+ hashPassword = async (password, userId) => {
+ await this._initialize();
+ return global.ncrypto.hashPassword.call(this, password, userId);
+ };
+}
+
+function loadScript(url) {
+ return new Promise((resolve) => {
+ // adding the script tag to the head as suggested before
+ var head = document.getElementsByTagName("head")[0];
+ var script = document.createElement("script");
+ script.type = "text/javascript";
+ script.src = url;
+
+ // then bind the event to the callback function
+ // there are several events for cross browser compatibility
+ script.onreadystatechange = resolve;
+ script.onload = resolve;
+
+ // fire the loading
+ head.appendChild(script);
+ });
+}
diff --git a/apps/web/src/interfaces/crypto.js b/apps/web/src/interfaces/nncrypto/nncryptoworker.js
similarity index 50%
rename from apps/web/src/interfaces/crypto.js
rename to apps/web/src/interfaces/nncrypto/nncryptoworker.js
index 9acea6206..d1fceb676 100644
--- a/apps/web/src/interfaces/crypto.js
+++ b/apps/web/src/interfaces/nncrypto/nncryptoworker.js
@@ -1,4 +1,4 @@
-class CryptoWorker {
+export default class NNCryptoWorker {
constructor() {
this.isReady = false;
this.initializing = false;
@@ -90,97 +90,3 @@ class CryptoWorker {
return this._communicate("hashPassword", { password, userId });
};
}
-
-const NCrypto =
- "Worker" in window || "Worker" in global ? CryptoWorker : Crypto;
-export default NCrypto;
-
-function loadScript(url) {
- return new Promise((resolve) => {
- // adding the script tag to the head as suggested before
- var head = document.getElementsByTagName("head")[0];
- var script = document.createElement("script");
- script.type = "text/javascript";
- script.src = url;
-
- // then bind the event to the callback function
- // there are several events for cross browser compatibility
- script.onreadystatechange = resolve;
- script.onload = resolve;
-
- // fire the loading
- head.appendChild(script);
- });
-}
-
-/**
- *
- * @param {string} alg
- */
-function parseAlgorithm(alg) {
- if (!alg) return {};
- const [enc, kdf, compressed, compressionAlg, base64variant] = alg.split("-");
- return {
- encryptionAlgorithm: enc,
- kdfAlgorithm: kdf,
- isCompressed: compressed === "1",
- compressionAlgorithm: compressionAlg,
- base64_variant: base64variant,
- };
-}
-
-class Crypto {
- isReady = false;
- constructor() {
- this.sodium = undefined;
- }
- async _initialize() {
- if (this.isReady) return;
- return new Promise(async (resolve) => {
- window.sodium = {
- onload: (_sodium) => {
- if (this.isReady) return;
- this.isReady = true;
- this.sodium = _sodium;
- loadScript("/crypto.worker.js").then(resolve);
- },
- };
- await loadScript("sodium.js");
- });
- }
-
- /**
- *
- * @param {{password: string}|{key:string, salt: string}} passwordOrKey - password or derived key
- * @param {string} plainData - the plaintext data
- * @param {boolean}
- */
- encrypt = async (passwordOrKey, plainData) => {
- await this._initialize();
- return global.ncrypto.encrypt.call(this, passwordOrKey, {
- type: "plain",
- data: plainData,
- });
- };
-
- /**
- *
- * @param {{password: string}|{key:string, salt: string}} passwordOrKey - password or derived key
- * @param {{alg: string, salt: string, iv: string, cipher: string}} cipherData - the cipher data
- */
- decrypt = async (passwordOrKey, cipherData) => {
- await this._initialize();
- cipherData.output = "text";
- return global.ncrypto.decrypt.call(this, passwordOrKey, cipherData);
- };
-
- deriveKey = async (password, salt, exportKey = false) => {
- await this._initialize();
- return global.ncrypto.deriveKey.call(this, password, salt, exportKey);
- };
-
- hashPassword = async (password, userId) => {
- await this._initialize();
- return global.ncrypto.hashPassword.call(this, password, userId);
- };
-}
diff --git a/apps/web/src/interfaces/storage.js b/apps/web/src/interfaces/storage.js
index ffa2f8684..86e1166d2 100644
--- a/apps/web/src/interfaces/storage.js
+++ b/apps/web/src/interfaces/storage.js
@@ -1,9 +1,9 @@
import localforage from "localforage";
import { extendPrototype } from "localforage-getitems";
import sort from "fast-sort";
-import Crypto from "./crypto";
+import NNCrypto from "./nncrypto";
-const crypto = new Crypto();
+const crypto = new NNCrypto();
extendPrototype(localforage);
localforage.config({