【问题标题】:How to encrypt a pouchdb database如何加密 pouchdb 数据库
【发布时间】:2016-06-15 23:23:48
【问题描述】:

背景:

我正在尝试使用 crypto-pouch 库来加密 pouchdb 数据库。 我查看了https://github.com/calvinmetcalf/crypto-pouch 中显示的示例 但它似乎对我没有任何作用。

我的代码:

<!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>

但是我的代码没有看到对输入的数据进行任何加密,或者我看不到任何生成的公钥。

任何线索我应该如何使用带有 pouchdb 的加密袋库。

【问题讨论】:

  • 我不太了解这个库,但它似乎确实有你所说的行为。我怀疑它可能会在出路时对其进行解密,但我不确定。出于这个原因,我创建了这个 GitHub 问题:github.com/calvinmetcalf/crypto-pouch/issues/21

标签: javascript encryption browser-cache offline pouchdb


【解决方案1】:

我尝试了combDB,它似乎是目前唯一可以与新的 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`)
})

或使用 Angular(打字稿)

import PouchDB from 'pouchdb-browser';

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

【讨论】:

    【解决方案2】:

    编辑:这个答案最初是指加密袋的 1.x 版本,但对于当前版本 (3.x) 不正确,在当前版本 db.crypto(password) 中确实如此不返回承诺,因此更新的代码示例是

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

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

    原始答案(对 v1.x 仍然有效)如下:

    所以文档有点混乱(我刚刚清理过),但是当您调用 db.crypto 时,它会包装数据库,以便透明地加密和解密文档

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

    它会透明地加密您创建的文档并解密您阅读的文档,直到您调用

    db.removeCrypto();
    

    所以如果你想测试做类似的事情

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

    【讨论】:

    • 嗨 Calvin,我期待在以下代码中提供给我的 publicKey 值 /*Start*/ db.crypto(password).then(function (publicKey) { console.log(publicKey); }); /*-- End--*/ 但它似乎给了我一个未定义的“publicKey”值..为什么会这样?。
    • 1) 当前的 url 'wzrd.in/standalone/crypto-pouch' 给了我一个 javascript 文件,它使用 nodejs 'require' 导入依赖项。如果我将此 javascript 文件复制到我的项目中(这是一个普通的 angularjs 页面),那么它会抱怨 nodejs 'require'。我应该怎么做才能在我的项目中使用您的库。我无法从我的项目中引用您的 url ('wzrd.in/standalone/crypto-pouch'),因为它是在没有 Internet 连接的 Intranet 环境中工作的。
    • 按顺序 1. publicKey 的东西是用于我要删除的带有 diffie-hellman 密钥的功能,忽略它,它也不适用于仅使用密码 2. 这不是要求的方式在该文件中工作,它们在内部用于捆绑但不涉及互联网连接
    猜你喜欢
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 2019-11-17
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多