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):


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:


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

Aug 31, 2018 in Blockchain by slayer
• 29,040 points

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 27,620 points

Related Questions In Blockchain

+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 9, 2018 in Blockchain by Shashank
• 10,330 points
0 votes
1 answer

How feasible is it to use one way cryptographic hash function?

you can use the ouput hash function ...READ MORE

answered May 7, 2018 in Blockchain by Johnathon
• 9,030 points
0 votes
1 answer
0 votes
1 answer
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,040 points
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
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(''); const mySocket ...READ MORE

answered Jul 9, 2018 in Blockchain by digger
• 27,620 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.