How to compute SHA256 hash for bitcoin header

0 votes

I am trying to understand the proof of work algorithm. I computed a block header (which includes the nonce):

"02000000aaf8ab82362344f49083ee4edef795362cf135293564c4070000000000000000c009bb6222e9bc4cdb8f26b2e8a2f8d163509691a4038fa692abf9a474c9b21476800755c02e17181fe6c1c3"

I have to apply SHA256 to this twice. I tried pack, unpack, hex, etc., but I can't get this output. 

The correct answer is supposed to be:

"00000000000000001354e21fea9c1ec9ac337c8a6c0bda736ec1096663383429"

But i get different answer. Can somebody help me with this?

Aug 31, 2018 in Blockchain by slayer
• 29,350 points
1,313 views

1 answer to this question.

0 votes
header_hex = "02000000aaf8ab82362344f49083ee4edef795362cf135293564c4070000000000000000c009bb6222e9bc4cdb8f26b2e8a2f8d163509691a4038fa692abf9a474c9b21476800755c02e17181fe6c1c3"

# Decode header hex into binary string
header = [header_hex].pack("H*")

# Apply SHA256 twice
require "digest"
d1 = Digest::SHA256.digest(header)
d2 = Digest::SHA256.digest(d1)

# Convert to hex
result = d2.unpack("H*").join
# => "293438636609c16e73da0b6c8a7c33acc91e9cea1fe254130000000000000000"

Oops, for some reason the result is somewhat "backwards". Perhaps it is a byte-ordering issue? Let's try that again with the binary data reversed:

result = d2.reverse.unpack("H*").join
# => "00000000000000001354e21fea9c1ec9ac337c8a6c0bda736ec1096663383429"
answered Aug 31, 2018 by digger
• 26,740 points

Related Questions In Blockchain

0 votes
1 answer

(bitcoin) Calculate hash from getwork function - how to do it?

This is correct as there is a ...READ MORE

answered Feb 28, 2022 in Blockchain by Soham
• 9,700 points
490 views
0 votes
0 answers

How to get Bitcoin value for corresponding USD value in ASP.NET C#?

I want to get the Bitcoin value ...READ MORE

Mar 1, 2022 in Blockchain by Soham
• 9,700 points
672 views
0 votes
0 answers

hd wallet bip44 in js - how to create an address for a chain other than bitcoin?

I have a small script basically taken ...READ MORE

Mar 2, 2022 in Blockchain by Aditya
• 7,680 points
831 views
+1 vote
4 answers

How to estimate the cost for deploying smart contract on mainnet?

Since you have already deployed the contract ...READ MORE

answered Apr 10, 2018 in Blockchain by Shashank
• 10,400 points
14,729 views
0 votes
1 answer

How to add Bitcoin as currency for my website?

You cannot use BTC as the currency ...READ MORE

answered Aug 17, 2018 in Blockchain by slayer
• 29,350 points
831 views
0 votes
1 answer
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,693 views
0 votes
1 answer
0 votes
1 answer

How i can use nodejs to watch transactions in bitcoin network?

you can use  const Socket = require('blockchain.info/Socket'); const mySocket ...READ MORE

answered Jul 9, 2018 in Blockchain by digger
• 26,740 points
1,040 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP