Hello there!
Hashing and Data Mining are heart of blockchain
Let's understand how you can perform hashing  and also how you can mine appropriate hash value based on set difficulty level. We are going to use Python for this demo.
Let's begin
You must import all these libraries
You are going to need these in this project
Next, you are going to write a small function which will return hash value of any message or any data you pass to this function
In my case the name of the function I am giving is hashing
And it will return hash value of message I pass to this function
You are going to use hashlib module of python
There are other construct available of same module, but for now let us use this module
return hashlib
within hashlib you are going to use sha256
it's better to encode the message in ascii format
and then get the hexadecimal conversion
very good!
make sure all the brakets are there properly
Awesome! looks good!
Very good! Your function is ready, let's test it.
Let's test by passing a message to it
I am going to pass a message 'hello brother'
and let's see hash256 value of 'hello brother'
here you go!
it's all hexadecimal numbers
This was easy
You pass the message to a hashing algorithm and get the hash value
You must be wondering, if this is so easy then what is all the fuss about data mining, complexity involved and lot of cpu power involved
let me try to explain you the complexity involved here
when it comes to deriving hash value of transactions of block in a block chain
you cannot give any hash value as we have seen in this example
In case of blockchain the hash value has to follow certain convention
You cannot change the message or transaction
So far the hash value to follow certain convention, you need to combine the transaction/message with something called nonce
And then derive the hash value of this combination
and you keep changing the nonce until you get appropriate hash value
which fits the rule prescribed by the blockchain alogrithm
the hash value has to follow that rule
okay, let's comes back to the python program we were writing.
let's take a small message 'hello brother how are you'
we are going to consider this message as a transaction in blockchain
The real blockchain transaction will be something like transaction of money etc.
but for demo purpose, let's consider this is the transaction which says hello brother how are you
We are writing small python logic which will combine this message with nonce
which will be a number anywhere between 1 to 1000
and it will return has value
the condition on the hash value will be that the first two characters of the hash value should be 11
Let's write the for loop where nonce value will start and it will loop anywhere between 1 and 1000
and this value will be combined with the message
and hash of this combined value, the nonce + message will be derived
Loop will break when it finds a hash value starting with 11
What we checking here is , how many times the loop runs before it gets a combined hash value which starts with 11
Very good! The logic is pretty much done! Let's run it now.
to get hash value starting with 11 you need to combine 'hello brother how are you' message with 198
That means for loop has run 198 times before arriving at a hash value of required complexity
Let's increase the complexity. Let's say we need hash value starting with 111
Here you go, that hash value cannot be derived within 1000 run for nonce. Let's increase it to 10000. Now you get it for 7145 nonce value.
That means the for loop ran many times more
Likewise if increase the complexity of hash value the nonce calculation will have to run more times to get a proper nonce value
This is very simple example where our requirement is limited to first few characters of hash value
what will happen if you put a restriction that the hash value should not exceed beyond a number
Bitcoin data minors are mining for a hash value of preset complexity. Hence the logic goes to several computation before arriving at appropriate nonce value.
And they have to find this hash value faster than others to get the reward
This is why they need a super fast computer
I hope, through this small example, now you understand the complexity involved in data mining and deriving hash value of a block in blockchain
This is a very small example, but this can help you understand what lies behind data mining
