2016-03-02 55 views
4

背景:如何加密pouchdb數據庫

我試圖加密使用加密袋庫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"); 
 
    \t  console.log(publicKey); 
 
     }); 
 
    
 
     /* db.removeCrypto(); */ 
 

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

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

 
    </script> 
 

 
</head> 
 
<body> 
 

 
</body> 
 

 
</html>

但我的代碼沒有看到做任何數據加密輸入,或者我看不到生成的任何公共密鑰。

任何線索我應該如何在pouchdb中使用crypto-pouch庫。

+0

我不知道回頭看這個圖書館,但它似乎有你說的行爲。我懷疑它可能在出路時沒有加密,但我不確定。由於這個原因,我創建了這個GitHub問題:https://github.com/calvinmetcalf/crypto-pouch/issues/21 – JustGage

回答

2

編輯:此答案最初是針對crypto pouch 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); 
}) 

原來的答覆(仍然有效1.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); 
}) 
+0

嗨Calvin,我期待在以下代碼中提供給我的publicKey值:* Start */db。 (密碼)。然後(函數(公鑰){console.log(publicKey); });/* - 結束 - * /但它似乎給了'publicKey'值的一個未定義的值。爲什麼? – user1455719

+0

1)當前url'http://wzrd.in/standalone/crypto-pouch'給了我一個JavaScript文件,它使用nodejs'require'導入依賴關係。如果我將這個JavaScript文件複製到我的項目(這是一個普通的angularjs頁面),那麼它會抱怨nodejs'require'。我應該如何在我的項目中使用您的庫。我無法從我的項目中引用您的url('http://wzrd.in/standalone/crypto-pouch'),因爲它意味着在沒有Internet連接的Intranet環境中工作。 – user1455719

+0

爲了 1. publicKey的東西是與我正在刪除diffie-hellman鍵的功能,忽略它,它也不會適用於只使用密碼 2。這不是要求在該文件中工作的方式,它們在內部用於捆綁,但不涉及互聯網連接 – Calvin