<p>I've come across this several times when trying to come up with an easy way to calculate a txid, and I finally decided to try and write a little script to do the same.  I used built-in libraries and figured this would be a good place to share the script:</p>
<pre><code>#!/usr/bin/env python3
import hashlib
import sys
inhex = sys.argv[1]
tx = bytes.fromhex(inhex)
h1 = hashlib.sha256(tx).hexdigest()
b1 = bytes.fromhex(h1)
h2 = hashlib.sha256(b1).hexdigest()
ba = bytearray.fromhex(h2)
ba.reverse()
id = ''.join(format(x, '02x') for x in ba)
print(id)
</code></pre>
<p>I called the script <code>txhash.py</code>.  For validation's sake, running <code>./txhash.py 01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000</code> returns <code>4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b</code> as expected in the original question.</p>

CC BY-SA 4.0
2024-05-14T10:27:38.043Z
It is worth noting that this won't work for Segwit transactions, which probably form the majority of transactions nowadays. There is now a distinction between TXID and WTXID. For a Segwit transaction this would give the WTXID not the TXID.
RedGrittyBrick
CC BY-SA 4.0
2024-05-14T10:17:50.433Z
2024-05-14T10:17:50.433Z
rpseu
<blockquote>
  <p>Taking the sha256 hash of that hash gives</p>
  
  <p><code>3b a3 ed fd 7a 7b ..............</code></p>
  
  <p>But the real transaction hash according to blockexplorer.com is</p>
  
  <p><code>.............. 7b 7a fd ed a3 3b</code></p>
</blockquote>

<p>The answer I was getting was correct, but bytewise reversed.  I need to get used to Bitcoin using little-endian storage.</p>

CC BY-SA 3.0
2014-03-08T22:25:18.613Z
Actually, it is encouraged to post and share your own answer, when applicable.  http://stackoverflow.com/help/self-answer
makerofthings7
CC BY-SA 4.0
2018-05-23T18:15:10.377Z
You just saved me so many hours of research . i was facing exactly the same issue. cheers
Karan
CC BY-SA 3.0
2012-02-06T06:31:10.433Z
I found the answer to my own question shortly after asking it.  Should I delete the question and the answer or leave them both?
Chris Moore
CC BY-SA 3.0
2012-02-06T08:27:37.920Z
It's a valid question, it's a correct answer and you did the work to find it. I'd give it a few days to make sure someone doesn't post a more complete (or awesome) answer and then just accept your own answer. Ideally it's not the way StackExchange works, but it's perfectly valid to self-answer - trust me you're not a trend-setter on this one ;)
David Perry
CC BY-SA 3.0
2012-02-06T06:30:19.030Z
2012-02-06T06:30:19.030Z
Chris Moore
<p>I'm trying to calculate the hash of transactions in bitcoin blocks, but I'm not getting the right answers.</p>

<p>For example, the <a href="http://blockexplorer.com/b/0">genesis block has a single transaction</a>.</p>

<p>Here's how I'm attempting to calculate its hash...</p>

<p>Transactions are encoded as:</p>

<ul>
<li>a 32 bit 'nVersion'</li>
<li>a list of input transactions, vin</li>
<li>a list of output transactions, vout</li>
<li>a 32 bit 'nLockTime'</li>
</ul>

<p>For the transaction in the genesis block, these are:</p>

<ul>
<li>nVersion: <code>01000000</code></li>
<li>inputs
<ul>
<li>count: <code>01</code></li>
<li>1st input:
<ul>
<li>prevout_hash: <code>0000000000000000000000000000000000000000000000000000000000000000</code></li>
<li>prevout_n: <code>ffffffff</code></li>
<li>scriptSig: <code>4d</code>:<code>04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73</code></li>
<li>sequence: ffffffff</li>
</ul></li>
</ul></li>
<li>outputs
<ul>
<li>count: <code>01</code></li>
<li>1st output:
<ul>
<li>value: <code>00f2052a01000000</code> (hex(50*10^8) is 0000012a05f200, and bitcoin puts the bytes in reverse order)</li>
<li>scriptPubKey: <code>43</code>:<code>4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac</code></li>
</ul></li>
</ul></li>
<li>nLockTime: <code>00000000</code></li>
</ul>

<p>If I string all those together end to end, I get 204 bytes: <code>01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000</code></p>

<p>Taking the sha256 hash of this gives <code>27362e66e032c731c1c8519f43063fe0e5d070db1c0c3552bb04afa18a31c6bf</code>.</p>

<p>Taking the sha256 hash of that hash gives <code>3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a</code>.</p>

<p>But the real transaction hash according to blockexplorer.com is <code>4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b</code>.</p>

<p>What am I doing wrong?  How can I get to the correct transaction hash?</p>

<p>Here's my working in Python:</p>

