So What Exactly is Hashing?
Without hashing, cryptocurrencies would not exist. Hashing is the most important security tool that leads to the building of a blockchain.
If you have used cryptocurrencies and wallets for a while, you have certainly noticed that a thing called hashing is somehow a part of the process.
So why should you know about hashing? First of all, to protect your own funds. But also, a brief discussion of the work of generating and testing hashes is a good counter-argument to being told that "cryptocurrencies are money out of thin air." You can make the argument that cryptocurrencies are backed by mathematics.
Calculating a new block on the blockchain involves hashing. Producing a wallet private key also uses a form of hashing. But what exactly is this process?
Hashing is the process of turning information, usually a string, into an alphanumeric digest. This happens through a hash function- there are different approaches to creating the digest. It is not easy to create a new hash function, this is usually the work of cryptographers and mathematicians. Think of it as a black box that transforms the text- otherwise the design of a hash function is way beyond the scope of users or even expert programmers.
Creating a good hash function may take years, and so far there are only a handful of these used, both in blockchain projects and for general computer security.
Here is how a string looks when hashed under the SHA-256 algorithm, used in Bitcoin and others. As you can see, a small difference in input leads to a large difference in the digest or hash:
You can play around with a hashing generator here. It may take you until the heat death of the Universe to mine even one Bitcoin block by hand.
The biggest challenge for hash functions is to avoid the case of collision, where two information inputs have the same digests. This is an astronomically rare event, especially with a well-designed and time-tested hash function.
So what is the input that produces the number (or hash) for the next block in a blockchain?
The input takes a bit of information from every transaction and adds another number coming from the miner- the nonce. But just hashing the information is not enough- the number must satisfy a certain condition. So all of the information is hashed again and again, with a different nonce and tested against a target number. This is yet another definition of mining- the generation of potential hashes with a different nonce.
You can see that the hash begins with many zero characters- this is to comply with the target. The greater the difficulty, the more zero characters. The hash displayed has the right number of zeros and is smaller than the target number, so it is accepted as a block header. And the miner who discovered it got the block reward of 25 Bitcoin.
Hash functions create what is called "an avalanche effect," so a small change in the input information leads to a very different digest, or hash.
When calculating the next block for any of the existing blockchains, what happens is that up to a trillion hashes are generated every second and checked for fulfilling a certain condition. In a way, this works like a lottery, since every hash is wildly different from the previous one, and may become "the winning number".
Hashing is also used in protecting your wallets. There are various input methods, trying to use complex and hard-to-guess inputs. For instance, users are rarely asked to think of a pass phrase themselves, as those are usually not random and may be guessed. But some wallets use a 12-word phrase which they hash to create private and public keys that are unique. Some wallets generate random input from mouse movements and use it to generate the key.