From ddbf6d282c008c3f89c76051ae2843575a95dc94 Mon Sep 17 00:00:00 2001 From: Mark Lowe Date: Wed, 24 Jun 2020 14:19:48 +0100 Subject: [PATCH] Only print base64 string value for a key if public. --- .../quorum/tessera/encryption/BaseKey.java | 6 ++--- .../tessera/encryption/PublicKeyImpl.java | 16 +++++++++++++- .../quorum/tessera/encryption/KeyTest.java | 22 ++++++++++++++----- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/encryption/encryption-api/src/main/java/com/quorum/tessera/encryption/BaseKey.java b/encryption/encryption-api/src/main/java/com/quorum/tessera/encryption/BaseKey.java index 242d20ead2..77e33b0025 100644 --- a/encryption/encryption-api/src/main/java/com/quorum/tessera/encryption/BaseKey.java +++ b/encryption/encryption-api/src/main/java/com/quorum/tessera/encryption/BaseKey.java @@ -33,16 +33,16 @@ public final int hashCode() { } @Override - public final String toString() { + public String toString() { final String typeName = Stream.of(getClass()) .map(Class::getInterfaces) .flatMap(Stream::of) - .map(Class::getSimpleName) + .map(Class::getName) .findFirst() .get(); - return String.format("%s[%s]", typeName, encodeToBase64()); + return typeName + "@" + Integer.toHexString(hashCode()); } } diff --git a/encryption/encryption-api/src/main/java/com/quorum/tessera/encryption/PublicKeyImpl.java b/encryption/encryption-api/src/main/java/com/quorum/tessera/encryption/PublicKeyImpl.java index 803d52bb20..0c989ed82c 100644 --- a/encryption/encryption-api/src/main/java/com/quorum/tessera/encryption/PublicKeyImpl.java +++ b/encryption/encryption-api/src/main/java/com/quorum/tessera/encryption/PublicKeyImpl.java @@ -2,10 +2,24 @@ package com.quorum.tessera.encryption; +import java.util.stream.Stream; + class PublicKeyImpl extends BaseKey implements PublicKey { PublicKeyImpl(byte[] keyBytes) { super(keyBytes); } - + + @Override + public final String toString() { + + final String typeName = Stream.of(getClass()) + .map(Class::getInterfaces) + .flatMap(Stream::of) + .map(Class::getSimpleName) + .findFirst() + .get(); + + return String.format("%s[%s]", typeName, encodeToBase64()); + } } diff --git a/encryption/encryption-api/src/test/java/com/quorum/tessera/encryption/KeyTest.java b/encryption/encryption-api/src/test/java/com/quorum/tessera/encryption/KeyTest.java index b05d50fb53..07ac725cbc 100644 --- a/encryption/encryption-api/src/test/java/com/quorum/tessera/encryption/KeyTest.java +++ b/encryption/encryption-api/src/test/java/com/quorum/tessera/encryption/KeyTest.java @@ -1,13 +1,15 @@ package com.quorum.tessera.encryption; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + import java.lang.reflect.Method; import java.util.Arrays; import java.util.Base64; import java.util.List; -import static org.assertj.core.api.Assertions.*; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; + +import static org.assertj.core.api.Assertions.assertThat; @RunWith(Parameterized.class) public class KeyTest { @@ -34,7 +36,15 @@ public void doTests() throws Exception { assertThat(key.encodeToBase64()).isEqualTo(base64Value); - assertThat(key.toString()).isEqualTo(type.getSimpleName() + "[" + base64Value + "]"); + if(this.type == PublicKey.class) { + assertThat(key.toString()) + .isEqualTo("PublicKey[" + base64Value + "]"); + } else { + assertThat(key.toString()) + .isNotNull() + .doesNotContain(base64Value) + .contains(type.getSimpleName()); + } assertThat(key.hashCode()).isEqualTo(Arrays.hashCode(data)); @@ -50,7 +60,7 @@ public void doTests() throws Exception { assertThat(key).isEqualTo(otherKeyWithSameData); } - @Parameterized.Parameters + @Parameterized.Parameters(name = "{0}") public static List> cases() { return Arrays.asList(MasterKey.class, SharedKey.class, PrivateKey.class, PublicKey.class); }