From ed335f35f778225b9a0bedb0e5a2922284100f5a Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Tue, 16 Sep 2014 11:33:49 -0700 Subject: [PATCH] throw error if hashbuf is not 32 bytes --- lib/ecdsa.js | 3 +++ test/ecdsa.js | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/ecdsa.js b/lib/ecdsa.js index 14eee0073..b22c409ce 100644 --- a/lib/ecdsa.js +++ b/lib/ecdsa.js @@ -156,6 +156,9 @@ ECDSA.prototype.sign = function() { if (!hashbuf || !privkey || !d) throw new Error('invalid parameters'); + if (!Buffer.isBuffer(hashbuf) || hashbuf.length !== 32) + throw new Error('hashbuf must be a 32 byte buffer'); + var N = Point.getN(); var G = Point.getG(); var e = BN().fromBuffer(hashbuf); diff --git a/test/ecdsa.js b/test/ecdsa.js index c285a4d9e..0291e21e7 100644 --- a/test/ecdsa.js +++ b/test/ecdsa.js @@ -144,6 +144,17 @@ describe("ECDSA", function() { ecdsa.verify().should.equal(true); }); + it('should should throw an error if hashbuf is not 32 bytes', function() { + var ecdsa2 = ECDSA().set({ + hashbuf: ecdsa.hashbuf.slice(0, 31), + keypair: ecdsa.keypair + }); + ecdsa2.randomK(); + (function() { + ecdsa2.sign(); + }).should.throw('hashbuf must be a 32 byte buffer'); + }); + }); describe('#signRandomK', function() {