We are going for building Blockchain in PHP programming Language, Blockchain in itself is a very simple concept, it’s a very simple data structure, cryptocurrencies are complicated but the Blockchain is not, the reason they are complicated is the consensus algorithm, their mining mechanism and the everything that is around but Blockchain in itself is very easy to understand, what you need to know before you can actually understand what Blockchain is and how it works, you need to know what hashes are? and how they work?

So a hash is basically a digital signature of some kind of data, for example I can take a movie and I can hash it and get a digital signature, I can take an email and I can hash it and get a digital signature, I can take a word and I can hash it, you can take whatever data and hash it and you get the digital signature for that data.

How this hash actually works? Lets dive into it.

We will Build Blockchain in PHP, it will be very simple and if you know programming you can do that in another language and if you don’t know programming, I think you still will be able to understand roughly how this works so let’s talk about hashes.

<?php $list1 = ["a","b","c"]; $list2 = ["a","b","c"]; echo "list 1: ".md5(serialize($list1)); echo "<br/>list 2: ".md5(serialize($list2)); ?> 1 2 3 4 5 6 & lt ; ? php $ list1 = [ "a" , "b" , "c" ] ; $ list2 = [ "a" , "b" , "c" ] ; echo "list 1: " . md5 ( serialize ( $ list1 ) ) ; echo "<br/>list 2: " . md5 ( serialize ( $ list2 ) ) ; ? >

As you can see we have two lists we have one list that contains A,B,C and we have a second list that also contains A, B and C so this is the data, this is basically the strings we’re trying to hash so let’s see what kind of digital signature we will get from list 1 & 2.

Output:

list 1: ec10e0c7a344da191700ab4ace1a5e26 list 2: ec10e0c7a344da191700ab4ace1a5e26 1 2 list 1 : ec10e0c7a344da191700ab4ace1a5e26 list 2 : ec10e0c7a344da191700ab4ace1a5e26

You can see as we get these two hash strings or digital signatures, these are identical for these two lists.

If I now change anything in list 1 for example –

<?php $list1 = ["aaa","b","c"]; //Changed $list2 = ["a","b","c"]; $list2 = ["a","b","c"]; echo "list 1: ".md5(serialize($list1)); echo "<br/>list 2: ".md5(serialize($list2)); ?> 1 2 3 4 5 6 7 & lt ; ? php $ list1 = [ "aaa" , "b" , "c" ] ; //Changed $ list2 = [ "a" , "b" , "c" ] ; $ list2 = [ "a" , "b" , "c" ] ; echo "list 1: " . md5 ( serialize ( $ list1 ) ) ; echo "<br/>list 2: " . md5 ( serialize ( $ list2 ) ) ; ? >

Now I run it again

Output:

list 1: 97f4361000fdba1732a50f1771c9d830 list 2: ec10e0c7a344da191700ab4ace1a5e26 1 2 list 1 : 97f4361000fdba1732a50f1771c9d830 list 2 : ec10e0c7a344da191700ab4ace1a5e26

You see that I get a completely different digital signature so if I just make a small amendment I just meddle with the data, I try to change anything in this list, I will get a completely different digital signature and that is the foundation of a Blockchain Technology because a Blockchain is a list of blocks basically a chain of blocks and each block holds the digital signature of the previous block and the digital signature of the next block is based upon the digital signature of the current block so they are always together the next signature will be based on the current signature and the current signature is based on the previous signature so if you change anything back in the past you will break all of the signatures they will look completely different.

Learn More: What is Blockchain?

Let’s actually get into our Blockchain coding so the first thing we need to do is talk about our Block class, our Block in this case will be very simple, it will only contain three things it will contain the list of transactions that happened in that block, it will contain the previous hash or the digital signature of the previous block and it will also contain the hash of the current of itself and this hash will be based on the transactions and the previous hash so if anyone would change anything in the previous block the digital signature of the current block will change and that changes the digital signature of the next blocks.

Create an empty PHP project using your favorite IDE, I am using XAMPP for my localhost environment.

Now create a new file block.php, and put the following code snippet in it.

<?php class Block{ private $previousHash; private $transactions=[]; private $blockHash; function __construct($previousHash,$transactions){ $this->previousHash = $previousHash; $this->transactions = $transactions; $contents = [md5(serialize($transactions)),$previousHash]; $this->blockHash = md5(serialize($contents)); } function getPreviousHash(){ return $this->previousHash; } function getTransactions(){ return $this->transactions; } function getBlockHash(){ return $this->blockHash; } } ?> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 & lt ; ? php class Block { private $ previousHash ; private $ transactions = [ ] ; private $ blockHash ; function __construct ( $ previousHash , $ transactions ) { $ this -> previousHash = $ previousHash ; $ this -> transactions = $ transactions ; $ contents = [ md5 ( serialize ( $ transactions ) ) , $ previousHash ] ; $ this -> blockHash = md5 ( serialize ( $ contents ) ) ; } function getPreviousHash ( ) { return $ this -> previousHash ; } function getTransactions ( ) { return $ this -> transactions ; } function getBlockHash ( ) { return $ this -> blockHash ; } } ? >

Now create another file called index.php and use block.php class to create your blocks.

Index.php