<pre><code>&gt;&gt;&gt; import Crypto.Hash.SHA256 as hash, binascii
&gt;&gt;&gt; tx = '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000'
&gt;&gt;&gt; len(binascii.unhexlify(tx))
204
&gt;&gt;&gt; hash.new(binascii.unhexlify(tx)).digest().encode('hex_codec')
'27362e66e032c731c1c8519f43063fe0e5d070db1c0c3552bb04afa18a31c6bf'
&gt;&gt;&gt; hash.new(hash.new(binascii.unhexlify(tx)).digest()).digest().encode('hex_codec')
'3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a'
</code></pre>

CC BY-SA 3.0
2012-02-06T06:32:39.213Z
This is nearly a dup of http://bitcoin.stackexchange.com/questions/2177/how-to-calculate-a-hash-of-a-tx
Chris Moore
<transactions><hash><encoding>
How are transaction hashes calculated?
Chris Moore
# How are transaction hashes calculated?

(Asked by: username_0 on 2012-02-06)

I'm trying to calculate the hash of transactions in bitcoin blocks, but I'm not getting the right answers.
For example, the genesis block has a single transaction.
Here's how I'm attempting to calculate its hash...
Transactions are encoded as:

a 32 bit 'nVersion'
a list of input transactions, vin
a list of output transactions, vout
a 32 bit 'nLockTime'

For the transaction in the genesis block, these are:

nVersion: 01000000
inputs

count: 01
1st input:

prevout_hash: 0000000000000000000000000000000000000000000000000000000000000000
prevout_n: ffffffff
scriptSig: 4d:04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73
sequence: ffffffff


outputs

count: 01
1st output:

value: 00f2052a01000000 (hex(50*10^8) is 0000012a05f200, and bitcoin puts the bytes in reverse order)
scriptPubKey: 43:4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac


nLockTime: 00000000

If I string all those together end to end, I get 204 bytes: 01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000
Taking the sha256 hash of this gives 27362e66e032c731c1c8519f43063fe0e5d070db1c0c3552bb04afa18a31c6bf.
Taking the sha256 hash of that hash gives 3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a.
But the real transaction hash according to blockexplorer.com is 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b.
What am I doing wrong?  How can I get to the correct transaction hash?
Here's my working in Python:
>>> import Crypto.Hash.SHA256 as hash, binascii
>>> tx = '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000'
>>> len(binascii.unhexlify(tx))
204
>>> hash.new(binascii.unhexlify(tx)).digest().encode('hex_codec')
'27362e66e032c731c1c8519f43063fe0e5d070db1c0c3552bb04afa18a31c6bf'
>>> hash.new(hash.new(binascii.unhexlify(tx)).digest()).digest().encode('hex_codec')
'3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a'

**Tags:** transactions, hash, encoding

> **Comments:**
> **username_0**: This is nearly a dup of http://bitcoin.stackexchange.com/questions/2177/how-to-calculate-a-hash-of-a-tx  

---

## 2 answers

### Answer by username_0 on 2012-02-06. Score: 39 | Accepted answer

> Taking the sha256 hash of that hash gives
>
> `3b a3 ed fd 7a 7b ..............`
>
> But the real transaction hash according to blockexplorer.com is
>
> `.............. 7b 7a fd ed a3 3b`

The answer I was getting was correct, but bytewise reversed. I need to get used to Bitcoin using little-endian storage.

> **Comments:**
> **username_0**: I found the answer to my own question shortly after asking it.  Should I delete the question and the answer or leave them both?  
> **username_5**: It's a valid question, it's a correct answer and you did the work to find it. I'd give it a few days to make sure someone doesn't post a more complete (or awesome) answer and then just accept your own answer. Ideally it's not the way StackExchange works, but it's perfectly valid to self-answer - trust me you're not a trend-setter on this one ;)  
> **username_3**: Actually, it is encouraged to post and share your own answer, when applicable.  http://stackoverflow.com/help/self-answer  
> **username_4**: You just saved me so many hours of research . i was facing exactly the same issue. cheers  

---

### Answer by username_1 on 2024-05-14. Score: 1

I've come across this several times when trying to come up with an easy way to calculate a txid, and I finally decided to try and write a little script to do the same.  I used built-in libraries and figured this would be a good place to share the script:
#!/usr/bin/env python3
import hashlib
import sys
inhex = sys.argv[1]
tx = bytes.fromhex(inhex)
h1 = hashlib.sha256(tx).hexdigest()
b1 = bytes.fromhex(h1)
h2 = hashlib.sha256(b1).hexdigest()
ba = bytearray.fromhex(h2)
ba.reverse()
id = ''.join(format(x, '02x') for x in ba)
print(id)

I called the script txhash.py.  For validation's sake, running ./txhash.py 01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000 returns 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b as expected in the original question.

> **Comments:**
> **username_2**: It is worth noting that this won't work for Segwit transactions, which probably form the majority of transactions nowadays. There is now a distinction between TXID and WTXID. For a Segwit transaction this would give the WTXID not the TXID.  

---

