From 9417496d3d172101ed8c7940d9fecb3758d5f05e Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Fri, 22 Mar 2024 16:23:24 +0200 Subject: [PATCH] Add more tests for signatures --- test.js | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/test.js b/test.js index 35854c3..98664ae 100644 --- a/test.js +++ b/test.js @@ -34,6 +34,13 @@ var UserAgent = bitmessage.UserAgent; var skipPow = {skipPow: true}; describe("Crypto", function() { + // The samples are from PyBitmessage's tests + var samplePublicKey = bmcrypto.getPublic(Buffer.from( + "93d0b61371a54b53df143b954035d612f8efa8a3ed1cf842c2186bfd8f876665", "hex")), + sampleSigSHA1 = Buffer.from("30460221008ad234687d1bdc259932e28ea6ee091b88b0900d8134902aa8c2fd7f016b96ed022100dafb94e28322c2fa88878f9dcbf0c2d33270466ab3bbffaec3dca0a2d1ef9354", "hex"), + sampleSigSHA256 = Buffer.from("304402202302475351db6b822de15d922e29397541f10d8a19780ba2ca4a920b1035f07502205e5bba40d5f07a24c23a89ba5f01a3828371dfbb685dd5375fa1c29095fd232b", "hex"), + sampleMessage = Buffer.from("0592a10584ffabf96539f3d780d776828c67da1ab5b169e9e8aed838aaecc9ed36d49ff1423c55f019e050c66c6324f53588be88894fef4dcffdb74b98e2b200", "hex"); + it("should implement SHA-1 hash", function() { expect(bmcrypto.sha1(Buffer.from("test")).toString("hex")).to.equal("a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"); }); @@ -101,14 +108,28 @@ describe("Crypto", function() { }); }); - // The samples are from PyBitmessage's tests + it("should verify sha1 signatures", function() { + return bmcrypto.verify(samplePublicKey, sampleMessage, sampleSigSHA1); + }); + it("should verify sha256 signatures", function() { - var publicKey = bmcrypto.getPublic(Buffer.from( - "93d0b61371a54b53df143b954035d612f8efa8a3ed1cf842c2186bfd8f876665", - "hex")), - sig = Buffer.from("304402202302475351db6b822de15d922e29397541f10d8a19780ba2ca4a920b1035f07502205e5bba40d5f07a24c23a89ba5f01a3828371dfbb685dd5375fa1c29095fd232b", "hex"), - message = Buffer.from("0592a10584ffabf96539f3d780d776828c67da1ab5b169e9e8aed838aaecc9ed36d49ff1423c55f019e050c66c6324f53588be88894fef4dcffdb74b98e2b200", "hex"); - return bmcrypto.verify(publicKey, message, sig); + return bmcrypto.verify(samplePublicKey, sampleMessage, sampleSigSHA256); + }); + + it("should throw on a bad signature", function(done) { + bmcrypto.verify( + samplePublicKey, sampleMessage, sampleSigSHA1.slice(0, 40) + ).catch(function() { + done(); + }); + }); + + it("should throw trying to verify a signature with bad pubkey", function(done) { + bmcrypto.verify( + samplePublicKey.slice(2), sampleMessage, sampleSigSHA1 + ).catch(function() { + done(); + }); }); it("should allow to encrypt and decrypt message", function() {