My Journey To Java 005 .. simple BlockChain project

Islam Taha
3 min readFeb 12, 2019

--

My Journey To Java: BlockChain

Welcome everyone, In the previous diary of my journey to java we explained some of the core concepts of java. Today we are going to build a simple blockchain program.

What is the blockchain?

The blockchain is a chain of blocks each block of them store the previouse hash, the data and the block hash . Here is a simple image to demonstrate the idea behind the blockchain.

Example:

Block #0:

previous hash: 0000000000000000000000000000000000000000000000000000000000000000
Data:
My Journey to Java By 0xIslamTaha : part 0
2019.02.12.21.34.07
Hash:9c9e0e8f79532feab5deee7995be0e55ba9233cab3ee0da9c2efb68655f28559

Block #1:

previous hash: 9c9e0e8f79532feab5deee7995be0e55ba9233cab3ee0da9c2efb68655f28559
Data:
My Journey to Java By 0xIslamTaha : part 1
2019.02.12.21.34.07
Hash:bc17efe654d9f5befc883609fd1ab93e4e29b5d93d6d7dc90663e782a4ad54bc

As you can see that, the second block’s previous hash = the first block hash and the first block hash is depending on the data in the first block. So why do we call it Blockchain? Cause if under any circumstances, the data inside any block has been changed, the chain will be broken i.e, when the block data has been changed, the block hash will be changed too and this hash won’t be any more equal to the previouse hash in the next block.

So let's stop taking and let us start in developing the block class.

Block class:

The block class will take data and the chain then it will produce a new block. It should do three tasks to produce a new block:

  • Add timestamp to the data
  • Get the previous block hash from the chain
  • Calculate the sha-256 hash for the new block
class Block{
String timeStamp, preHash, data, curHash;

Block(String data, ArrayList<Block> chain) throws NoSuchAlgorithmException {
this.timeStamp = getTimeStamp();
if (chain.size() == 0){
this.preHash = "0000000000000000000000000000000000000000000000000000000000000000";
}else {
this.preHash = chain.get(chain.size() - 1).curHash;
}
this.data = data;
this.curHash = calcCurrentHash(this.timeStamp+this.preHash+this.data);
}

private String getTimeStamp(){
return new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
}

private String calcCurrentHash(String data) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hashInBytes = md.digest(data.getBytes(StandardCharsets.UTF_8));

// bytes to hex
StringBuilder sb = new StringBuilder();
for (byte b : hashInBytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}

}

In this example, We will use a construct method. hmm what is the construct method? its a method with the same name of the class and it doesn't have a type and it doesn't return anything. So why do we use it? If the class has a constructor method, then once u define an object, it will call this constructor method by default, so normally we use the construct method to do some logic once we create a new object of the class.

The getTimeStamp method will return the current time value and the calcCurrentHash will take a data and calculate its sha-256 hash then returns this hash. While the Block method will take the data and the chain then get the timeStamp and check if the chain with empty, make the previous hash all zeros but if the chain has an element, make the previous hash equal to the last element of the chain hash and at the end calculate the block hash of all those data (original data + timestamp + previous hash).

What we only need to do in the main method is creating a block object with the required data to be stored in this block and add it to the chain. Check the following code

public class BlockChain{
public static ArrayList <Block> chain = new ArrayList<>();

public static void main(String[] args) throws NoSuchAlgorithmException {
for (int i=0; i<5; i++) {

String data = String.format("My Journey to Java By 0xIslamTaha : part %d", i);

Block block_obj = new Block(data, chain);

if (chain.size() > 1) {
if (block_obj.preHash == chain.get(chain.size() - 1).curHash) {
chain.add(block_obj);
}
} else{
chain.add(block_obj);
}
System.out.println(chain.get(i).preHash);
System.out.println(chain.get(i).data);
System.out.println(chain.get(i).timeStamp);
System.out.println(chain.get(i).curHash);
System.out.println("\n");
}
}

}

That is it!

with this simple example we created the most simple blockchain :D and I can see that we almost used everything we have been talked about. You can check the full code here. Thanks!

--

--