Tuesday, March 15, 2016

How to encrypt a pouchdb database

Leave a Comment

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.

1 Answers

Answers 1

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); }) 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment