From 640db52cf48416db8e2b24b502b3775243ad5162 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Thu, 1 Nov 2012 12:05:30 +0100 Subject: [PATCH] Implement equals/hashCode on ECKey. Resolves issue 254. --- .../java/com/google/bitcoin/core/ECKey.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/core/src/main/java/com/google/bitcoin/core/ECKey.java b/core/src/main/java/com/google/bitcoin/core/ECKey.java index e50ec5d1..dab865b9 100644 --- a/core/src/main/java/com/google/bitcoin/core/ECKey.java +++ b/core/src/main/java/com/google/bitcoin/core/ECKey.java @@ -32,6 +32,7 @@ import java.io.IOException; import java.io.Serializable; import java.math.BigInteger; import java.security.SecureRandom; +import java.util.Arrays; import static com.google.common.base.Preconditions.checkArgument; @@ -335,4 +336,23 @@ public class ECKey implements Serializable { throw new IllegalArgumentException("Cannot set creation time to negative value: " + newCreationTimeSeconds); creationTimeSeconds = newCreationTimeSeconds; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ECKey ecKey = (ECKey) o; + + if (!Arrays.equals(pub, ecKey.pub)) return false; + + return true; + } + + @Override + public int hashCode() { + // Public keys are random already so we can just use a part of them as the hashcode. Read from the start to + // avoid picking up the type code (compressed vs uncompressed) which is tacked on the end. + return (pub[0] & 0xFF) | ((pub[1] & 0xFF) << 8) | ((pub[2] & 0xFF) << 16) | ((pub[3] & 0xFF) << 24); + } }