<p>This is the correct way to double hash two tx's.
First you need to convert them into little endian, and then convert into raw binary data before hashing.</p>
<pre><code>&lt;?php

function le_raw ($str) {
    return pack(&quot;H*&quot;, join(&quot;&quot;, array_reverse(str_split($str, 2))) );
    // return map { pack &quot;H*&quot;, join &quot;&quot;, reverse /(..)/g } @_;
}
function be_hex ($raw) {
    return join(&quot;&quot;, array_reverse(str_split( unpack(&quot;H*&quot;, $raw)[1], 2)));
    // return map { join &quot;&quot;, reverse unpack(&quot;H*&quot;, $_) =~ /(..)/g } @_;
}

$tx1 = le_raw(&quot;32650049a0418e4380db0af81788635d8b65424d397170b8499cdc28c4d27006&quot;);
$tx2 = le_raw(&quot;30861db96905c8dc8b99398ca1cd5bd5b84ac3264a4e1b3e65afa1bcee7540c4&quot;);
$v = hash('sha256', hash('sha256', $tx1.$tx2, true), true );

echo be_hex($v), &quot;\n&quot;;
</code></pre>
<p>output</p>
<pre><code>d47780c084bad3830bcdaf6eace035e4c6cbf646d103795d22104fb105014ba3
</code></pre>

CC BY-SA 4.0
2020-11-19T12:07:50.617Z
Also, take a note of third param to hash(); https://www.php.net/manual/en/function.hash.php
mpapec
CC BY-SA 4.0
2020-11-19T12:06:30.287Z
2020-11-19T12:06:30.287Z
mpapec
<p>I found this example:</p>

<hr>

<p>Current merkle hash list:
  32650049a0418e4380db0af81788635d8b65424d397170b8499cdc28c4d27006
  30861db96905c8dc8b99398ca1cd5bd5b84ac3264a4e1b3e65afa1bcee7540c4</p>

<p>Current merkle hash list:
  d47780c084bad3830bcdaf6eace035e4c6cbf646d103795d22104fb105014ba3</p>

<hr>

<p>I have tried to calculate the hash using PHP:</p>

<pre><code>$tx1="32650049a0418e4380db0af81788635d8b65424d397170b8499cdc28c4d27006";
$tx2="30861db96905c8dc8b99398ca1cd5bd5b84ac3264a4e1b3e65afa1bcee7540c4";
$v=hash('sha256', hash('sha256',$tx1.$tx2) );
</code></pre>

<p>But I got:<br>
2b5f377b4adab64f489a2f73605ffb448b8add5b708d218729f9dfc58f1f5fe2</p>

<p>What is wrong?</p>

CC BY-SA 3.0
2017-10-16T09:32:35.533Z
you should hash the binary data, not the human-readable hexadecimal representation
amaclin
CC BY-SA 3.0
2017-10-17T17:38:24.387Z
@amaclin Sounds like that should be an answer, not just a comment
Pieter Wuille
<development><php><merkle-tree>
How To Calculate Merkle Root?
Sh Sh
# How To Calculate Merkle Root?

(Asked by: username_0 on 2017-10-16)

I found this example:

Current merkle hash list:
  32650049a0418e4380db0af81788635d8b65424d397170b8499cdc28c4d27006
  30861db96905c8dc8b99398ca1cd5bd5b84ac3264a4e1b3e65afa1bcee7540c4
Current merkle hash list:
  d47780c084bad3830bcdaf6eace035e4c6cbf646d103795d22104fb105014ba3

I have tried to calculate the hash using PHP:
$tx1="32650049a0418e4380db0af81788635d8b65424d397170b8499cdc28c4d27006";
$tx2="30861db96905c8dc8b99398ca1cd5bd5b84ac3264a4e1b3e65afa1bcee7540c4";
$v=hash('sha256', hash('sha256',$tx1.$tx2) );

But I got:
2b5f377b4adab64f489a2f73605ffb448b8add5b708d218729f9dfc58f1f5fe2
What is wrong?

**Tags:** development, php, merkle-tree

> **Comments:**
> **username_1**: you should hash the binary data, not the human-readable hexadecimal representation  
> **username_2**: @username_1 Sounds like that should be an answer, not just a comment  

---

## 1 answers

### Answer by username_3 on 2020-11-19. Score: 3

This is the correct way to double hash two tx's.
First you need to convert them into little endian, and then convert into raw binary data before hashing.

output
d47780c084bad3830bcdaf6eace035e4c6cbf646d103795d22104fb105014ba3

> **Comments:**
> **username_3**: Also, take a note of third param to hash(); https://www.php.net/manual/en/function.hash.php  

---

