Commit 0787ce2e authored by Schudel, MJ (Michel)'s avatar Schudel, MJ (Michel)

Removed hashutils and replaced it with commons codec.

parent 60f9dbc7
......@@ -94,7 +94,5 @@ dependencies {
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: swaggerVersion
compile group: 'io.springfox', name: 'springfox-swagger2', version: swaggerVersion
compile group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.59'
compile 'commons-codec:commons-codec:1.11'
}
......@@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import nl.craftsmen.blockchain.craftscoinnode.transaction.Transaction;
import org.apache.commons.codec.digest.DigestUtils;
import java.time.Instant;
import java.util.*;
......@@ -23,7 +24,7 @@ public class Blockchain {
*
* @return a new blockchain containing a genesis block.
*/
public static Blockchain create() {
static Blockchain create() {
Blockchain blockchain = new Blockchain();
Block block = new Block(0, Instant
.now()
......@@ -44,8 +45,8 @@ public class Blockchain {
* @param transactionsToBeIncluded the transactions the be included in the block.
* @return the new block.
*/
public Block mineNewBlock(Set<Transaction> transactionsToBeIncluded) {
String previousHash = HashUtil.createHashOf(this.getLastBlock());
Block mineNewBlock(Set<Transaction> transactionsToBeIncluded) {
String previousHash = createHashOf(this.getLastBlock());
Block block = new Block(getIndexOfLastBlock() + 1, Instant
.now()
.toEpochMilli(), transactionsToBeIncluded, this.proofOfWork(), previousHash);
......@@ -58,7 +59,7 @@ public class Blockchain {
*
* @param block the block to be added.
*/
public void addBlock(Block block) {
void addBlock(Block block) {
chain.add(block);
}
......@@ -68,7 +69,7 @@ public class Blockchain {
*
* @return index of the block height of the next block.
*/
public long getIndexOfNextBlock() {
long getIndexOfNextBlock() {
return getIndexOfLastBlock() + 1;
}
......@@ -83,7 +84,7 @@ public class Blockchain {
* @return the most recent block in the blockchain.
*/
private Block getLastBlock() {
return chain.size() > 0 ? chain.get(chain.size() - 1): null;
return chain.size() > 0 ? chain.get(chain.size() - 1) : null;
}
/**
......@@ -100,7 +101,7 @@ public class Blockchain {
*
* @return all transactions in the blockchain.
*/
public List<Transaction> getAllTransactions() {
List<Transaction> getAllTransactions() {
return this
.chain
.stream()
......@@ -134,9 +135,9 @@ public class Blockchain {
*
* @return if this blockchain is valid.
*/
public boolean isValid() {
boolean isValid() {
for (int i = chain.size() - 1; i >= 1; i--) {
if (!chain.get(i).getPreviousHash().equals(HashUtil.createHashOf(chain.get(i - 1)))) {
if (!chain.get(i).getPreviousHash().equals(createHashOf(chain.get(i - 1)))) {
return false;
}
if (!validProof(chain.get(i - 1).getProof(), chain.get(i).getProof())) {
......@@ -153,9 +154,9 @@ public class Blockchain {
* @param newBlock the new block.
* @return true if the new block is valid, false otherwise.
*/
public boolean isNewBlockValid(Block newBlock) {
boolean isNewBlockValid(Block newBlock) {
Block lastBlock = getLastBlock();
return lastBlock == null || newBlock.getPreviousHash().equals(HashUtil.createHashOf(lastBlock)) && validProof(lastBlock.getProof(), newBlock.getProof());
return lastBlock == null || newBlock.getPreviousHash().equals(createHashOf(lastBlock)) && validProof(lastBlock.getProof(), newBlock.getProof());
}
/**
......@@ -175,11 +176,11 @@ public class Blockchain {
}
private boolean validProof(long previousProof, long currentProof) {
String hash = HashUtil.createHashOf(previousProof + "" + currentProof);
String hash = DigestUtils.sha256Hex(previousProof + "" + currentProof);
return hash.startsWith("0000");
}
public boolean isInferiorTo(Blockchain otherChain) {
boolean isInferiorTo(Blockchain otherChain) {
return ((otherChainIsHigher(otherChain) || otherChainIsOlder(otherChain)) && otherChain.isValid());
}
......@@ -191,4 +192,10 @@ public class Blockchain {
return otherChain.getBlockHeight() > this.getBlockHeight();
}
private String createHashOf(Block block) {
return DigestUtils.sha256Hex(block.toString());
}
}
package nl.craftsmen.blockchain.craftscoinnode.blockchain;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.binary.Hex;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
class HashUtil {
/**
* Produces a SHA-256 hex-encoded createHashOf string of a block.
* @param block the block.
* @return a has string in Hex format, like 'da348aff2d'
*/
static String createHashOf(Block block) {
try {
ObjectMapper mapper = new ObjectMapper();
String blockString = mapper.writeValueAsString(block);
return createHashOf(blockString);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
/**
* Produces a SHA-256 hex-encoded createHashOf string of a string.
* @param aString the string.
* @return a has string in Hex format, like 'da348aff2d'
*/
static String createHashOf(String aString) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(aString.getBytes());
return new String(Hex.encodeHex(hash));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}
package nl.craftsmen.blockchain.craftscoinnode.blockchain;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class HashUtilTest {
@Test
public void stringShouldProduceAHash() {
String hash = HashUtil.createHashOf("I can see my house from here");
assertThat(hash).isEqualTo("c6599690d709c8240563146cf0e04a54cbe3a37b621e5ba8ceb6d5e4d345deae");
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment