From 211966b17bce0117fe94527d2467e3f9947aba9d Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Fri, 19 Dec 2014 02:29:23 +0100 Subject: [PATCH] ECKey: add throwing variants of the verify methods. Can help avoid mistakes when ignoring the result of verify. --- .../main/java/org/bitcoinj/core/ECKey.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/core/src/main/java/org/bitcoinj/core/ECKey.java b/core/src/main/java/org/bitcoinj/core/ECKey.java index 8c95da16d..83e9641ec 100644 --- a/core/src/main/java/org/bitcoinj/core/ECKey.java +++ b/core/src/main/java/org/bitcoinj/core/ECKey.java @@ -695,6 +695,26 @@ public class ECKey implements EncryptableItem, Serializable { return ECKey.verify(sigHash.getBytes(), signature, getPubKey()); } + /** + * Verifies the given ASN.1 encoded ECDSA signature against a hash using the public key, and throws an exception + * if the signature doesn't match + * @throws java.security.SignatureException if the signature does not match. + */ + public void verifyOrThrow(byte[] hash, byte[] signature) throws SignatureException { + if (!verify(hash, signature)) + throw new SignatureException(); + } + + /** + * Verifies the given R/S pair (signature) against a hash using the public key, and throws an exception + * if the signature doesn't match + * @throws java.security.SignatureException if the signature does not match. + */ + public void verifyOrThrow(Sha256Hash sigHash, ECDSASignature signature) throws SignatureException { + if (!ECKey.verify(sigHash.getBytes(), signature, getPubKey())) + throw new SignatureException(); + } + /** * Returns true if the given pubkey is canonical, i.e. the correct length taking into account compression. */