Blockchain Technology – Using PHP develop Blockchain technology
Welcome back to Learn Tech Tips channel, as we discuss on the first topic, This topic will using PHP language develop Blockchain technology. OK let do now
To describe a Block (transaction element) in the Blockchain (array of transactions),
we need a class like this:
Data in a Block is very simple. check below code
The most important function in each Block is the function that calculates the Hash value (encryption) of the entire Block.
Since the Hash calculation function requires a strong encryption algorithm, which is 256-bit SHA encryption, I use hash(‘sha256’, $hash_string);
// Email: huuvi168@gmail.com
We need to create a Blockchain class to hold the array of Block elements.
The add_new_block($time, $data) function is a function that will ensure 2 things:
– Calc entire hash with current block.
– Get the Hash of the last Block to store it in the HashTruocDo variable of the current Block.
=> Thus, the Blocks will be connected (chain-chained) together through the binding of Hash code.
The validate() function is a very important function that plays the heart role in the Blockchain.
– Every time any user wants to check the entire current Blockchain whether it is safe or not to continue the transaction.
– All Hashes in the string are compared.
– The integrity of each Block and the interconnection of the entire Block must be guaranteed without the wrong Hash code.
– If there is a false positive, the data has been edited by someone in the network.
So the self-generated Blockchain technology is done, now we can start testing it:
=> The result will be show on $blockchain params with two transaction and after validate will return Correct data (because of no one change it before)
Array
(
[chain] => Array
(
[0] => AppControllerAdminBlock Object
(
[time] => 1640505857
[data] => Genesis Block
[previous_hash] =>
[hash] => 8de7d3c326ac02b8cd447c87726d18eaa715c9b2647b1c2573b4eb5723686429
)
[1] => AppControllerAdminBlock Object
(
[time] => 1640505857
[data] => Array
(
[Send] => Mr.A
[received] => Mrs.B
[Money] => 300
)
[previous_hash] => 8de7d3c326ac02b8cd447c87726d18eaa715c9b2647b1c2573b4eb5723686429
[hash] => 05fdb82a073d3e70e19c5d4148aa9165956f1bf8468ae899f4a2662c04e0526b
)
[2] => AppControllerAdminBlock Object
(
[time] => 1640505857
[data] => Array
(
[Send] => Mrs.B
[received] => Mr.C
[Money] => 200
)
[previous_hash] => 05fdb82a073d3e70e19c5d4148aa9165956f1bf8468ae899f4a2662c04e0526b
[hash] => 8267a1a4404d373c5db38060470412929430039f4f4b7000a9ffc99219cfeaba
)
)
[time] =>
[data] =>
[previous_hash] =>
[hash] =>
)
Validate of block chain
If anyone / hacker change the anydata on some element
=> The result will be
Array
(
[chain] => Array
(
[0] => AppControllerAdminBlock Object
(
[time] => 1640506057
[data] => Genesis Block
[previous_hash] =>
[hash] => 79f9d544d7a4cf1928a53c0eca23d06fd7f3effeb2b51911fda5e4a87b525ca2
)
[1] => AppControllerAdminBlock Object
(
[time] => 1640506057
[data] => Array
(
[Send] => Mr.A
[received] => Mrs.B
[Money] => 30000
)
[previous_hash] => 79f9d544d7a4cf1928a53c0eca23d06fd7f3effeb2b51911fda5e4a87b525ca2
[hash] => a5db4e7fdfdf79961a39a3d6254bdf5902621b10a384d15d71df08ea0718cd9f
)
[2] => AppControllerAdminBlock Object
(
[time] => 1640506057
[data] => Array
(
[Send] => Mrs.B
[received] => Mr.C
[Money] => 200
)
[previous_hash] => a5db4e7fdfdf79961a39a3d6254bdf5902621b10a384d15d71df08ea0718cd9f
[hash] => 582c97c25491c100fc31813200f4fdadd8ec296ab8f0435c75b2fc88228f82c5
)
)
[time] =>
[data] =>
[previous_hash] =>
[hash] =>
)
Validate of block chain
=> you will see the Incorrect Chain. That a core value of Blockchain Technology
Any feedback leave your message, we can discuss about it!!
Learn Tech Tips – Zidane
Full source code you can checkout here – BlockchainsControllers_v1.php
Related Posts:
Blockchain Part 1: What is Blockchain: https://learn-tech-tips.blogspot.com/2021/12/what-is-blockchain.html
Blockchain Part 2: Using PHP develop Blockchain Technology: https://learn-tech-tips.blogspot.com/2021/12/using-php-develop-blockchain-technology.html
Blockchain Part 3: Loophples of the current Blockchain:
https://learn-tech-tips.blogspot.com/2021/12/loophples-of-the-current-blockchain.html
Blockchain Part 4: Create virtual currency mining technology:
Blockchain Part 5: How to create own bitcoin virtual currency
Blockchain Part 6: Apply blockchain application in the life
https://learn-tech-tips.blogspot.com/2022/01/block-chain-application-in-the-life.html