From 46271de06ec384da58883c5ecd14a406740f05cb Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Thu, 1 May 2014 12:27:55 -0400 Subject: [PATCH] add some basic sanity tests for signatures and key generation Signing a message with ECDSA involves using a random number, and this means that two signatures should never be the same. This commit adds some basic tests to Key.signSync and Message.sign to make sure this is the case. Also, a new bitcore.Key should never have the same private key twice. This commit also adds a basic test to make sure that is the case. --- test/test.Key.js | 18 ++++++++++++++++++ test/test.Message.js | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/test/test.Key.js b/test/test.Key.js index 87fce8f..09a631d 100644 --- a/test/test.Key.js +++ b/test/test.Key.js @@ -130,7 +130,25 @@ describe('Key', function() { ret.should.equal(false); }); + describe('generateSync', function() { + it('should not generate the same key twice in a row', function() { + var key1 = Key.generateSync(); + var key2 = Key.generateSync(); + key1.private.toString('hex').should.not.equal(key2.private.toString('hex')); + }); + }); + describe('signSync', function() { + it('should not generate the same signature twice in a row', function() { + var hash = coinUtil.sha256('my data'); + var key = new Key(); + key.private = coinUtil.sha256('a fake private key'); + key.regenerateSync(); + var sig1 = key.signSync(hash); + var sig2 = key.signSync(hash); + sig1.toString('hex').should.not.equal(sig2.toString('hex')); + }); + it('should sign 10 times and have a different signature each time', function() { var key = new Key(); key.private = coinUtil.sha256('my fake private key'); diff --git a/test/test.Message.js b/test/test.Message.js index 9ec581f..413cba8 100644 --- a/test/test.Message.js +++ b/test/test.Message.js @@ -13,6 +13,15 @@ describe('Message', function() { var sig = Message.sign('my message', key); sig.length.should.be.greaterThan(0); }); + + it('should not sign a message the same way twice', function() { + var key = new bitcore.Key(); + key.private = coinUtil.sha256('a fake private key'); + key.regenerateSync(); + var sig1 = Message.sign('my message', key); + var sig2 = Message.sign('my message', key); + sig1.toString('hex').should.not.equal(sig2.toString('hex')); + }); }); describe('verifyWithPubKey', function() {