From a52f239f3cd7edd9416fdff0594c85aadbed1755 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 12 Sep 2016 22:33:24 -0700 Subject: [PATCH] crypto: always use buffers for hmacs. --- lib/crypto/crypto.js | 11 +++-------- lib/hd/private.js | 3 ++- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/crypto/crypto.js b/lib/crypto/crypto.js index 9447ff08..3575e0d1 100644 --- a/lib/crypto/crypto.js +++ b/lib/crypto/crypto.js @@ -126,14 +126,6 @@ crypto.checksum = function checksum(data) { crypto.hmac = function hmac(alg, data, salt) { var hmac; - if (native) { - if (typeof data === 'string') - data = new Buffer(data, 'utf8'); - if (typeof salt === 'string') - salt = new Buffer(salt, 'utf8'); - return native.hmac(alg, data, salt); - } - if (!nativeCrypto) { hmac = hash.hmac(hash[alg], salt); return new Buffer(hmac.update(data).digest()); @@ -143,6 +135,9 @@ crypto.hmac = function hmac(alg, data, salt) { return hmac.update(data).digest(); }; +if (native) + crypto.hmac = native.hmac; + /** * Perform key stretching using PBKDF2. * @param {Buffer} key diff --git a/lib/hd/private.js b/lib/hd/private.js index 846c42b2..1cb33861 100644 --- a/lib/hd/private.js +++ b/lib/hd/private.js @@ -23,6 +23,7 @@ var HD = require('./hd'); */ var FINGER_PRINT = new Buffer('00000000', 'hex'); +var SEED_SALT = new Buffer('Bitcoin seed', 'ascii'); /** * HDPrivateKey @@ -481,7 +482,7 @@ HDPrivateKey.prototype.fromSeed = function fromSeed(seed, network) { throw new Error('Entropy not in range.'); } - hash = crypto.hmac('sha512', seed, 'Bitcoin seed'); + hash = crypto.hmac('sha512', seed, SEED_SALT); left = hash.slice(0, 32); right = hash.slice(32, 64);