Hash Functions

Danny Diekroeger @dannydiekroeger 路 Jul 09

Hash Functions are a key concept in computer science, cryptography, and yes #bitcoin.

It's impossible to explain how bitcoin works without first understanding what a Hash is.

If you'd like to expand your background knowledge, read on 馃憞

Danny Diekroeger @dannydiekroeger 路 Jul 09

Hashes are used for all kinds of things, but a simple analogy is to think of them like fingerprints for data. https://t.co/CQywn8n6jf

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

You can think of a Hash Function like a magic fingerprint reader. You can enter any data into the hash function, and it will spit out a fingerprint. https://t.co/AeY1Jop2KB

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

Anything can be used as a Hash Function, but a good hash function will ensure that different input data will each have unique fingerprints.

One of the most common Hash Functions used today is called SHA-256, which stands for "Secure Hash Algorithm, 256 bits"

Danny Diekroeger @dannydiekroeger 路 Jul 09

Check out the link below. Try entering your name and see what the Hash Function spits out

Notice that no matter how much or little data you enter, the resulting hash is always different

https://t.co/8yBYPCVm79

Danny Diekroeger @dannydiekroeger 路 Jul 09

Here's what happens when I type in my name. Notice that if you type in "Danny" as well, you'll get the exact same result. This is the SHA-256 hash (or "fingerprint") of my name. https://t.co/5ZYt8OWeYI

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

But what is this SHA-256 voodoo anyway? How is it calculating that fingerprint?

Basically, it does some fancy math on the input data.

If you're curious, you can look at the details here:

https://t.co/3gqDo6E5O8

Danny Diekroeger @dannydiekroeger 路 Jul 09

This fancy math guarantees a couple things:

1) Every fingerprint is very likely to be unique

2) If I give you just a fingerprint, you have no way of guessing what the input data was

This second point actually begins to erode the "fingerprint" analogy, but its very important.

Danny Diekroeger @dannydiekroeger 路 Jul 09

To explain further, here's an example question...

What is the input data who's SHA-256 hash is:

04a474a12bad5d61e9d461945e6785e28b8789c591efc33a75621a2e65183393

Danny Diekroeger @dannydiekroeger 路 Jul 09

Any guesses?

Didn't think so! According to what we know about math, it's impossible to figure out the answer to this question without just brute force guessing. https://t.co/o2SlHRRPZ8

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

So go ahead, start guessing! The input data could be anything. Maybe its the letter "A", or maybe its the entire text from the 3rd Harry Potter book.

You'll never know unless you guess... or if I tell you... https://t.co/4om5LsfVsZ

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

The answer is "Bitcoin is freedom"

Go ahead and run that through the hash function to verify I'm telling the truth! https://t.co/tOD8ZGQtnJ

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

Ok great, so we can play silly games with Hash functions, but what else can we do?

Well hashes are used for all kinds of things in computer science.

One common use case is storing passwords! https://t.co/D6QmyFSnqa

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

Imagine if a website just stored your password, in plain text, in their database.

Any employee could easily see all the passwords. And if any attacker got access to the database, they'd get them all too!

Bad security!

Instead, websites store the *hashes* of passwords https://t.co/vBDFROI7x7

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

When you login, the website takes your password and hashes it, then compares it to the hash that's stored in their database...

If it matches, you're good to go... https://t.co/gO1kc2sD5l

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

Ever wonder why the "Forgot my password" always makes you create a new password (instead of just telling you what your old one was)?

It's because the website doesn't know what your old password was!

They only have the Hash. They have no way of figuring out what the original was

Danny Diekroeger @dannydiekroeger 路 Jul 09

Great so Hashes are neat and useful, but how do they relate to #Bitcoin? ..... https://t.co/QEs0Poy1Zi

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

In Bitcoin, the SHA-256 function plays a key role in addresses, address scripts, and mining.

I won't go into all the details here, but I will finish with one more game that will begin to explain the mechanics of how Bitcoin uses hashes in its mining system... https://t.co/LayS4WT8fu

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

Ok here's the game...

I want you to come up with some data that starts with the word "Bitcoin", and SHA-256 hashes to something that begins with 000.

First person to come up with an answer wins a reward. https://t.co/b2KPTTpiBk

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

What's your strategy?

Well, remember that there's no way to start with the Hash and figure out the input data....

The only way is to brute force guess the input data!! https://t.co/QSjRVppZn6

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

We'll brute force it like this:

Calculate the hash of "Bitcoin-1" and see if it starts with 000.

Calculate the hash of "Bitcoin-2" and see if it starts with 000.

Then try "Bitcoin-3", "Bitcoin-4", etc.

The only way is to keep trying and checking until you find one! https://t.co/efue2OzcIp

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

Eventually if you keep up this process, you'll come across the number 1918, and check it out!

"Bitcoin-1918" hashes to 00007eb7d15a7f52aabb6bece4b1b3be3e606cc93d020c6f703bf7ff9bd2ac9e, which starts with '000' !! https://t.co/jEX5xUBt1m

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

How did I find that out? Well, I just wrote a simple script that does that same brute force process.

Try one number, check it, move on to the next, try it, etc... until you find a winner... https://t.co/nPzaeia7nh

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

In fact, what I've done is I "Proved" to you that my computer did some "Work"...

Does "Proof of Work" sound familiar? https://t.co/gmxL6ax5mB

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

Just edit our little game and replace the starting word "Bitcoin" with a list of bitcoin transactions thats basically how the bitcoin mining competition works! https://t.co/sU5Bg4IGh0

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

Miners are all trying to find a list of valid of transactions that Hash to something with a bunch of leadings 0s

And when one of them finds a valid one, they get to add a block to the blockchain and claim a big reward https://t.co/XqeS9rhUoH

[Tweet image. If not displayed, your browser may be blocking content from Twitter. Try turning off content blocking to view images on this page; on Firefox this can be done by clicking on the shield just to the left of the URL and turning off Enhanced Tracking Protection for this site only.]

Danny Diekroeger @dannydiekroeger 路 Jul 09

Anyway, that's the basics, and I'll end the thread on Hash Functions here.

Follow + DM or comment here if you have any questions about technical concepts that you'd like an overview of!

Learn about how to write your own.