A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
refactor: use subtle.crypto instead of CryptoJS
thecodrr38485f9cd45e
diff --git a/apps/web/package.json b/apps/web/package.jsonindex e924e4cd5..948bdd8a0 100644--- a/apps/web/package.json+++ b/apps/web/package.json@@ -4,7 +4,6 @@"private": true,"dependencies": {"@rebass/forms": "^4.0.6",- "crypto-js": "^4.0.0","emotion-theming": "^10.0.19","events": "^3.0.0","immer": "^5.3.6",diff --git a/apps/web/src/interfaces/storage.js b/apps/web/src/interfaces/storage.jsindex 1abc455f7..74fe6e542 100644--- a/apps/web/src/interfaces/storage.js+++ b/apps/web/src/interfaces/storage.js@@ -1,5 +1,8 @@import localforage from "localforage";-import CryptoJS from "crypto-js";+import { encode, decode } from "../utils/base64";+import { hexToBuffer, bufferToHex } from "../utils/hex";+const encoder = new TextEncoder();+const decoder = new TextDecoder();localforage.config({name: "Notesnook",@@ -19,42 +22,32 @@ function clear() {return localforage.clear();}-function encrypt(password, data) {- return getPBKDF2Key(password, "salt")- .then(key =>- CryptoJS.AES.encrypt(data, key, {- mode: CryptoJS.mode.CBC,- padding: CryptoJS.pad.Pkcs7- })- )- .then(encryptedData => ({- cipher: encryptedData.ciphertext.toString(CryptoJS.enc.Base64),- iv: encryptedData.iv.toString(CryptoJS.enc.Hex)- }));+const randArray = new Uint8Array(16);+async function encrypt(password, data) {+ var algo = {+ name: "AES-CBC",+ length: 256,+ iv: crypto.getRandomValues(randArray)+ };+ var key = await getPBKDF2Key(password, "salt");+ var encoded = encoder.encode(data);+ var cipher = await crypto.subtle.encrypt(algo, key, encoded);+ return {+ cipher: encode(cipher),+ iv: bufferToHex(algo.iv)+ };}-function decrypt(password, data) {- return getPBKDF2Key(password, "salt")- .then(key =>- CryptoJS.AES.decrypt(data.cipher, key, {- iv: CryptoJS.enc.Hex.parse(data.iv),- mode: CryptoJS.mode.CBC,- padding: CryptoJS.pad.Pkcs7- })- )- .then(decryptedText => decryptedText.toString(CryptoJS.enc.Utf8));-}--const params = {- keySize: 256 / 32,- hasher: CryptoJS.algo.SHA512,- iterations: 5000-};+async function decrypt(password, data) {+ var algo = {+ name: "AES-CBC",+ length: 256,+ iv: hexToBuffer(data.iv)+ };+ var key = await getPBKDF2Key(password, "salt");+ var decrypted = await crypto.subtle.decrypt(algo, key, decode(data.cipher));-function getPBKDF2Key(password, salt) {- return new Promise(resolve =>- resolve(CryptoJS.PBKDF2(password, salt, params))- );+ return decoder.decode(decrypted);}export default {@@ -65,3 +58,26 @@ export default {encrypt,decrypt};++async function getPBKDF2Key(password, salt) {+ var algo = {+ name: "PBKDF2",+ hash: "SHA-512",+ salt: encoder.encode(salt),+ iterations: 5000+ };+ var derived = { name: "AES-CBC", length: 256 };+ var encoded = encoder.encode(password);+ var key = await crypto.subtle.importKey(+ "raw",+ encoded,+ { name: "PBKDF2" },+ false,+ ["deriveKey"]+ );++ return crypto.subtle.deriveKey(algo, key, derived, false, [+ "encrypt",+ "decrypt"+ ]);+}diff --git a/apps/web/src/utils/base64.js b/apps/web/src/utils/base64.jsindex d7f98ce14..f83d32a61 100644--- a/apps/web/src/utils/base64.js+++ b/apps/web/src/utils/base64.js@@ -5,69 +5,66 @@* Copyright (c) 2012 Niklas von Hertzen* Licensed under the MIT license.*/-(function() {- var chars =- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";+var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";- // Use a lookup table to find the index.- var lookup = new Uint8Array(256);- for (var i = 0; i < chars.length; i++) {- lookup[chars.charCodeAt(i)] = i;- }+// Use a lookup table to find the index.+var lookup = new Uint8Array(256);+for (var i = 0; i < chars.length; i++) {+ lookup[chars.charCodeAt(i)] = i;+}- exports.encode = function(arraybuffer) {- var bytes = new Uint8Array(arraybuffer),- i,- len = bytes.length,- base64 = "";+export function encode(arraybuffer) {+ var bytes = new Uint8Array(arraybuffer),+ i,+ len = bytes.length,+ base64 = "";- for (i = 0; i < len; i += 3) {- base64 += chars[bytes[i] >> 2];- base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];- base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];- base64 += chars[bytes[i + 2] & 63];- }+ for (i = 0; i < len; i += 3) {+ base64 += chars[bytes[i] >> 2];+ base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];+ base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];+ base64 += chars[bytes[i + 2] & 63];+ }- if (len % 3 === 2) {- base64 = base64.substring(0, base64.length - 1) + "=";- } else if (len % 3 === 1) {- base64 = base64.substring(0, base64.length - 2) + "==";- }+ if (len % 3 === 2) {+ base64 = base64.substring(0, base64.length - 1) + "=";+ } else if (len % 3 === 1) {+ base64 = base64.substring(0, base64.length - 2) + "==";+ }- return base64;- };+ return base64;+}- exports.decode = function(base64) {- var bufferLength = base64.length * 0.75,- len = base64.length,- i,- p = 0,- encoded1,- encoded2,- encoded3,- encoded4;+export function decode(base64) {+ var bufferLength = base64.length * 0.75,+ len = base64.length,+ i,+ p = 0,+ encoded1,+ encoded2,+ encoded3,+ encoded4;- if (base64[base64.length - 1] === "=") {+ if (base64[base64.length - 1] === "=") {+ bufferLength--;+ if (base64[base64.length - 2] === "=") {bufferLength--;- if (base64[base64.length - 2] === "=") {- bufferLength--;- }}+ }- var arraybuffer = new ArrayBuffer(bufferLength),- bytes = new Uint8Array(arraybuffer);+ var arraybuffer = new ArrayBuffer(bufferLength),+ bytes = new Uint8Array(arraybuffer);- for (i = 0; i < len; i += 4) {- encoded1 = lookup[base64.charCodeAt(i)];- encoded2 = lookup[base64.charCodeAt(i + 1)];- encoded3 = lookup[base64.charCodeAt(i + 2)];- encoded4 = lookup[base64.charCodeAt(i + 3)];+ for (i = 0; i < len; i += 4) {+ encoded1 = lookup[base64.charCodeAt(i)];+ encoded2 = lookup[base64.charCodeAt(i + 1)];+ encoded3 = lookup[base64.charCodeAt(i + 2)];+ encoded4 = lookup[base64.charCodeAt(i + 3)];- bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);- bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);- bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);- }+ bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);+ bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);+ bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);+ }- return arraybuffer;- };-})();+ return arraybuffer;+}diff --git a/apps/web/src/utils/hex.js b/apps/web/src/utils/hex.jsnew file mode 100644index 000000000..054a86c3a--- /dev/null+++ b/apps/web/src/utils/hex.js@@ -0,0 +1,32 @@+const byteToHex = [];+const hexToByte = [];++for (let n = 0; n <= 0xff; ++n) {+ const hexOctet = n.toString(16).padStart(2, "0");+ byteToHex.push(hexOctet);+ hexToByte[hexOctet] = n;+}++function bufferToHex(buff) {+ const hexOctets = new Array(buff.length); // new Array(buff.length) is even faster (preallocates necessary array size), then use hexOctets[i] instead of .push()++ for (let i = 0; i < buff.length; ++i) hexOctets[i] = byteToHex[buff[i]];++ return hexOctets.join("");+}++function hexToBuffer(hex) {+ var length2 = hex.length;+ if (length2 % 2 !== 0) {+ throw new Error("hex string must have length a multiple of 2");+ }+ var length = length2 / 2;+ var result = new Uint8Array(length);+ for (var i = 0; i < length; i++) {+ var i2 = i * 2;+ var b = hex.substring(i2, i2 + 2);+ result[i] = hexToByte[b];+ }+ return result;+}+export { hexToBuffer, bufferToHex };diff --git a/apps/web/yarn.lock b/apps/web/yarn.lockindex b1f73350d..34327ea86 100644--- a/apps/web/yarn.lock+++ b/apps/web/yarn.lock@@ -6818,7 +6818,7 @@ normalize-url@^3.0.0, normalize-url@^3.0.1:"notes-core@https://github.com/thecodrr/notes-core.git":version "1.1.0"- resolved "https://github.com/thecodrr/notes-core.git#418e48b357179db10ddb6b3116b61c5746c92e48"+ resolved "https://github.com/thecodrr/notes-core.git#b270a0640f2a8195d86e21a64bf40e651034c545"dependencies:fast-sort "^2.0.1"fuzzysearch "^1.0.3"