<p>You have to byte-swap the hex values BEFORE concatenating them. You also have to byte-swap the double hashed hex string!</p>

<blockquote>
  <blockquote>
    <blockquote>
      <p>import hashlib 
      header_hex = "435fc2d898ebcd7821c6d407d8d915f9ef4f106057df089ddf3be7abbeef51a7435fc2d898ebcd7821c6d407d8d915f9ef4f106057df089ddf3be7abbeef51a7" </p>
      
      <p>header_bin = header_hex.decode('hex') 
      hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest() 
      hash.encode('hex_codec') 'b3b366122163d146f56ecdcd0f5029f387da1d9f62e0fbb4b9c22c3eaaa0ec15' >>> </p>
      
      <p>hash[::-1].encode('hex_codec') '15eca0aa3e2cc2b9b4fbe0629f1dda87f329500fcdcd6ef546d163211266b3b3' </p>
    </blockquote>
  </blockquote>
</blockquote>

CC BY-SA 4.0
2018-07-08T05:02:38.090Z
2018-07-08T05:02:38.090Z
Ben Stolman
<p>Concatenating and double SHA-256 hashing</p>

<p>"a751efbeabe73bdf9d08df5760104feff915d9d807d4c62178cdeb98d8c25f43" </p>

<p>with itself</p>

<p>"a751efbeabe73bdf9d08df5760104feff915d9d807d4c62178cdeb98d8c25f43" </p>

<p>should output
15eca0aa3e2cc2b9b4fbe0629f1dda87f329500fcdcd6ef546d163211266b3b3</p>

<blockquote>
  <blockquote>
    <blockquote>
      <p>import hashlib
      header_hex = "a751efbeabe73bdf9d08df5760104feff915d9d807d4c62178cdeb98d8c25f43a751efbeabe73bdf9d08df5760104feff915d9d807d4c62178cdeb98d8c25f43"</p>
      
      <p>header_bin = header_hex.decode('hex')</p>
      
      <p>hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()</p>
      
      <p>hash.encode('hex_codec')
      '2b4a9fdb97e89e73c4647791e476090eaad04f43c93a3de21a95d2c4fc8e8e0c'</p>
    </blockquote>
  </blockquote>
</blockquote>

<p>I'm getting the wrong answer :/?</p>

CC BY-SA 4.0
2018-07-08T01:20:30.870Z
Your code looks correct. Where do you read that the double-SHA256 hash of a751... input concatenated with itself should have 15eca... as output?
Pieter Wuille
CC BY-SA 4.0
2018-07-08T01:29:17.993Z
https://bitcointalk.org/index.php?topic=44707.msg534605#msg534605 the bottom of the last page
Ben Stolman
CC BY-SA 4.0
2018-07-08T01:35:20.987Z
https://www.blockchain.com/en/btc/block-index/120872 the Merkle Root in the example on the forum is the same as the one on Blockchain.info
Ben Stolman
CC BY-SA 4.0
2018-07-08T01:37:36.267Z
Block hashes are often displayed in byte-reversed order. Try reversing the bytes before hashing, and again on the computed hash.
Pieter Wuille
CC BY-SA 4.0
2018-07-08T01:46:26.633Z
>>> import hashlib
>>> header_hex = "435fc2d898ebcd7821c6d407d8d915f9ef4f106057df089ddf3be7abbeef51a7435fc2d898ebcd7821c6d407d8d915f9ef4f106057df089ddf3be7abbeef51a7"
>>> header_bin = header_hex.decode('hex')
>>> hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
>>> hash.encode('hex_codec') 
'b3b366122163d146f56ecdcd0f5029f387da1d9f62e0fbb4b9c22c3eaaa0ec15'
>>> hash[::-1].encode('hex_codec')
'15eca0aa3e2cc2b9b4fbe0629f1dda87f329500fcdcd6ef546d163211266b3b3' YIPEE THANK YOU PIETER!
Ben Stolman
CC BY-SA 4.0
2018-07-08T03:28:24.127Z
Don't forget to write an answer yourself if you fixed the issue.
Pieter Wuille
<blockchain><hash>
How do I concatenate two hashes in Python?
Ben Stolman
# How do I concatenate two hashes in Python?

(Asked by: username_0 on 2018-07-08)

Concatenating and double SHA-256 hashing

"a751efbeabe73bdf9d08df5760104feff915d9d807d4c62178cdeb98d8c25f43"

with itself

"a751efbeabe73bdf9d08df5760104feff915d9d807d4c62178cdeb98d8c25f43"

should output
15eca0aa3e2cc2b9b4fbe0629f1dda87f329500fcdcd6ef546d163211266b3b3

> > > import hashlib
> > > header\_hex = "a751efbeabe73bdf9d08df5760104feff915d9d807d4c62178cdeb98d8c25f43a751efbeabe73bdf9d08df5760104feff915d9d807d4c62178cdeb98d8c25f43"
> > >
> > > header\_bin = header\_hex.decode('hex')
> > >
> > > hash = hashlib.sha256(hashlib.sha256(header\_bin).digest()).digest()
> > >
> > > hash.encode('hex\_codec')
> > > '2b4a9fdb97e89e73c4647791e476090eaad04f43c93a3de21a95d2c4fc8e8e0c'

I'm getting the wrong answer :/?

**Tags:** blockchain, hash

> **Comments:**
> **username_1**: Your code looks correct. Where do you read that the double-SHA256 hash of a751... input concatenated with itself should have 15eca... as output?  
> **username_0**: https://bitcointalk.org/index.php?topic=44707.msg534605#msg534605 the bottom of the last page  
> **username_0**: https://www.blockchain.com/en/btc/block-index/120872 the Merkle Root in the example on the forum is the same as the one on Blockchain.info  
> **username_1**: Block hashes are often displayed in byte-reversed order. Try reversing the bytes before hashing, and again on the computed hash.  
> **username_0**: >>> import hashlib >>> header_hex = "435fc2d898ebcd7821c6d407d8d915f9ef4f106057df089ddf3be7abbeef51a7435fc2d898ebcd7821c6d407d8d915f9ef4f106057df089ddf3be7abbeef51a7" >>> header_bin = header_hex.decode('hex') >>> hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest() >>> hash.encode('hex_codec')  'b3b366122163d146f56ecdcd0f5029f387da1d9f62e0fbb4b9c22c3eaaa0ec15' >>> hash[::-1].encode('hex_codec') '15eca0aa3e2cc2b9b4fbe0629f1dda87f329500fcdcd6ef546d163211266b3b3' YIPEE THANK YOU username_1!  
> **username_1**: Don't forget to write an answer yourself if you fixed the issue.  

---

## 1 answers

### Answer by username_0 on 2018-07-08. Score: 2 | Accepted answer

You have to byte-swap the hex values BEFORE concatenating them. You also have to byte-swap the double hashed hex string!

> > > import hashlib
> > > header\_hex = "435fc2d898ebcd7821c6d407d8d915f9ef4f106057df089ddf3be7abbeef51a7435fc2d898ebcd7821c6d407d8d915f9ef4f106057df089ddf3be7abbeef51a7"
> > >
> > > header\_bin = header\_hex.decode('hex')
> > > hash = hashlib.sha256(hashlib.sha256(header\_bin).digest()).digest()
> > > hash.encode('hex\_codec') 'b3b366122163d146f56ecdcd0f5029f387da1d9f62e0fbb4b9c22c3eaaa0ec15' >>>
> > >
> > > hash[::-1].encode('hex\_codec') '15eca0aa3e2cc2b9b4fbe0629f1dda87f329500fcdcd6ef546d163211266b3b3'


---

