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
feat: fix database crypto
thecodrr1444cab7e60b
diff --git a/apps/web/package.json b/apps/web/package.jsonindex 948bdd8a0..e924e4cd5 100644--- a/apps/web/package.json+++ b/apps/web/package.json@@ -4,6 +4,7 @@"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 33fb08f7e..1abc455f7 100644--- a/apps/web/src/interfaces/storage.js+++ b/apps/web/src/interfaces/storage.js@@ -1,4 +1,5 @@import localforage from "localforage";+import CryptoJS from "crypto-js";localforage.config({name: "Notesnook",@@ -19,42 +20,41 @@ function clear() {}function encrypt(password, data) {- return deriveKey(password).then(key => {- console.log(key);- // a public value that should be generated for changes each time- var initializationVector = new Uint8Array(16);- crypto.getRandomValues(initializationVector);-- return crypto.subtle- .encrypt(- { name: "AES-CBC", iv: initializationVector },- key,- toArrayBuffer(new Buffer(data))- )- .then(function(encrypted) {- let b64 = require("../utils/base64");- return {- iv: b64.encode(initializationVector),- cipher: b64.encode(encrypted)- };- });- });+ 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)+ }));}function decrypt(password, data) {- return deriveKey(password).then(key => {- let b64 = require("../utils/base64");+ 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));+}- return crypto.subtle- .decrypt(- { name: "AES-CBC", iv: b64.decode(data.iv) },- key,- b64.decode(data.cipher)- )- .then(function(decrypted) {- return new Buffer(b64.encode(decrypted), "base64").toString("utf8");- });- });+const params = {+ keySize: 256 / 32,+ hasher: CryptoJS.algo.SHA512,+ iterations: 5000+};++function getPBKDF2Key(password, salt) {+ return new Promise(resolve =>+ resolve(CryptoJS.PBKDF2(password, salt, params))+ );}export default {@@ -65,60 +65,3 @@ export default {encrypt,decrypt};--function deriveKey(passphrase) {- var keyLenBits = 256;- var kdfname = "PBKDF2";- var aesname = "AES-CBC"; // AES-CTR is also popular- // 100 - probably safe even on a browser running from a raspberry pi using pure js ployfill- // 10000 - no noticeable speed decrease on my MBP- // 100000 - you can notice- // 1000000 - annoyingly long- var iterations = 5000; // something a browser on a raspberry pi or old phone could do- var hashname = "SHA-256";- var extractable = true;-- // First, create a PBKDF2 "key" containing the password- return (- crypto.subtle- .importKey(- "raw",- new Buffer(passphrase, "binary"),- { name: kdfname },- false,- ["deriveKey"]- )- // Derive a key from the password- .then(function(passphraseKey) {- return crypto.subtle.deriveKey(- {- name: kdfname,- salt: toArrayBuffer(new Buffer("salt")),- iterations: iterations,- hash: hashname- },- passphraseKey,- // required to be 128 (or 256) bits- { name: aesname, length: keyLenBits }, // Key we want- extractable, // Extractble- ["encrypt", "decrypt"] // For new key- );- })- // Export it so we can display it- .then(function(aesKey) {- return aesKey;- })- .catch(function(err) {- window.alert("Key derivation failed: " + err.message);- })- );-}--function toArrayBuffer(buf) {- var ab = new ArrayBuffer(buf.length);- var view = new Uint8Array(ab);- for (var i = 0; i < buf.length; ++i) {- view[i] = buf[i];- }- return ab;-}diff --git a/apps/web/yarn.lock b/apps/web/yarn.lockindex 15c13441c..b1f73350d 100644--- a/apps/web/yarn.lock+++ b/apps/web/yarn.lock@@ -3099,6 +3099,11 @@ crypto-browserify@^3.11.0:randombytes "^2.0.0"randomfill "^1.0.3"+crypto-js@^4.0.0:+ version "4.0.0"+ resolved "https://registry.yarnpkg.com/crypto-js/-/crypto-js-4.0.0.tgz#2904ab2677a9d042856a2ea2ef80de92e4a36dcc"+ integrity sha512-bzHZN8Pn+gS7DQA6n+iUmBfl0hO5DJq++QP3U6uTucDtk/0iGpXd/Gg7CGR0p8tJhofJyaKoWBuJI4eAO00BBg==+css-blank-pseudo@^0.1.4:version "0.1.4"resolved "https://registry.yarnpkg.com/css-blank-pseudo/-/css-blank-pseudo-0.1.4.tgz#dfdefd3254bf8a82027993674ccf35483bfcb3c5"