From c3ab8c3dd9abd50e79baa9e5966f4e6a75ee1cc5 Mon Sep 17 00:00:00 2001 From: Walter Oggioni Date: Sat, 24 Jul 2021 20:01:41 +0200 Subject: [PATCH] added test for Hash --- src/main/java/net/woggioni/jwo/hash/Hash.java | 32 +++++-------- .../java/net/woggioni/jwo/hash/HashTest.java | 45 +++++++++++++++++++ 2 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 src/test/java/net/woggioni/jwo/hash/HashTest.java diff --git a/src/main/java/net/woggioni/jwo/hash/Hash.java b/src/main/java/net/woggioni/jwo/hash/Hash.java index e6627a6..9b6145b 100644 --- a/src/main/java/net/woggioni/jwo/hash/Hash.java +++ b/src/main/java/net/woggioni/jwo/hash/Hash.java @@ -1,12 +1,14 @@ package net.woggioni.jwo.hash; +import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import java.io.InputStream; import java.security.MessageDigest; -import java.util.Arrays; +@EqualsAndHashCode @RequiredArgsConstructor public class Hash { @@ -22,26 +24,11 @@ public class Hash { private final String key; } - final Algorithm algorithm; - final byte[] bytes; + @Getter + private final Algorithm algorithm; - @Override - public boolean equals(Object other) { - if(other == null) return false; - else if(getClass() != other.getClass()) return false; - Hash otherHash = (Hash) other; - if(algorithm != otherHash.algorithm) return false; - return Arrays.equals(bytes, otherHash.bytes); - } - - @Override - public int hashCode() { - int result = algorithm.hashCode(); - for(byte b : bytes) { - result ^= b; - } - return result; - } + @Getter + private final byte[] bytes; @SneakyThrows public static Hash hash(Algorithm algo, InputStream is, byte[] buffer) { @@ -83,4 +70,9 @@ public class Hash { } return new String(hexChars); } + + @Override + public String toString() { + return bytesToHex(bytes); + } } \ No newline at end of file diff --git a/src/test/java/net/woggioni/jwo/hash/HashTest.java b/src/test/java/net/woggioni/jwo/hash/HashTest.java new file mode 100644 index 0000000..30a9ea1 --- /dev/null +++ b/src/test/java/net/woggioni/jwo/hash/HashTest.java @@ -0,0 +1,45 @@ +package net.woggioni.jwo.hash; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.util.Random; + +public class HashTest { + + private byte[] sample; + + @BeforeEach + public void setup() { + sample = new byte[0x400]; + Random random = new Random(0xdeadbeef); + random.nextBytes(sample); + } + + @Test + public void checkHashIsRepeatableAndComparesCorrectly() { + byte[] buffer = new byte[0x1000]; + Hash hash1 = Hash.md5(new ByteArrayInputStream(sample), buffer); + Hash hash2 = Hash.md5(new ByteArrayInputStream(sample), buffer); + Assertions.assertEquals(hash1.hashCode(), hash2.hashCode()); + Assertions.assertEquals(hash1, hash2); + Assertions.assertEquals(hash1.toString(), hash2.toString()); + } + + @Test + public void checkReversingTheSampleGivesDifferentHash() { + byte[] reverseSample = new byte[sample.length]; + for(int i = 0; i < sample.length; i++) { + reverseSample[reverseSample.length - i - 1] = sample[i]; + } + + byte[] buffer = new byte[0x1000]; + Hash hash1 = Hash.md5(new ByteArrayInputStream(sample), buffer); + Hash hash2 = Hash.md5(new ByteArrayInputStream(reverseSample), buffer); + Assertions.assertNotEquals(hash1.hashCode(), hash2.hashCode()); + Assertions.assertNotEquals(hash1, hash2); + Assertions.assertNotEquals(hash1.toString(), hash2.toString()); + } +}