mirror of
https://github.com/status-im/ethereumj-personal.git
synced 2025-01-11 12:24:13 +00:00
Add RIPEMD160 with separete HashUtil unit tests
This commit is contained in:
parent
e6a229bf21
commit
bb52b1c492
@ -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.
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user