Hash Function
  • Hash256
  • Hash160
  • Reverse Bytes
  • Hexadecimal
Youtube

Hash Function A small program that scrambles data.

A hash function is a mini computer program that takes data, scrambles it, and gives you a unique fixed-length result.

The cool thing about hash functions is that:

  • You can put as much data as you want in to the hash function, but it will always return the same-length result.
  • The result is unique, so you can use it as a way to identify that data.

So in other words, a hash function allows you to create a digital fingerprint for whatever data you put in to it.

Try one out: SHA256

Hash Function Properties.

A good hash function has 3 important properties that make it useful.

Note: The SHA256 hash function is the main one used in Bitcoin, so I’ll use that in my upcoming examples.

1. You cannot work out the original data from the result.

A cryptographic hash function produces a random result (with no patterns), so there is no way of “going backwards” through the hash function to figure out what the original data was.

This is the property of a cryptographic hash function. You may be able to reconstruct the original data from the result of a “basic” hash function, but a cryptographic hash function’s job is to make this as difficult as possible.

2. The same data always returns the same result.

A hash function scrambles data systematically, so that the same input will always produce the same result.

If you put some data in to a hash function, you can be sure that data going to produce the same result every time.

For example:

data                sha256(data)
---------------     ----------------------------------------------------------------
learnmeabitcoin     ef235aacf90d9f4aadd8c92e4b2562e1d9eb97f0df9ba3b508258739cb013db2
learnmeabitcoin     ef235aacf90d9f4aadd8c92e4b2562e1d9eb97f0df9ba3b508258739cb013db2
learnmeabitcoin     ef235aacf90d9f4aadd8c92e4b2562e1d9eb97f0df9ba3b508258739cb013db2

3. Different data produces different results.

If you put unique data in to the hash function, the hash function will give you a unique result.

Even the smallest changes in data return wildly different results.

For example:

data                sha256(data)
----------------    ----------------------------------------------------------------
learnmeabitcoin     ef235aacf90d9f4aadd8c92e4b2562e1d9eb97f0df9ba3b508258739cb013db2
learnmeabitcoin1    f94a840f1e1a901843a75dd07ffcc5c84478dc4f987797474c9393ac53ab55e6
learnmeabitcoin2    b9638ef00b064055b5d0b408414be02f3ab66cce752c7ac3b7595b0fffaa6567
learnmeabitcoin3    c6fd80741e150fb7ee71453fb0a2a391261f6a0d4d60759b843639e6cbae7b91
learnmeabitcoin4    255da46dc8699fffd841b7c66a31eeb4f8eda8e1ca6850c7356376518f52d3c1

If different data returned the same result it would be called a “collision”, and it would mean the hash function was broken.

Where are hash functions used in Bitcoin?

1. Transaction Hashes

You hash transaction data to get a TXID (Transaction ID, Transaction Hash).

  • The ability to hash a long string of transaction data in to a short, unique string allows you to create a unique identifier for each transaction.

2. Block Hashes (and Mining)

You hash block headers to get a block hash.

  • So you can also create a unique ID for each block.
  • The fact that each hash result is random allows for the mechanism of mining.

3. Addresses

A public key is hashed (using both SHA256 and RIPEMD160) in the process of creating a bitcoin address.

  • The fact that you cannot work backwards from a hash result helps with the security of public keys when they are placed inside locking scripts.

By Greg Walker,

Last Updated: 09 Oct 2019
  • 09 Oct 2019: renamed browser to explorer, glossary to guide, and guide to beginners

Hey there, it's Greg.

I'll let you know about cool website updates, or if something seriously interesting happens in bitcoin.


Don't worry, it doesn't happen very often.