0% found this document useful (0 votes)
277 views5 pages

First Blockchain With Java

This document provides instructions for creating a basic blockchain using Java. It explains that a blockchain is a chain of blocks, with each block containing the hash of the previous block. It describes creating a Block class to represent each block with fields for the hash, previous hash, and data. It also shows how to generate hashes using the SHA-256 algorithm and include them in the blocks. Finally, it demonstrates storing the blocks in an ArrayList, outputting the blockchain as JSON, and validating the integrity of the chain by checking hashes.

Uploaded by

monica_lufo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
277 views5 pages

First Blockchain With Java

This document provides instructions for creating a basic blockchain using Java. It explains that a blockchain is a chain of blocks, with each block containing the hash of the previous block. It describes creating a Block class to represent each block with fields for the hash, previous hash, and data. It also shows how to generate hashes using the SHA-256 algorithm and include them in the blocks. Finally, it demonstrates storing the blocks in an ArrayList, outputting the blockchain as JSON, and validating the integrity of the chain by checking hashes.

Uploaded by

monica_lufo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Creating Your First Blockchain

with Java.
We will be using Java but you should be able to follow along in
any OOP language. I’ll be using Eclipse but you can use any new
fancy text editor ( though you’ll miss out on a lot of good bloat ).

You will need:

• Java and JDK installed. ( duh ).


• Eclipse ( or another IDE/Text Editor ).

Don’t worry if your eclipse looks different to mine. I’ll be using a dark theme in eclipse
because ^

Optionally, you can grab GSON library by google (who are they
???). This will allow us to turn an object into Json \o/. It’s a
super useful library that we will also be using further down the
line for peer2peer stuff, but feel free to use an alternate method.

In Eclipse create a (file > new > ) Java project. I’ll call my Project
“noobchain” and create a new Class by the same name
(NoobChain).
Don’t be copying my project name now ( ͠° ͟ ͜ʖ ͡°)

Now you’re good to go :)

Making the Blockchain.


A blockchain is just a chain/list of blocks. Each block in the
blockchain will have its own digital fingerprint, contain digital
fingerprint of the previous block, and have some data ( this data
could be transactions for example ).

I sure hope Nakamoto never sees this.

Hash = Digital Fingerprint.


Each block doesn’t just contain the hash of the block
before it, but its own hash is in part, calculated from
the previous hash. If the previous block’s data is changed
then the previous block’s hash will change ( since it is calculated
in part, by the data) in turn affecting all the hashes of the blocks
there after. Calculating and comparing the hashes allow
us to see if a blockchain is invalid.

What does this mean ? …Changing any data in this list, will
change the signature and break the chain.

So Firsts lets create class Block that make up the


blockchain:

As you can see our basic Block contains a String hash that will
hold our digital signature. The variable previousHash to hold the
previous block’s hash andString data to hold our block data.

Next we will need a way to generate a digital


fingerprint,
there are many cryptographic algorithms you can choose from,
however SHA256 fits just fine for this example. We can import
java.security.MessageDigest; to get access to the SHA256
algorithm.

We need to use SHA256 later down the line so lets create a


handy helper method in a new StringUtil ‘utility’ class :
This is mostly a carbon copy of the http://www.baeldung.com/sha-256-hashing-java

Don’t worry too much if you don’t understand the


contents of this helper method, all you need to know is that
it takes a string and applies SHA256 algorithm to it, and
returns the generated signature as a string.
Now lets use our applySha256 helper, in a new method in
the Block class, to calculate the hash. We must calculate the
hash from all parts of the block we don’t want to be tampered
with. So for our block we will include the previousHash,
the data and timeStamp.

and lets add this method to the Block constructor…

Time for some testing…


In our main NoobChain class lets create some blocks and print
the hashes to the screen to see that everything is in working
order.

Lets test this…

The first block is called the genesis block, and because there is
no previous block we will just enter “0” as the previous hash.

The output should look similar to this:

Your values will be different because your timestamp will be different.

Each block now has its own digital signature based on its
information and the signature of the previous block.

Currently it’s not much of a blockchain, so lets store our blocks


in an ArrayList and also import gson to view it as Json. (click
here to find out how to import the gson library)

Now our output should look something closer to what we expect


a blockchain to look like.
Now we need a way to check the integrity of our
blockchain.
Lets create an isChainValid() Boolean method in
the NoobChain class, that will loop through all blocks in the
chain and compare the hashes. This method will need to check
the hash variable is actually equal to the calculated hash, and the
previous block’s hash is equal to the previousHash variable.

Any change to the blockchain’s blocks will cause this method to


return false.

On the bitcoin network nodes share their blockchains and


the longest valid chain is accepted by the network. What’s
to stop someone tampering with data in an old block then
creating a whole new longer blockchain and presenting that to
the network ? Proof of work. The hashcash proof of work
system means it takes considerable time and computational
power to create new blocks. Hence the attacker would need
more computational power than the rest of the peers combined.

You might also like