add encrypt/decrypt methods
This commit is contained in:
parent
aa9c8e7ce6
commit
78033f7c97
|
@ -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,6 +62,7 @@ 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');
|
||||
};
|
||||
|
@ -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;
|
||||
|
|
|
@ -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": {
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue