aes.
This commit is contained in:
parent
839f915fb7
commit
aa3579c8cd
2
bin/node
2
bin/node
@ -10,7 +10,7 @@ process.on('uncaughtException', function(err) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
var node = bcoin.fullnode({
|
var node = bcoin.fullnode({
|
||||||
passphrase: 'node',
|
// passphrase: 'node',
|
||||||
prune: process.argv.indexOf('--prune') !== -1,
|
prune: process.argv.indexOf('--prune') !== -1,
|
||||||
useCheckpoints: process.argv.indexOf('--checkpoints') !== -1,
|
useCheckpoints: process.argv.indexOf('--checkpoints') !== -1,
|
||||||
listen: process.argv.indexOf('--listen') !== -1,
|
listen: process.argv.indexOf('--listen') !== -1,
|
||||||
|
|||||||
@ -5,7 +5,7 @@ var utils = bcoin.utils;
|
|||||||
var assert = utils.assert;
|
var assert = utils.assert;
|
||||||
|
|
||||||
var node = bcoin.spvnode({
|
var node = bcoin.spvnode({
|
||||||
passphrase: 'node',
|
// passphrase: 'node',
|
||||||
preload: process.argv.indexOf('--preload') !== -1,
|
preload: process.argv.indexOf('--preload') !== -1,
|
||||||
useCheckpoints: process.argv.indexOf('--checkpoints') !== -1
|
useCheckpoints: process.argv.indexOf('--checkpoints') !== -1
|
||||||
});
|
});
|
||||||
|
|||||||
@ -863,14 +863,14 @@ HDPrivateKey.prototype.toJSON = function toJSON(passphrase) {
|
|||||||
json.encrypted = passphrase ? true : false;
|
json.encrypted = passphrase ? true : false;
|
||||||
if (this.mnemonic) {
|
if (this.mnemonic) {
|
||||||
json.phrase = passphrase
|
json.phrase = passphrase
|
||||||
? utils.encrypt(this.mnemonic.phrase, passphrase)
|
? utils.encrypt(this.mnemonic.phrase, passphrase).toString('hex')
|
||||||
: this.mnemonic.phrase;
|
: this.mnemonic.phrase;
|
||||||
json.passphrase = passphrase
|
json.passphrase = passphrase
|
||||||
? utils.encrypt(this.mnemonic.passphrase, passphrase)
|
? utils.encrypt(this.mnemonic.passphrase, passphrase).toString('hex')
|
||||||
: this.mnemonic.passphrase;
|
: this.mnemonic.passphrase;
|
||||||
}
|
}
|
||||||
json.xprivkey = passphrase
|
json.xprivkey = passphrase
|
||||||
? utils.encrypt(this.xprivkey, passphrase)
|
? utils.encrypt(this.xprivkey, passphrase).toString('hex')
|
||||||
: this.xprivkey;
|
: this.xprivkey;
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
@ -898,10 +898,10 @@ HDPrivateKey.parseJSON = function parseJSON(json, passphrase) {
|
|||||||
if (json.phrase) {
|
if (json.phrase) {
|
||||||
data.mnemonic = {
|
data.mnemonic = {
|
||||||
phrase: json.encrypted
|
phrase: json.encrypted
|
||||||
? utils.decrypt(json.phrase, passphrase)
|
? utils.decrypt(json.phrase, passphrase).toString('utf8')
|
||||||
: json.phrase,
|
: json.phrase,
|
||||||
passphrase: json.encrypted
|
passphrase: json.encrypted
|
||||||
? utils.decrypt(json.passphrase, passphrase)
|
? utils.decrypt(json.passphrase, passphrase).toString('utf8')
|
||||||
: json.passphrase
|
: json.passphrase
|
||||||
};
|
};
|
||||||
if (!json.xprivkey)
|
if (!json.xprivkey)
|
||||||
@ -910,7 +910,7 @@ HDPrivateKey.parseJSON = function parseJSON(json, passphrase) {
|
|||||||
|
|
||||||
if (json.xprivkey) {
|
if (json.xprivkey) {
|
||||||
data.xprivkey = json.encrypted
|
data.xprivkey = json.encrypted
|
||||||
? utils.decrypt(json.xprivkey, passphrase)
|
? utils.decrypt(json.xprivkey, passphrase).toString('utf8')
|
||||||
: json.xprivkey;
|
: json.xprivkey;
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -207,7 +207,7 @@ KeyPair.prototype.toJSON = function toJSON(passphrase) {
|
|||||||
|
|
||||||
if (this.key.privateKey) {
|
if (this.key.privateKey) {
|
||||||
json.privateKey = passphrase
|
json.privateKey = passphrase
|
||||||
? utils.encrypt(this.toSecret(), passphrase)
|
? utils.encrypt(this.toSecret(), passphrase).toString('hex')
|
||||||
: this.toSecret();
|
: this.toSecret();
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
@ -237,7 +237,7 @@ KeyPair.parseJSON = function parseJSON(json, passphrase) {
|
|||||||
if (json.privateKey) {
|
if (json.privateKey) {
|
||||||
privateKey = json.privateKey;
|
privateKey = json.privateKey;
|
||||||
if (json.encrypted)
|
if (json.encrypted)
|
||||||
privateKey = utils.decrypt(privateKey, passphrase);
|
privateKey = utils.decrypt(privateKey, passphrase).toString('utf8');
|
||||||
return KeyPair.parseSecret(privateKey);
|
return KeyPair.parseSecret(privateKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -363,72 +363,64 @@ utils.pbkdf2 = function pbkdf2(key, salt, iterations, dkLen) {
|
|||||||
* @const
|
* @const
|
||||||
*/
|
*/
|
||||||
|
|
||||||
utils.salt = 'bcoin:';
|
utils.salt = new Buffer('bcoin:', 'ascii');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypt a string.
|
* Encrypt with aes-256-cbc.
|
||||||
* @param {String} data
|
* @param {Buffer|String} data
|
||||||
* @param {String} passphrase
|
* @param {String} passphrase
|
||||||
* @returns {String} Hex string.
|
* @returns {Buffer} Hex string.
|
||||||
* @throws on no passphrase
|
* @throws on no passphrase
|
||||||
*/
|
*/
|
||||||
|
|
||||||
utils.encrypt = function encrypt(data, passphrase) {
|
utils.encrypt = function encrypt(data, passphrase) {
|
||||||
var cipher, out;
|
var cipher, out;
|
||||||
|
|
||||||
if (!crypto)
|
assert(crypto, 'No crypto module available.');
|
||||||
return data;
|
assert(passphrase, 'No passphrase.');
|
||||||
|
|
||||||
if (data[0] === ':')
|
if (typeof data === 'string')
|
||||||
return data;
|
data = new Buffer(data, 'utf8');
|
||||||
|
|
||||||
if (!passphrase)
|
|
||||||
throw new Error('No passphrase.');
|
|
||||||
|
|
||||||
cipher = crypto.createCipher('aes-256-cbc', passphrase);
|
cipher = crypto.createCipher('aes-256-cbc', passphrase);
|
||||||
|
|
||||||
out = '';
|
out = Buffer.concat([
|
||||||
out += cipher.update(utils.salt + data, 'utf8', 'hex');
|
cipher.update(utils.salt),
|
||||||
out += cipher.final('hex');
|
cipher.update(data),
|
||||||
|
cipher.final()
|
||||||
|
]);
|
||||||
|
|
||||||
return ':' + out;
|
return out;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrypt an encrypted hex string.
|
* Decrypt from aes-256-cbc.
|
||||||
* @param {String} data - Hex string.
|
* @param {Buffer|String} data
|
||||||
* @param {String} passphrase
|
* @param {String} passphrase
|
||||||
* @returns {String}
|
* @returns {Buffer}
|
||||||
* @throws on bad decrypt.
|
* @throws on bad decrypt.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
utils.decrypt = function decrypt(data, passphrase) {
|
utils.decrypt = function decrypt(data, passphrase) {
|
||||||
var decipher, out;
|
var decipher, out;
|
||||||
|
|
||||||
if (!crypto) {
|
assert(crypto, 'No crypto module available.');
|
||||||
if (data[0] === ':')
|
assert(passphrase, 'No passphrase.');
|
||||||
throw new Error('Cannot decrypt.');
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data[0] !== ':')
|
if (typeof data === 'string')
|
||||||
return data;
|
data = new Buffer(data, 'hex');
|
||||||
|
|
||||||
if (!passphrase)
|
|
||||||
throw new Error('No passphrase.');
|
|
||||||
|
|
||||||
data = data.substring(1);
|
|
||||||
|
|
||||||
decipher = crypto.createDecipher('aes-256-cbc', passphrase);
|
decipher = crypto.createDecipher('aes-256-cbc', passphrase);
|
||||||
|
|
||||||
out = '';
|
out = Buffer.concat([
|
||||||
out += decipher.update(data, 'hex', 'utf8');
|
decipher.update(data),
|
||||||
out += decipher.final('utf8');
|
decipher.final()
|
||||||
|
]);
|
||||||
|
|
||||||
if (out.indexOf(utils.salt) !== 0)
|
if (utils.icmp(out, utils.salt, 0) !== 0)
|
||||||
throw new Error('Decrypt failed.');
|
throw new Error('Decrypt failed.');
|
||||||
|
|
||||||
out = out.substring(utils.salt.length);
|
out = out.slice(utils.salt.length);
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user