Gareth Johnson
Gareth Johnson
Updated on November 17, 2021

Bitcoin is the first of many cryptocurrencies, a trailblazer technology many altcoins followed or even directly copied. The design that made Bitcoin so unique and impressive when it first hit the crypto-sphere is now well documented (if not well understood) and it is definitely well discussed.

One of the defining features of Bitcoin and altcoins like Ethereum or Litecoin is the use of cryptography for security. Cryptography is like glue for these digital currencies, preventing them from falling apart at the hands of scammers who would take advantage of the digital and decentralized nature of these currencies.

In this article, we discuss the cryptographic hash function Bitcoin uses, SHA256, in detail to explain how cryptography is relevant for all the layers of Bitcoin’s operation.

What Is a Cryptographic Hash Function?

A hash function, or simply a hash, is a mathematical way of turning an input of any length into an output of a fixed length. No matter how long or short the original data set is, its hash ends up being a fixed length. Even if you put a word or a novel through a hash function, you get a hash value with the same digits.

Maybe the most important thing about cryptographic hash functions is that they can not be reverse engineered. These are one-way functions. Once a dataset is hashed, you receive a hash value that can’t be decrypted to reveal the original message anymore. 

Gold bitcoin standing on top of paper with crypto trend written

You might be wondering how a cryptographic function that can’t be decrypted could be useful. There are actually quite a lot of uses for hashing functions even outside of cryptocurrencies. The most obvious benefit is that you can use a hash function to compare if a dataset is unaltered. The same dataset always gives the same hash value when it is put through the hash function, so it is possible to compare the validity of files/passwords/datasets.

Let’s go over some important properties of hash functions that make them useful for cryptocurrencies and Bitcoin in particular.

Deterministic

Some people refer to cryptographic hashes as fingerprints. That is because the same input will always give the same output once it is hashed. The hash value works like a fingerprint for the data. If the input data changes so do the hash value.

Unequivocal and Irreversible

Another thing about hash values is that output cannot be used to recover an input. Once you input data through a hash function, it gets chopped and stirred so much that it is impossible to obtain the original message, much like it is impossible to revert a delicious bowl of stir-fry to its ingredients. This is also known as pre-image resistance, meaning that the input can’t be guessed from the output. Funnily enough, the word “hash” actually comes from the French word “hacher” meaning “to hack, chop into small pieces.”

Unique and Collision-Resistant

Two different inputs should always give different hash outputs. Each different input needs to produce a unique hash value. This makes it possible for people to compare datasets and check if they are indeed the same.

Fast

The hash function can compute the hash function quickly for any input. This makes cryptographic hash functions very useful in daily operations, from online security to detecting security breaches.

Hashing Algorithms and Cryptocurrencies

One interesting thing about Satoshi Nakamoto’s Bitcoin proposal is that the pseudonymous creator of Bitcoin didn’t claim to invent many new things with it. Instead, they simply showed how an innovative design that simply brought together pre-existing concepts is by itself creative. Most of the components of Bitcoin had already been in use in other projects before Satoshi Nakamoto stitched them up together to bring Bitcoin to life, including Bitcoin’s hashing system.

Physical bitcoin standing on top of computer hardware

Adam Back, a British cryptographer and co-founder of Blockstream, designed a proof-of-work algorithm to filter spam and prevent denial-of-service attacks back in 1997. He even foresaw that his HashCash method could be used as a minting mechanism for cryptocurrencies, which was proven true when Bitcoin’s whitepaper credited Adam Back for its proof-of-work mining process. Many cryptocurrencies adopted Bitcoin’s (and Adam Back’s) Hashcash proof-of-work method to validate transactions and mint new coins.

Hashcash basically uses a hashing function to make users show they solved an arbitrary problem in order to prove they are intentional. Think of CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart), but older and based on cryptography instead of robots’ inability to read swirly lines and pick pictures of a bus, among others.

Adam Back designed the Hashcash problem in a way that required users to derive a known target hash value, an operation that takes enough energy to be too costly for spammers. While deriving the correct hash value can be difficult, verifying the solution is pretty easy, allowing the system to filter out all the unwanted mail.

Satoshi Nakamoto, in return, adopted the Hashcash proof-of-work method in order to come up with the designs for the blockchain technology, which is considered the bedrock of Bitcoin and all other digital currencies.

Bitcoin Hashing Algorithm

There are several different cryptographic hash functions with different applications. We said before that a hash function is a mathematical operation that takes any given input and turns it into a fixed-size output, usually a string of alphanumeric characters. The size of outputs depends on the exact hashing algorithm used for the hashing function.

