As cryptocurrencies become more popular every day, words and phrases like “hashing” and “hash function” get used more and more often. But what are hash functions? And how are they used in cryptocurrencies?
This article will provide brief answers to these questions.
What is a hash function?
Imagine that Bill wants to send an electronic message to Cindy, and when Cindy receives the message, she needs to know that the message arrived intact and was not altered. One way to do this would be to send multiple copies of the message and let Cindy compare all of the copies to ensure that it hadn’t been changed. However, this would consume a lot of memory and wouldn’t be ideal.
If Bill could just find a way to first shorten the message without losing any of the information within it, and then send the shortened copy along with the original, this would save memory and still prove that the message did or did not make it intact. When Cindy received the message, she could compare the shortened copy to the longer copy. If the longer copy had different information than the shortened copy, it would mean that the message must have been tampered with or part of it must have been lost.
It turns out that something like this can be done by running the message through what is called a “hash function.” A hash function is a series of steps that can be performed on a message of any arbitrary length that will always reduce it to a fixed length.
So let’s return to our example of Bill and Cindy. If Bill wants to send a message to Cindy, one way to make sure that it arrived intact would be to make a second copy of the message with each letter mapped to a particular number. For example, write a “1” for every time there is an “a” in the letter, a “2” each time there is a “b,” a “3” each time there is a “c,” etc. Once this entire second copy of the letter is written out, add up all of the numbers. Take this total and deliver it to Cindy along with the original message.
Bill can even send multiple copies of this number, called a “hash,” to make sure it gets to Cindy. Since this number is much shorter than the original message, sending multiple copies of it takes up significantly less memory than sending multiple copies of the message does.
When Cindy receives the message and the number, she can map all of the letters of the message to numbers, then add all of the numbers up. If no information has been lost or changed during delivery, the total number she comes up with will match the one sent with the message. This way, Cindy knows that the message arrived intact.
Principles of a good hash function
There are a couple of principles that can be used to judge whether a hash function is useful or not. First, a good hash function needs to create a completely different hash if even one character of a message is changed. This way, if a message has been tampered with, it will be obvious to the recipient.
Second, it should be very difficult for a third-party to find multiple messages that produce the same hash. This way, a third-party will not be able to switch a message out with a different one that just happens to have the same hash. These situations, where two different messages produce the same hash, are called “hash collisions.” Hash collisions are possible with any hash function, but should be rare and virtually impossible to find on purpose.
List of a few hash functions
Here are list of a few historical and modern hash functions
Secure Hash Algorithm 1 or “SHA-1” was created in 1993 by the U.S. National Security Agency as a way to secure government documents. The function worked well until 2005, when several cryptographers found ways to produce collisions with it using expensive computers. Still, many websites continued to use SHA-1 to create a secure connection with users until the practice was banned by all of the major web-browsers in 2017.
SHA-2 is a family of hash functions created by the NSA to replace SHA-1 after it was cracked. It includes SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, and SHA-512/256. It is currently used in a wide range of applications, such as password verification, digital signatures, file verification in file-sharing services, and others.
SHA-256 is a member of the SHA-2 family of hash functions. It produces a 256-bit or 32-byte hash. SHA-256 is used by bitcoin software to hash a public key into a bitcoin address. It is also used to create the cryptographic puzzle that miners must solve in order to have their blocks added to the bitcoin blockchain. Many other cryptocurrencies also use SHA-256 in this way.
RACE Integrity Primitives Evaluation Message Digest 160 or RIPEMD-160 was developed by the RIPE consortium, a group of private technology organizations that wanted to foster broadband Internet use in Europe. Along with SHA-256, bitcoin software uses RIPEMD-160 to hash public keys into bitcoin addresses.
How cryptocurrencies use hash functions
In bitcoin and in many other cryptocurrencies, hash functions are used to solve several problems. First, a user’s public key is hashed into a bitcoin address to prevent average users from obtaining the public key.
Second, all bitcoin transactions contain both a transaction message and a ”digital signature,” which is the same transaction message after having been hashed and encrypted with the user’s private key.
When a node receives the transaction message, it computes the hash of the message independently and then decrypts the digital signature using the user’s public key. If the message hasn’t been tampered with, the hash received from the user (after being decrypted) will be the same as the hash the node computed on its own. In this way, the node can verify that a hacker has not tampered with the message.
Note: The above two paragraphs are a simplified explanation of what happens in transaction verification. The details involve a bunch of algebra, but they do state the essence of what is going on in these algebraic computations.
Third, hashing is used to create a cryptographic puzzle for miners. If a miner finds the solution to this puzzle, its block gets added to the blockchain, and it is rewarded by being allowed to mint new bitcoins. The miner solves this puzzle by taking the hash of the previous block, adding the hash of the current transactions it is trying to process, and adding a random number (called the “nonce”) to this number. If the number that results is less than the target set by the difficulty algorithm, the miner “wins.” If the number that results is not less than the target, the miner chooses a new random number and starts over.
Because it is very difficult to find a value that results in a particular hash, it requires an enormous amount of work for a computer to solve this puzzle. But because it is very easy to calculate the hash of the answer once the answer is given, it is very easy for other nodes on the network to verify that a particular miner has solved the puzzle and earned the right to add its block to the blockchain
Thus, hash functions very useful in an application like cryptocurrencies that requires both security (computational difficulty of hashing) and speedy transaction verification (ease of calculating hashes after hash function completed).