Anyone interested in Bitcoin will have heard of the term “cryptographic hash function” at some point. But what does this mean and how does it relate to cryptocurrency?
The hash function is not only an important part of the Bitcoin protocol but also an important part of all information security.
In the following article, we will do a simple demo to see some simple examples of how they work.
What is a hash function?
Abstractly speaking, a hash function is a mathematical process that accepts input data of any size, performs operations on it, and returns output data of a fixed size.
In a more specific example, this can be used to take as input a sequence of letters of any length, which we call a string, and return a sequence of letters of a fixed length. Whether the input string is a single letter, word, sentence, or an entire novel, the output (called a summary) will always have the same length.
A common use of this type of hash function is to store passwords.
When you create a user account with any web service that requires a password, the password will run through a hash function and store the hash digest of the message. When you enter your password to log in, the same hash function will be executed on the word you entered, and the server will check if the result matches the stored summary.
This means that if hackers have access to the database containing the stored hash, they will not be able to destroy all user accounts immediately because there is no easy way to find the password that produced a given hash.
Simple Hash Functions in Python
You can experiment with hashes using Python, a programming language that is installed by default on Mac and Linux operating systems. (This tutorial assumes you are using a version of OS X or Linux because using Python on Windows is more complicated.)
First, open a terminal, type python, and hit ENTER.
This will put you in the Python REPL, where you can directly test Python commands instead of writing programs in separate files.
Then type the following, press ENTER after each line and TAB at the marked position:
def hash (mystring):
[TAB] hash_object = hashlib.md5 (mystring.encode ())
[TAB ] print (hash_object.hexdigest ())
You have now created a hash () function that will use the MD5 hash algorithm to calculate and print the hash value of a given string. To run it, put a string in brackets in quotes, for example:
hash (“CoinDesk rock”)
and press ENTER to see the hash summary of the string.
You will see that calling a hash function on the same string will always generate the same hash value, but adding or changing a character will generate a completely different hash value:
hash (“CoinDesk Rocks”) => 7ae26e64679abd1e66cfe1e9b93a9e85
hash (“CoinDesk rock! “) => 6b1f6fde5ae60b2fe1bfe50677434c88
The hash function in Bitcoin
In the Bitcoin protocol, the hash function is part of the block hash algorithm and is used to write new transactions to the area through the Blockchain mining process.
In Bitcoin mining, the input to this function is all the latest unconfirmed transactions (and some additional inputs related to the timestamp and a reference to the previous block).
In the above code example, we have seen that changing a small part of the input of the hash function results in a completely different output. This attribute is essential for the “proof of work” algorithm involved in mining: in order to successfully “solve” a block, miners try to combine all inputs with their own arbitrary input data, so that the generated hash is The number of zeros.
As a basic demonstration, we can try to “dig” with our Python hash function by manually adding an exclamation point after “CoinDesk is great!”. Until we find a hash that starts with a single zero.
Hash (“CoinDesk is great!”)
Hash (“CoinDesk is great!”)
c8de96b4cf781a636684cf Hash (“Desk is great!” CoinDesk is great >>Desk4f >>Desk4f >>Desk is great) !” >> Hash!!!”)
Hash (“CoinDesk is wobbly!!!!!!”)
46 at the time of writing this article should start with 18 zeros, it requires a very large amount of calculations (so the comprehensive processing power of all computers on the network still takes about 10 minutes to solve a block)
is such a powerful processing power needed This means that new bitcoins are mined over a long period of time, not all at once.
To earn bitcoins through mining, you have to do a lot of work to solve a block. By obtaining the reward, you lock all new transactions in a block, which is added to the permanent of all previous transactions Record-in the blockchain.