added test for Hash
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
45
src/test/java/net/woggioni/jwo/hash/HashTest.java
Normal file
45
src/test/java/net/woggioni/jwo/hash/HashTest.java
Normal file
@@ -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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user