calvinmetcalf / crypto-pouch

plugin for encrypted pouchdb/couchdb databases
MIT License
243 stars 43 forks source link

The application freezes when executing db.crypto #53

Closed alvin-ho closed 3 years ago

alvin-ho commented 7 years ago

I am now developing an app using ionic 1 with pouchDB and pouchdb-adapter-cordova-sqlite. The app is freezes for about 10 seconds even the UI and the database is empty in iPhone 6s. I am tried it with the database with about 100+ records, it takes about 20 minutes.

Can I know is there something I missed, and what can I do in order to improve it?

My code as following:

` $rootScope.DeviceReady.promise.then(function(){

            var db = new PouchDB(name,{
              auto_compaction: true,
              adapter: 'cordova-sqlite',
              iosDatabaseLocation: 'default',
              androidDatabaseImplementation: 2
            });

             db.crypto( 'password' );                        // the app freezes after this

} `

library version: iOS 9.3.5

angular#1.5.3 pouchdb#6.0.5 pouchdb-adapter-cordova-sqlite crypto-pouch

calvinmetcalf commented 7 years ago

it's running the key derivation in the main thread and safari web view doesn't support crypto api so it's all being done in javascript

MAOstrander commented 7 years ago

I'm running into the same issue. It works like a dream on Android devices. However, on iOS the login process comes to a halt for 1-3 minutes while it does the db.crypto() command.

After reading this issue and the following part of the readme:

Chacha20-Poly1305 is also available and previous versions defaulted to this algorithm. You might consider using this if your app will primarily be used in browsers that don't support the web crypto api (e.g. safari).

I tried to make it more performant on my iOS devices by encrypting with this option: options.algorithm = 'chacha20'; I'm not really noticing any difference however. Am I implementing that correctly, or is there something else I need to be doing in order to get this functioning properly?

Do you have any other suggestions to make the login/encryption process more bearable for the user? Thanks in advance.

calvinmetcalf commented 7 years ago

so it's not the encryption that is the issue, it's the key derivation e.g. turning your human readable text into a binary bits. You can do the key derivation your self if you want and and pass in a 32 byte browserify buffer as a key parameter of the options (you can omit the password and just pass the options as the first parameter)

So you can do something like

var buf = createBufferSomehow();
console.log(Buffer.isBuffer(buf));// needs to be true
console.log(buf.length);//needs to be 32
db.crypto( {key: buf});
// or since you are on safari
db.crypto( {key: buf, algorithm: 'chacha20'});

you can use this along with a key you derived some other way from your password, if you have access to like a web worker or whatever the safari web view equivalent is, you can use pbkdf2 in a background thread (that's the algo we use) or some other algo to generate the key from the password.

wouterSeyen commented 7 years ago

Hi Calvin,

I'm using crypto-pouch to encrypt the pouch-db's we use in an Ionic app but I'm also having issues with the cryptography being terribly slow on iOS devices. I've tried your suggestion and created a buffer using forge

var salt = forge.random.getBytesSync(128);
var key = forge.pkcs5.pbkdf2(password, salt, 100000, 32);
db.crypto({ key: key, algorithm: 'chacha20'});

100000 iterations was not manageable (also not on Android), 1000 iterations took about 2-3 seconds computing time on iOS. Using this generated key as an option for the db.crypto is not faster than without passing an options object. Do you know of any other options I can try?

P.S: the plugin works flawlessly on Android. And it's awesome that it's just a 'feature toggle'.

calvinmetcalf commented 7 years ago

the other option is to use a web worker to, where you could open the database in that thread and have it receive messages from your main thread and send back the decrypted data from the database

On Thu, Apr 6, 2017 at 9:07 AM wouterSeyen notifications@github.com wrote:

Hi Calvin,

I'm using crypto-pouch to encrypt the pouch-db's we use in an Ionic app but I'm also having issues with the cryptography being terribly slow on iOS devices. I've tried your suggestion and created a buffer using forge https://github.com/digitalbazaar/forge

var salt = forge.random.getBytesSync(128);var key = forge.pkcs5.pbkdf2(password, salt, 100000, 32);db.crypto({ key: key, algorithm: 'chacha20'});

100000 iterations was not manageable (also not on Android), 1000 iterations took about 2-3 seconds computing time on iOS. Using this generated key as an option for the db.crypto is not faster than without passing an options object. Do you know of any other options I can try?

P.S: the plugin works flawlessly on Android. And it's awesome that it's just a 'feature toggle'.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/calvinmetcalf/crypto-pouch/issues/53#issuecomment-292168540, or mute the thread https://github.com/notifications/unsubscribe-auth/ABE4n7VIekK2gEUGcwa0kSyhbDTRv_00ks5rtOOKgaJpZM4Mh_dt .

garbados commented 3 years ago

Howdy, folks! We just released version 4.0 which changes a lot of things. Could you try again with the new version and let me know how it goes?

Since it's been four years, I'm going to close this issue for now. Let me know if you'd still like help!