From bb52b1c492b11d611a57edaaaca3f3d702385792 Mon Sep 17 00:00:00 2001 From: nicksavers Date: Fri, 31 Oct 2014 21:43:09 +0100 Subject: [PATCH] Add RIPEMD160 with separete HashUtil unit tests --- .../java/org/ethereum/crypto/HashUtil.java | 28 ++++-- .../java/org/ethereum/crypto/SHA3Helper.java | 8 +- .../java/org/ethereum/util/HashUtilTest.java | 85 +++++++++++++++++++ 3 files changed, 110 insertions(+), 11 deletions(-) create mode 100644 ethereumj-core/src/test/java/org/ethereum/util/HashUtilTest.java diff --git a/ethereumj-core/src/main/java/org/ethereum/crypto/HashUtil.java b/ethereumj-core/src/main/java/org/ethereum/crypto/HashUtil.java index b21bc138..fb58f078 100644 --- a/ethereumj-core/src/main/java/org/ethereum/crypto/HashUtil.java +++ b/ethereumj-core/src/main/java/org/ethereum/crypto/HashUtil.java @@ -9,6 +9,8 @@ import java.security.NoSuchAlgorithmException; import org.ethereum.db.ByteArrayWrapper; import org.ethereum.util.RLP; import org.ethereum.util.Utils; +import org.spongycastle.crypto.Digest; +import org.spongycastle.crypto.digests.RIPEMD160Digest; import org.spongycastle.util.encoders.Hex; import org.ethereum.util.LRUMap; @@ -34,14 +36,26 @@ public class HashUtil { } public static byte[] sha3(byte[] input) { - ByteArrayWrapper inputByteArray = new ByteArrayWrapper(input); - byte[] result = sha3Cache.get(inputByteArray); - if(result != null) - return result; - result = SHA3Helper.sha3(input); - sha3Cache.put(inputByteArray, result); - return result; + ByteArrayWrapper inputByteArray = new ByteArrayWrapper(input); + byte[] result = sha3Cache.get(inputByteArray); + if(result != null) + return result; + result = SHA3Helper.sha3(input); + sha3Cache.put(inputByteArray, result); + return result; } + + public static byte[] ripemd160(byte[] message) { + Digest digest = new RIPEMD160Digest(); + if (message != null) { + byte[] resBuf = new byte[digest.getDigestSize()]; + digest.update(message, 0, message.length); + digest.doFinal(resBuf, 0); + return resBuf; + } + throw new NullPointerException("Can't hash a NULL value"); + } + /** * Calculates RIGTMOST160(SHA3(input)). This is used in address calculations. diff --git a/ethereumj-core/src/main/java/org/ethereum/crypto/SHA3Helper.java b/ethereumj-core/src/main/java/org/ethereum/crypto/SHA3Helper.java index 2bba8741..8ef39d46 100644 --- a/ethereumj-core/src/main/java/org/ethereum/crypto/SHA3Helper.java +++ b/ethereumj-core/src/main/java/org/ethereum/crypto/SHA3Helper.java @@ -47,7 +47,7 @@ public class SHA3Helper { private static String sha3String(String message, SHA3Digest digest, boolean bouncyencoder) { if (message != null) { - return sha3String(Hex.decode(message), digest, bouncyencoder); + return sha3String(Hex.decode(message), digest, bouncyencoder); } throw new NullPointerException("Can't hash a NULL value"); } @@ -55,10 +55,10 @@ public class SHA3Helper { private static String sha3String(byte[] message, SHA3Digest digest, boolean bouncyencoder) { byte[] hash = doSha3(message, digest, bouncyencoder); if (bouncyencoder) { - return Hex.toHexString(hash); + return Hex.toHexString(hash); } else { - BigInteger bigInt = new BigInteger(1, hash); - return bigInt.toString(16); + BigInteger bigInt = new BigInteger(1, hash); + return bigInt.toString(16); } } diff --git a/ethereumj-core/src/test/java/org/ethereum/util/HashUtilTest.java b/ethereumj-core/src/test/java/org/ethereum/util/HashUtilTest.java new file mode 100644 index 00000000..9f4db45d --- /dev/null +++ b/ethereumj-core/src/test/java/org/ethereum/util/HashUtilTest.java @@ -0,0 +1,85 @@ +package org.ethereum.util; + +import org.ethereum.crypto.HashUtil; +import org.junit.Test; +import org.spongycastle.util.encoders.Hex; + +import static org.junit.Assert.assertEquals; + +public class HashUtilTest { + + @Test + public void testSha256_EmptyString() { + String expected1 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; + String result1 = Hex.toHexString(HashUtil.sha256(new byte[0])); + assertEquals(expected1, result1); + } + + @Test + public void testSha256_Test() { + String expected2 = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"; + String result2 = Hex.toHexString(HashUtil.sha256("test".getBytes())); + assertEquals(expected2, result2); + } + + @Test + public void testSha256_Multiple() { + String expected1 = "1b4f0e9851971998e732078544c96b36c3d01cedf7caa332359d6f1d83567014"; + String result1 = Hex.toHexString(HashUtil.sha256("test1".getBytes())); + assertEquals(expected1, result1); + + String expected2 = "60303ae22b998861bce3b28f33eec1be758a213c86c93c076dbe9f558c11c752"; + String result2 = Hex.toHexString(HashUtil.sha256("test2".getBytes())); + assertEquals(expected2, result2); + } + + @Test + public void testSha3_EmptyString() { + String expected1 = "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"; + String result1 = Hex.toHexString(HashUtil.sha3(new byte[0])); + assertEquals(expected1, result1); + } + + @Test + public void testSha3_Test() { + String expected2 = "9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658"; + String result2 = Hex.toHexString(HashUtil.sha3("test".getBytes())); + assertEquals(expected2, result2); + } + + @Test + public void testSha3_Multiple() { + String expected1 = "6d255fc3390ee6b41191da315958b7d6a1e5b17904cc7683558f98acc57977b4"; + String result1 = Hex.toHexString(HashUtil.sha3("test1".getBytes())); + assertEquals(expected1, result1); + + String expected2 = "4da432f1ecd4c0ac028ebde3a3f78510a21d54087b161590a63080d33b702b8d"; + String result2 = Hex.toHexString(HashUtil.sha3("test2".getBytes())); + assertEquals(expected2, result2); + } + + @Test + public void testRIPEMD160_EmptyString() { + String expected1 = "9c1185a5c5e9fc54612808977ee8f548b2258d31"; + String result1 = Hex.toHexString(HashUtil.ripemd160(new byte[0])); + assertEquals(expected1, result1); + } + + @Test + public void testRIPEMD160_Test() { + String expected2 = "5e52fee47e6b070565f74372468cdc699de89107"; + String result2 = Hex.toHexString(HashUtil.ripemd160("test".getBytes())); + assertEquals(expected2, result2); + } + + @Test + public void testRIPEMD160_Multiple() { + String expected1 = "9295fac879006ff44812e43b83b515a06c2950aa"; + String result1 = Hex.toHexString(HashUtil.ripemd160("test1".getBytes())); + assertEquals(expected1, result1); + + String expected2 = "80b85ebf641abccdd26e327c5782353137a0a0af"; + String result2 = Hex.toHexString(HashUtil.ripemd160("test2".getBytes())); + assertEquals(expected2, result2); + } +}