What is the algorithm SHA256?

By April 27, 2021Bitcoin Business
Click here to view original web page at medium.com


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.

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

(stdin)= b76b7041106a75de9fa4fbf880b3886cc114cbfd570e1a17adb58b937afee351

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!

(stdin)= b76b7041106a75de9fa4fbf880b3886cc114cbfd570e1a17adb58b937afee351

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

(stdin)= 41dbcc447756ddd2c32ac99c76ef6f090fa0a63da5a6ecfda3311936f85daa85

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.

$ bitcoin-cli getrawtransaction edee419f93521f43259b763ffb42e4b882504534494381b7e18057015a27c548 2“txid”: “edee419f93521f43259b763ffb42e4b882504534494381b7e18057015a27c548”,“hash”: “12cf1e132b1d775f5403a875592b447a825f493c0eecdf6bbaa8f5e759c1c71d”,“hex”: “020000000001019a8bb2699fc92968c62d2197649c7d70a6a71d7d8ffb2d70cab8f138d666cec50100000000ffffffff02b88201000000000017a914ffd0dbb44402d5f8f12d9ba5b484a2c1bb47da42870000000000000000236a21636f72736f636f6d706c65746f2e626974636f696e696e616374696f6e2e636f6d0247304402205688399cb5a230f050330e2bc6d04d9864d459f85fec48a0118ca31be9239d530220228d7c04fe9e6eea3690033c01ed222284efaa01b28a9a7cae809bdb32d7ce7a0121020d12775323bbdaf0cb6e9a2b44ae7a591ef5872364e80e363a93d283c10b9e4f00000000”,

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

We get the first digest.

$ printf 020000000001019a8bb2699fc92968c62d2197649c7d70a6a71d7d8ffb2d70cab8f138d666cec50100000000ffffffff02b88201000000000017a914ffd0dbb44402d5f8f12d9ba5b484a2c1bb47da42870000000000000000236a21636f72736f636f6d706c65746f2e626974636f696e696e616374696f6e2e636f6d0247304402205688399cb5a230f050330e2bc6d04d9864d459f85fec48a0118ca31be9239d530220228d7c04fe9e6eea3690033c01ed222284efaa01b28a9a7cae809bdb32d7ce7a0121020d12775323bbdaf0cb6e9a2b44ae7a591ef5872364e80e363a93d283c10b9e4f00000000 | xxd -r -p | sha256sum -b33bf8e3e54327c84758e3442ccea54cfef3621ee4d7276cc1bdcde301d4c4796

We need to apply again the SHA256 on the digest.

$ printf 33bf8e3e54327c84758e3442ccea54cfef3621ee4d7276cc1bdcde301d4c4796 | xxd -r -p | sha256sum -b1dc7c159e7f5a8ba6bdfec0e3c495f827a442b5975a803545f771d2b131ecf12

The last step is to get the little endian representation

$ printf 1dc7c159e7f5a8ba6bdfec0e3c495f827a442b5975a803545f771d2b131ecf12 | tac -rs ..12cf1e132b1d775f5403a875592b447a825f493c0eecdf6bbaa8f5e759c1c71d

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!

Leave a Reply