Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encrypt a pouchdb database

Background:

I am trying to encrypt a pouchdb database by using crypto-pouch library. I had a look at the example shown at https://github.com/calvinmetcalf/crypto-pouch But it doesn't seem to do anything for me.

My code:

<!DOCTYPE html>
<html ng-app="pouchdbApp">
 <head>
   <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
   <script src="pouchdbDemo.js"></script>
   <script src="http://cdn.jsdelivr.net/pouchdb/5.2.1/pouchdb.min.js"></script>
   <!-- <script src="crypto-pouch-master/bundle.js"></script> -->
   <script src="http://wzrd.in/standalone/crypto-pouch"></script>

   <script>
       var db = new PouchDB('kittens2');

       var password = "mypassword";

      db.crypto(password).then(function (publicKey) {
            console.log("publicKey");
   	    console.log(publicKey);
       });
   
       /* db.removeCrypto();  */

       var doc = {
		  "_id": "mittens",
		  "name": "Mittens",
		  "occupation": "kitten",
		  "age": 3,
		  "hobbies": [
		    "playing with balls of yarn",
		    "chasing laser pointers",
		    "lookin' hella cute"
 		   ]
		};
      
      db.put(doc);

      db.get('mittens').then(function (doc) {
         console.log(doc);
      });

   </script>

 </head>
 <body>

 </body>

</html>

But my code doesn't see to do any encryption of the data entered, or i couldn't see any public key generated.

Any clue how i should be using the crypto-pouch library with pouchdb.

like image 247
user1455719 Avatar asked Mar 02 '16 21:03

user1455719


1 Answers

Edit: this answer originally refereed to version 1.x of crypto pouch, but is not correct for the current version (3.x), in the current version db.crypto(password) does not return a promise so the code examples updated are

db.crypto(password)
// <-- encryption set up

and

db.crypto(password);
db.put({_id: 'foo', bar: 'baz'}).then(function () {
    return db.get('foo');
}).then(function (doc) {
    console.log('decrypted', doc);
    return db.removeCrypto();
}).then(function () {
    return db.get('foo');
}).then(function (doc) {
    console.log('encrypted', doc);
})

Original answer (still valid for v1.x) follows:

so the documentation is a bit confusing (which I just cleaned up) but when you call db.crypto it wraps the database so that documents are transparently encrypted and decrypted

db.crypto(password).then(function () {
   // <-- encryption set up
})

and it will transparently encrypt documents you create and decrypt ones you read until you call

db.removeCrypto();

so if you want to test do something like

db.crypto(password).then(function () {
   return db.put({_id: 'foo', bar: 'baz'});
}).then(function () {
    return db.get('foo');
}).then(function (doc) {
    console.log('decrypted', doc);
    return db.removeCrypto();
}).then(function () {
    return db.get('foo');
}).then(function (doc) {
    console.log('encrypted', doc);
})
like image 115
Calvin Avatar answered Sep 28 '22 00:09

Calvin