Bitcoin uses a version of SHA (Secure Hash Algorithm), a family of cryptographic hash functions designed by the United States National Security Agency (NSA). Hashcash initially used the SHA1 hashing algorithm, but a design flaw that could potentially cause identical outputs for different inputs caused Satoshi Nakamoto to pick the SHA256 algorithm. Bitcoin actually uses double cycles for SHA256 hashing, meaning every hash value (or output) is hashed once more for security.

SHA256 hash function was first designed in 2001 and produces 32-byte outputs. It can be used to calculate trees of hashes (merkle trees) to verify the validity of cryptocurrency data blocks. Actually, cryptographic hash functions are literally at the root of Bitcoin because both the Bitcoin blockchain and Bitcoin mining process depend on hashing. 

Hashing Algorithm and Bitcoin Blockchain 

As you may already know, Bitcoin consists of blocks of transactions, each chained to each other chronologically through cryptography. Each block is actually a dataset that contains several transactions and the whole of the blockchain serves as a collective and public ledger for all Bitcoin transactions.

Binary code in green forming bitcoin icon on black background

But what prevents a Bitcoin user from making a transaction with false funds, or a Bitcoin miner from validating a block that gives him or herself millions of bitcoins? That is where the cryptography and hashing function steps in.

In the Bitcoin blockchain, all blocks are stamped with a cryptographic proof that shows the block is consistent with all the previous transactions. All blocks have their own unique hash value and they are all connected to each other like a big family.

Let’s illustrate how SHA256 cryptographic hashing function helps to keep the Bitcoin blockchain safe by describing exactly how blocks work. 

Each Bitcoin block has a block header that contains some information, including the block version number, timestamp of the block, the hash of the previous block, nonce, and the hash of the merkle root.

As we said before, each block contains many transactions. Each of these transactions gets a unique hash value. These unique hash values can also be hashed together to make a new unique hash value for the block. Then the block can be represented as a merkle tree, in which a final hash of all other hashes is the merkle root of the block. This way it becomes easy to track whether a transaction belongs in a block or not.

Another way hashing functions help Bitcoin is through public-key cryptography. The SHA256 algorithm (along with the RACE Integrity Primitives Evaluation Message Digest RIPEMD) is used to derive a public key from a private key and to derive multiple Bitcoin addresses from the one public key.

Bitcoin Mining and Hashing Algorithm

Bitcoin mining is where the cryptographic hashing function really makes a difference. Even though we think of Bitcoin mining mostly in terms of earning BTC, block rewards are actually just rewards for fulfilling a critical function i.e. securing the blockchain. Bitcoin miners have to prove that they are adding valid transactions to the blockchain to earn rewards and they race other miners to come up with the proof before everyone else.

Bitcoin uses the SHA256 algorithm to integrate Hashcash proof-of-work protocol to Bitcoin mining. That basically means that protocol spits out a target hash for each new block. Miners have to compile a block of transactions and come up with a hash that equals (or smaller than) the target hash. That translates into hashing all the transactions, and then hashing those hashes together to come up with the block hash. However, the odds of that happening are infinitesimally small.

What really happens is that once miners get a block hash, they have to hash the block with a random number until the target hash is found. These random numbers are called “nonces” for numbers only used once. If a number is hashed with a block hash and the hash output doesn’t match the target block, the number is discarded and new numbers are tried until the desired hash output emerges.

Miner mini figure digging for bitcoin on the ground

That random number is a variable and the odds of finding it changes based on the difficulty rate of the block, which gets adjusted approximately every two weeks based on the total computing power of the Bitcoin network. Once a nonce is found, everyone can easily run the hashing algorithm on the block hash and nonce to confirm whether the block checks out. Full nodes in the Bitcoin network automatically do this and every full node has to confirm the validity of a block before it can be accepted as valid.

How Does the Hashing Algorithm Protect the Bitcoin Blockchain?

Remember how we said each block contains the hash of the previous block? That works like a digital signature for the block. If anyone tries to change the contents of one block by tampering with the blockchain, they inevitably change the entire block hash. Since changing the hash of a single block affects all the blocks in the chain, it is impossible to make such an attack without enough computing power to tamper with the whole chain. That requires 51% of all the computing power in the Bitcoin network.

A Few Words Before You Go…

A hash function is a mathematical function that transforms any length of data into a fixed-size string. A cryptographic hash function works as a one-way mechanism for obtaining a unique value for a single dataset. The output hash can’t be reversed to reveal the original input, providing high-level information security. Collision resistance and deterministic function make cryptographic hashing a good way of ensuring the integrity of transaction blocks. Several Bitcoin protocols, including proof-of-work and public-key cryptography, use the SHA256 algorithm for cryptographic hashing.