最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to encrypt a pouchdb database - Stack Overflow

programmeradmin9浏览0评论

Background:

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

My code:

<!DOCTYPE html>
<html ng-app="pouchdbApp">
 <head>
   <script src=".4.8/angular.min.js"></script>
   <script src="pouchdbDemo.js"></script>
   <script src=".2.1/pouchdb.min.js"></script>
   <!-- <script src="crypto-pouch-master/bundle.js"></script> -->
   <script src=""></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>

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./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./ajax/libs/angularjs/1.4.8/angular.min.js"></script>
   <script src="pouchdbDemo.js"></script>
   <script src="http://cdn.jsdelivr/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.

Share Improve this question edited Mar 14, 2016 at 5:26 S M Abrar Jahin 14.6k24 gold badges115 silver badges170 bronze badges asked Mar 2, 2016 at 21:35 user1455719user1455719 1,0655 gold badges16 silver badges37 bronze badges 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./calvinmetcalf/crypto-pouch/issues/21 – JustGage Commented Mar 9, 2016 at 5:20
Add a ment  | 

2 Answers 2

Reset to default 4 +50

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);
})

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

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

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);

发布评论

评论列表(0)

  1. 暂无评论