add encrypt/decrypt methods

This commit is contained in:
Ivan Socolsky 2015-02-17 22:50:12 -03:00
parent aa9c8e7ce6
commit 78033f7c97
3 changed files with 28 additions and 3 deletions

View File

@ -5,6 +5,7 @@ var PrivateKey = Bitcore.PrivateKey;
var PublicKey = Bitcore.PublicKey;
var crypto = Bitcore.crypto;
var HDPath = require('./hdpath');
var sjcl = require('sjcl');
function WalletUtils() {};
@ -61,8 +62,9 @@ WalletUtils.xPubToCopayerId = function(xpub) {
return (new Bitcore.HDPublicKey(xpub)).derive(HDPath.IdBranch).publicKey.toString();
};
<< << << < HEAD
WalletUtils.toSecret = function(walletId, walletPrivKey, network) {
return walletId + ':' + walletPrivKey.toWIF() + ':' + (network == 'testnet' ? 'T' : 'L');
return walletId + ':' + walletPrivKey.toWIF() + ':' + (network == 'testnet' ? 'T' : 'L');
};
WalletUtils.fromSecret = function(secret) {
@ -72,7 +74,7 @@ WalletUtils.fromSecret = function(secret) {
var networkChar = secretSplit[2];
return {
return {
walletId: walletId,
walletPrivKey: walletPrivKey,
network: networkChar == 'T' ? 'testnet' : 'livenet',
@ -80,6 +82,19 @@ WalletUtils.fromSecret = function(secret) {
};
WalletUtils.encryptMessage = function(message, password) {
var key = sjcl.codec.utf8String.toBits(password);
key = sjcl.bitArray.clamp(key, 256);
return sjcl.encrypt(key, message, {
ks: 256,
iter: 1
});
};
WalletUtils.decryptMessage = function(cyphertextJson, password) {
var key = sjcl.codec.utf8String.toBits(password);
key = sjcl.bitArray.clamp(key, 256);
return sjcl.decrypt(key, cyphertextJson);
};
module.exports = WalletUtils;

View File

@ -30,8 +30,9 @@
"morgan": "*",
"npmlog": "^0.1.1",
"preconditions": "^1.0.7",
"request": "^2.53.0",
"qr-image": "*",
"request": "^2.53.0",
"sjcl": "^1.0.2",
"uuid": "*"
},
"devDependencies": {

View File

@ -65,4 +65,13 @@ describe('WalletUtils', function() {
WalletUtils.verifyMessage(aLongerText, sig, aPubKey).should.equal(true);
});
});
describe('#encryptMessage #decryptMessage round trip', function() {
it('should encrypt and decrypt', function() {
var pwd = '0dea92f1df6675085b5cdd965487bb862f84f2755bcb56fa45dbf5b387a6c4a0';
var ct = WalletUtils.encryptMessage('hello world', pwd);
var msg = WalletUtils.decryptMessage(ct, pwd);
msg.should.equal('hello world');
});
});
});