A brief description of Bitcoin hash functions

A brief description of Bitcoin hash functions

Anyone interested in Bitcoin will have heard of the term "cryptographic hash function" at some point. But what does it mean and how does it relate to cryptocurrency?

Hash functions are not only an important part of the Bitcoin protocol, but also an important part of the entire information security.

We will use some simple examples below to show how hash functions work.

What is a Hash Function?

In theory, a hash function is a mathematical process that takes input data of any size and returns output data of a fixed size.

More specifically, it takes as input a sequence of letters of any length - we call it a string - and returns a sequence of letters of a fixed length. Whether the input string is a single letter, a word, a sentence, or an entire novel, the length of the output - called a digest - is always the same.

A common use case for this type of hash function is storing passwords.

When you create a user account with any web service that requires a password, the password is run through a hash function and a hashed summary of the password information is stored. When you enter your password to log into your account, the same hash function is run through the password you entered, and the server checks to see if the result matches the stored summary.

This means that even if a hacker were able to gain access to the database where the hashes are stored, they would not be able to compromise all user accounts at once because the password that produced a particular hash could not be easily found.

Python Simple Hash Function

You can experiment with hashing using Python, a programming language that comes installed by default on Mac and Linux operating systems. (This tutorial assumes you are using some version of OSX or Linux, as using Python on Windows is more complicated.)

First, open the terminal, type python and hit Enter.

You’ll then be dropped into the Python REPL, an environment where you can try out Python commands directly instead of writing programs in separate files.

Then enter the following values, pressing Enter after each line and typing TAB at the markers:

 import hashlib
def hash(mystring):
[TAB] hash_object = hashlib.md5(mystring.encode())
[TAB] print(hash_object.hexdigest())
[ENTER]

You have now created a function, hash(), which will calculate the hash value of a particular string using the MD5 hashing algorithm. To run the function, insert the string into the parentheses () above. For example:

hash(“CoinDesk rocks”)

Press Enter to view the hash digest of the string.

You'll see that calling this hash function on the same string will always produce the same hash, but adding or changing a single character will produce a completely different hash value:

 hash("CoinDesk rocks") => 7ae26e64679abd1e66cfe1e9b93a9e85
hash("CoinDesk rocks!") => 6b1f6fde5ae60b2fe1bfe50677434c88

Bitcoin hash function

In the Bitcoin protocol, hash functions are part of the block hashing algorithm that is used to write new transactions to the blockchain through the mining process.

In Bitcoin mining, the inputs to the function are all recent unconfirmed transactions (along with some timestamps associated with the previous block and references to some additional inputs).

In the code examples above, we have seen that changing a small part of the hash function can result in a completely different output. This property is crucial to the "proof of work" algorithm in the mining process: in order to successfully "solve" a block, miners need to combine all inputs with their own input data in such a way that the resulting hash will start with some zeros.

As a basic demonstration, we can try to “mine” using our Python hash function by manually adding an exclamation mark (!) after “CoinDesk rocks!” until we find a hash that starts with a single 0.

 >>> hash("CoinDesk rocks!!")
66925f1da83c54354da73d81e013974d
>>> hash("CoinDesk rocks!!!")
c8de96b4cf781a6373766c668ceac0f0
>>> hash("CoinDesk rocks!!!!")
9ea367cea6a2cc4a6f5a1d9a334d0d9e
>>> hash("CoinDesk rocks!!!!!")
b8d43387d98f035e2f0ac49740a5af38
>>> hash("CoinDesk rocks!!!!!!")
0fe46518541f4739613b9ce29ecea6b6 => SOLVED!

Of course, solving the hash for a bitcoin block — which, at the time of writing, must begin with 18 zeros — requires an extremely large amount of computation (all the computer processing power of the bitcoin network combined still takes close to 10 minutes to solve a block).

The need for a large amount of processing power means that the mining of new bitcoins takes a long process to complete, and it is impossible to mine all bitcoins at once.

To be able to earn Bitcoins from mining, you need to put in a lot of work to solve blocks - by earning this reward, you lock all new transactions into blocks, which are added to the permanent record of all previous transactions: the blockchain.

<<:  The Greek debt crisis reappears, which may trigger a new round of Bitcoin fans

>>:  Bitcoin Scaling: Reflections from the DCG Portfolio

Recommend

People who are lucky in gaining weight according to their physiognomy

In the Tang Dynasty, fat women were considered be...

Palmistry tells you which palmistry has strong financial management ability

In the Chinese idiom collection, there is an idiom...

Uganda Begins Regulating Bitcoin

Rage Comment : The digital currency conference he...

PengolinCoin Mining Tutorial

Currency Introduction PangolinCoin (English name:...

Do women with hooked noses have good luck in love?

Usually, luck in love does not mean being liked b...

Mine owners talk about all aspects of park electricity

In May this year, at the mining conference held i...

What moles does a man with a good wife have?

What moles does a man with a good wife have? (1) ...

What could be the next narrative to ignite the crypto bull run?

When we have begun to adapt to the rhythm of the ...

A woman with a face that is favored and rich

A woman with a face that is favored and rich Will...

Teach you how to read the distance between eyebrows

As we all know, facial features are closely relat...

What is the personality of a person with straight eyebrows?

Straight eyebrows are the most popular eyebrow sh...

DIY Ethereum Miner: List of Mining Configurations Available on the Market

Strictly speaking, electronic currency is nothing...