<?php include("block.php"); $genesisTransaction = ["a sends 11 bitcoins to b","b sends 44 bitcoins to c"]; $genenisBlock = new Block(0, $genesisTransaction); $block1Transaction = ["a sends 11 bitcoins to b","b sends 44 bitcoins to c"]; $block1 = new Block($genenisBlock->getBlockHash(), $block1Transaction); $block2Transaction = ["a sends 11 bitcoins to b","b sends 44 bitcoins to c"]; $block2 = new Block($block1->getBlockHash(), $block2Transaction); echo "Genesis Block: ".$genenisBlock->getBlockHash(); echo "<br/>Block 1: ".$block1->getBlockHash(); echo "<br/>Block 2: ".$block2->getBlockHash(); ?> 1 2 3 4 5 6 7 8 9 10 11 12 13 & lt ; ? php include ( "block.php" ) ; $ genesisTransaction = [ "a sends 11 bitcoins to b" , "b sends 44 bitcoins to c" ] ; $ genenisBlock = new Block ( 0 , $ genesisTransaction ) ; $ block1Transaction = [ "a sends 11 bitcoins to b" , "b sends 44 bitcoins to c" ] ; $ block1 = new Block ( $ genenisBlock -> getBlockHash ( ) , $ block1Transaction ) ; $ block2Transaction = [ "a sends 11 bitcoins to b" , "b sends 44 bitcoins to c" ] ; $ block2 = new Block ( $ block1 -> getBlockHash ( ) , $ block2Transaction ) ; echo "Genesis Block: " . $ genenisBlock -> getBlockHash ( ) ; echo "<br/>Block 1: " . $ block1 -> getBlockHash ( ) ; echo "<br/>Block 2: " . $ block2 -> getBlockHash ( ) ; ? >

Output:

Genesis Block: d9c559b57e148b19802d8e70555f0303 Block 1: 29e9e5a309aae81243fdf73112253c74 Block 2: fbff8f664f6b4069bf7288cb1b86cb3a 1 2 3 4 Genesis Block : d9c559b57e148b19802d8e70555f0303 Block 1 : 29e9e5a309aae81243fdf73112253c74 Block 2 : fbff8f664f6b4069bf7288cb1b86cb3a

See that the current hash will be based on the previous hash, if you change any of the previous transaction your all digital signatures will effect, for example I changed 11 to 12 in the genesisTransaction it will output completely different like:

<?php include("block.php"); $genesisTransaction = ["a sends 12 bitcoins to b","b sends 44 bitcoins to c"]; //changed 11 to 12 $genenisBlock = new Block(0, $genesisTransaction); $block1Transaction = ["a sends 11 bitcoins to b","b sends 44 bitcoins to c"]; $block1 = new Block($genenisBlock->getBlockHash(), $block1Transaction); $block2Transaction = ["a sends 11 bitcoins to b","b sends 44 bitcoins to c"]; $block2 = new Block($block1->getBlockHash(), $block2Transaction); echo "Genesis Block: ".$genenisBlock->getBlockHash(); echo "<br/>Block 1: ".$block1->getBlockHash(); echo "<br/>Block 2: ".$block2->getBlockHash(); ?> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 & lt ; ? php include ( "block.php" ) ; $ genesisTransaction = [ "a sends 12 bitcoins to b" , "b sends 44 bitcoins to c" ] ; //changed 11 to 12 $ genenisBlock = new Block ( 0 , $ genesisTransaction ) ; $ block1Transaction = [ "a sends 11 bitcoins to b" , "b sends 44 bitcoins to c" ] ; $ block1 = new Block ( $ genenisBlock -> getBlockHash ( ) , $ block1Transaction ) ; $ block2Transaction = [ "a sends 11 bitcoins to b" , "b sends 44 bitcoins to c" ] ; $ block2 = new Block ( $ block1 -> getBlockHash ( ) , $ block2Transaction ) ; echo "Genesis Block: " . $ genenisBlock -> getBlockHash ( ) ; echo "<br/>Block 1: " . $ block1 -> getBlockHash ( ) ; echo "<br/>Block 2: " . $ block2 -> getBlockHash ( ) ; ? >

Output:

Genesis Block: d9c559b57e148b19802d8e70555f0303 Block 1: 29e9e5a309aae81243fdf73112253c74 Block 2: fbff8f664f6b4069bf7288cb1b86cb3a 1 2 3 4 Genesis Block : d9c559b57e148b19802d8e70555f0303 Block 1 : 29e9e5a309aae81243fdf73112253c74 Block 2 : fbff8f664f6b4069bf7288cb1b86cb3a

You can see a change in any of these transactions, in any of the data in the one block will propagate and change all of the digital signatures in the future chains and that is how Blockchain works, because if I change something everyone else will see that my digital signatures are completely different from everyone else’s so that means that I have cheated, so if I just tell everyone that hey Person A gave me this many bitcoins back then and here is the Blockchain that confirms that everyone will take a look at it and say well I will know this blockchain doesn’t confirm that because we have our own versions of the Blockchain and the digital signatures are completely different.

I hope you understood the basic concepts of hashing of linking blocks together via their digital signatures. Implement this example in PHP and let me know your thoughts in the comments section below.

