How to encrypt a pouchdb database
Asked Answered
P

2

6

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.

Piapiacenza answered 2/3, 2016 at 21:35 Comment(1)
I don't know about this library very much but it does seem to have the behavior you say. I suspect that it's probably unencryping it on the way out but I'm not sure. For this reason I created this GitHub issue: github.com/calvinmetcalf/crypto-pouch/issues/21Elaterin
D
4

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);
})
Diapason answered 9/3, 2016 at 13:53 Comment(3)
Hi Calvin, I was expecting a publicKey value supplied to me in the following code /*Start*/ db.crypto(password).then(function (publicKey) { console.log(publicKey); }); /*-- End--*/ But it seems to be giving me an undefined value for the 'publicKey' value .. why is it ?.Piapiacenza
1) The current url 'wzrd.in/standalone/crypto-pouch' gives me a javascript file which imports dependencies using nodejs 'require'. If I copy this javascript file into my project (which is a plain angularjs page ) then it complains about the nodejs 'require'. What should I do to use your library within my project. I can't refer to your url ('wzrd.in/standalone/crypto-pouch') from my project as it is meant to be working in an intranet environment with no internet connection.Piapiacenza
in order 1. the publicKey stuff was for a feature with diffie-hellman keys I'm removing, ignore that, it also wouldn't apply with just using a password 2. that's not how the requires work in that file, they are used internally to bundle but don't involve an internet connectionDiapason
M
1

I tried combDB and its the only one that seems to work as of now with the new nodeJS

const PouchDB = require('pouchdb')
PouchDB.plugin(require('comdb'))

const password = 'extremely secure value'

const db = new PouchDB(POUCH_PATH)
db.setPassword(password)

db.post({
  _id: 'gay-agenda',
  type: 'queerspiracy',
  agenda: ['be gay', 'do crimes']
}).then(() => {
  // now replicate to a couchdb instance
  return db.replicate.to(`${COUCH_URL}/FALGSC`)
})

or with Angular (Typescript)

import PouchDB from 'pouchdb-browser';

...
 this.db = new PouchDB('myProjectDB');
 this.db.setPassword(environment.dbPassword);

Mcinnis answered 3/11, 2019 at 15:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.