From ecddf69fd08d050ca121c6123797a39b6f3e92bf Mon Sep 17 00:00:00 2001 From: Tor Brede Vekterli Date: Tue, 14 Feb 2023 13:34:51 +0100 Subject: Use explicit `equals` and `hashCode` to use contents of arrays, not just refs Also add a friendlier `toString()` that hex dumps the enc/ciphertext fields. --- .../java/com/yahoo/security/SealedSharedKey.java | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'security-utils/src/main') diff --git a/security-utils/src/main/java/com/yahoo/security/SealedSharedKey.java b/security-utils/src/main/java/com/yahoo/security/SealedSharedKey.java index 99d07465812..20745ab4312 100644 --- a/security-utils/src/main/java/com/yahoo/security/SealedSharedKey.java +++ b/security-utils/src/main/java/com/yahoo/security/SealedSharedKey.java @@ -2,6 +2,10 @@ package com.yahoo.security; import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Objects; + +import static com.yahoo.security.ArrayUtils.hex; /** * A SealedSharedKey represents the public part of a secure one-way ephemeral key exchange. @@ -97,4 +101,35 @@ public record SealedSharedKey(int version, KeyId keyId, byte[] enc, byte[] ciphe } } + // Friendlier toString() with hex dump of enc/ciphertext fields + @Override + public String toString() { + return "SealedSharedKey{" + + "version=" + version + + ", keyId=" + keyId + + ", enc=" + hex(enc) + + ", ciphertext=" + hex(ciphertext) + + '}'; + } + + // Explicitly generated equals() and hashCode() to use _contents_ of + // enc/ciphertext arrays, and not just their refs. + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + SealedSharedKey that = (SealedSharedKey) o; + return version == that.version && keyId.equals(that.keyId) && + Arrays.equals(enc, that.enc) && + Arrays.equals(ciphertext, that.ciphertext); + } + + @Override + public int hashCode() { + int result = Objects.hash(version, keyId); + result = 31 * result + Arrays.hashCode(enc); + result = 31 * result + Arrays.hashCode(ciphertext); + return result; + } + } -- cgit v1.2.3