What is the algorithm SHA256?

Bitcoin in Action
Coinmonks
3 min readJun 18, 2020

--

Hi!

Often we received this kind of question during our Bitcoin course: What is the algorithm SHA256 and where Bitcoin uses it?

Bitcoin uses algorithm SHA256 in several scenarios, for instance during an address generation or during the generation of transaction id or during mining (proof of work)

SHA stands for Secure Hash Algorithm, converts a string (clear text) of arbitrary length in another string with fix length, in that case 256 bits because we are using SHA256.

It’s a one way algorithm , which means that it’s impossible to retrieve the clear text from the result of SHA256, called digest or hash. Retrieve the clear text it very complicated, like to find an atom in the universe, for that reason we can say “impossibile”

We are going to use an example in order to better understand what I want to explain.

In Action

First of all, create a digest from clear text, for example bitcoininaction

It’s impossible to understand what clear text was from digest.

What’s happen If I repeat the same action?

If your answer is: I get the same result, you are right!

What’s happen if I change the clear text a little bit? For instance I use bitcoininaction.com instead of bitcoininaction?

I get a completely different digest! For that reason is very easy to check the digest if I have the clear message.

Ok, it’s very interesting, but I’d like to see something about Bitcoin!

We are getting hash from the transaction that we made with previous video “Posso scrivere nella blockchain?”

To achieve our result we need to use the SHA256 twice and get the little endian representation. Shortly, Big endian and little endian is a topic about byte order.

The transaction of the previous video have that txid: edee419f93521f43259b763ffb42e4b882504534494381b7e18057015a27c548

We can retrieve the transaction using getrawtransaction command.

The hash is 12cf1e132b1d775f5403a875592b447a825f493c0eecdf6bbaa8f5e759c1c71d and we have to get it using SHA256.

We get the first digest.

We need to apply again the SHA256 on the digest.

The last step is to get the little endian representation

Super! The result is correct!

This is just an example about SHA256 and Bitcoin protocol but there could be many more. Bitcoin uses several algorithm, for example it uses RIPEMD160 while generating address. You can find the source code on our GitHub Repository!

See ya!

🎥 Canale youtube — Bitcoin in Action

🐙 GitHub: https://bit.ly/2Lj3yeY

— –

📖 Libro Bitcoin dalla teoria alla pratica (Amazon): https://amzn.to/2Ldym0F

📖 Libro Bitcoin dalla teoria alla pratica (pagamento in bitcoin): http://bit.ly/2ADHUN1

— –

🎥 Video Corso Bitcoin dalla teoria alla pratica: https://bit.ly/3cUJDyZ

— –

📖 Tascabile Bitcoin 199 domande (Amazon): https://amzn.to/3ckIkJj

📖 Tascabile Bitcoin 199 domande (pagamento in bitcoin): https://bit.ly/3cmz07W

— –

📖 Pocket Bitcoin 199 questions (Amazon): https://amzn.to/3fB4Kbs

📖 Pocket Bitcoin 199 questions (pagamento in bitcoin): https://bit.ly/2SUNzYz

— –

► Sito ufficiale: https://www.corsobitcoin.com/

► Twitter: https://twitter.com/satoshiwantsyou

► Facebook: https://www.facebook.com/satoshiwantsyou

► Linkedin: https://www.linkedin.com/company/bitcoin-dalla-teoria-alla-pratica

► Medium: https://medium.com/@satoshiwantsyou

► Instagram: https://www.instagram.com/satoshiwantsyou

In crypto we trust

--

--

Bitcoin in Action
Coinmonks

Bitcoin. Money for nerds? Digital gold? Perhaps, but certainly it is a social revolution!