add cache for keys
This commit is contained in:
parent
50d37aa22e
commit
65698e7e45
2 changed files with 18 additions and 8 deletions
|
|
@ -17,7 +17,7 @@ var config = {
|
||||||
maxPeers: 15,
|
maxPeers: 15,
|
||||||
// debug: 3,
|
// debug: 3,
|
||||||
sjclParams: {
|
sjclParams: {
|
||||||
salt: 'cc295e13e14edcc0', // choose your own salt (hex string)
|
salt: 'mjuBtGybi/4=', // choose your own salt (base64)
|
||||||
iter:500,
|
iter:500,
|
||||||
mode:'ccm',
|
mode:'ccm',
|
||||||
ts:parseInt(64),
|
ts:parseInt(64),
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,7 @@ Network.prototype.cleanUp = function() {
|
||||||
this.authenticatedPeers=[];
|
this.authenticatedPeers=[];
|
||||||
this.copayerForPeer={};
|
this.copayerForPeer={};
|
||||||
this.connections={};
|
this.connections={};
|
||||||
|
this.keyCache={};
|
||||||
if (this.peer) {
|
if (this.peer) {
|
||||||
console.log('## DESTROYING PEER INSTANCE'); //TODO
|
console.log('## DESTROYING PEER INSTANCE'); //TODO
|
||||||
this.peer.disconnect();
|
this.peer.disconnect();
|
||||||
|
|
@ -362,12 +363,22 @@ Network.prototype.getPeer = function() {
|
||||||
return this.peer;
|
return this.peer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Network.prototype._keyForCopayerId = function(copayerId) {
|
||||||
|
var key=this.keyCache[copayerId];
|
||||||
|
if (key) return key;
|
||||||
|
|
||||||
|
var cBuf = new Buffer(copayerId,'hex');
|
||||||
|
var key = bitcore.util.sha256(cBuf).toString('base64');
|
||||||
|
this.keyCache[copayerId] = key;
|
||||||
|
return key;
|
||||||
|
};
|
||||||
|
|
||||||
Network.prototype._encryptFor = function(copayerId, payloadStr) {
|
Network.prototype._encryptFor = function(copayerId, payloadStr) {
|
||||||
var cBits= sjcl.codec.hex.toBits(copayerId);
|
var key = this._keyForCopayerId(copayerId);
|
||||||
var pass64= sjcl.codec.base64.fromBits(cBits);
|
|
||||||
var plainText = sjcl.codec.utf8String.toBits(payloadStr);
|
var plainText = sjcl.codec.utf8String.toBits(payloadStr);
|
||||||
var p = this.sjclParams; // auth strength
|
var p = this.sjclParams; // auth strength
|
||||||
ct = sjcl.encrypt(pass64, plainText, p);//,p, rp);
|
ct = sjcl.encrypt(key, plainText, p);//,p, rp);
|
||||||
var c = JSON.parse(ct);
|
var c = JSON.parse(ct);
|
||||||
var toSend = {
|
var toSend = {
|
||||||
iv: c.iv,
|
iv: c.iv,
|
||||||
|
|
@ -383,9 +394,8 @@ Network.prototype._decrypt = function(encStr) {
|
||||||
i[k] = this.sjclParams[k];
|
i[k] = this.sjclParams[k];
|
||||||
}
|
}
|
||||||
var str= JSON.stringify(i);
|
var str= JSON.stringify(i);
|
||||||
var cBits= sjcl.codec.hex.toBits(this.copayerId);
|
var key= this._keyForCopayerId(this.copayerId);
|
||||||
var pass64= sjcl.codec.base64.fromBits(cBits);
|
var pt = sjcl.decrypt(key, str);
|
||||||
var pt = sjcl.decrypt(pass64, str);
|
|
||||||
return pt;
|
return pt;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue