Hash functions are frequently used in the bitcoin protocol.  It is important that we understand what they are and how they are used.   A hash function is a mathematical function that takes and arbitrary long piece of information, and yields a district fixed length number.

Properties of a good hash function:

  • Deterministic
  • Collision resistant
  • One way

Hash functions that are used in the bitcoin protocol:

  • SHA256
  • RipeMD160

There are many other hash functions but they are not part of this course.

Lets look at why a hash function is useful.  Lets say a we site takes credit card numbers.  The website itself would not process the payment, it would pass off to a merchant provider to verify.  We could take a hash of the card number and then compare it to a hashed version at the bank.

The card number 4012888888881881 hashes (SHA256) to dd13cdf9af9dd3baf46ce96aecd7163cabf381ccb21e63f15f0fa10b1c663fa9.  Should the hashed card number to stolen by an attacker, they cannot use that information on another website to make a purchase.   Also, the dd13cdf9af9dd3baf46ce96aecd7163cabf381ccb21e63f15f0fa10b1c663fa9 can’t be reversed to form the original card number.

Say we increment the card number by 1, to 4012888888881882.  The hashed value is very different from the previous example b270d2f2177c283d95910c62f195faeaa109fd21b54cdf8e4b4f0786a8422de7

This is important, because we cannot guess numbers that might otherwise be sequential.

Note:  the hashed version is a Base 16 number.  It could also be represented as Base 64.

Hashes in Bitcoin

There are few parts in the bitcoin protocol that use hash functions.  To create a bitcoin address, a private key is double SHA256 hashed, and hashed with RipeMD160 too.  This gives the user good security as their private key cannot be determeind.

The proof of work protocol also is a hash + nonce unspent transactions.

Public Key and Private Key