<p>Here's a python implementation to find the doubleSHA256 hash for genesis block:</p>

<pre><code>01000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000

import codecs

//switch the endianness of a given string
def revEndian(string):
    return ''.join(reversed([string[i:i+2] for i in range(0, len(string), 2)]))

//convert a bytebuffer into a string
def hashStr(bytebuffer):
    return str(codecs.encode(bytebuffer, 'hex'))[2:-1]

//find the double sha256 hash for a given hex string
def doubleSha256(hex): 
    bin = codecs.decode(hex, 'hex')
    hash = hashlib.sha256(bin).digest()
    hash2 = hashlib.sha256(hash).digest()
    return revEndian(hashStr(hash2))
</code></pre>

CC BY-SA 4.0
2022-02-11T05:03:23.803Z
How is hex computed from the transaction?
Clemens
CC BY-SA 4.0
2018-06-13T06:25:04.393Z
2018-06-14T01:51:20.117Z
2018-06-14T01:51:20.117Z
ArchitPandey
<p>There's a diagram explaining the process of hashing a transaction when creating or verifying the signature of a bitcoin transaction on <a href="https://bitcointalk.org/index.php?topic=29416.msg370321#msg370321" rel="nofollow">the forum</a>, and a higher resolution version of the same image on <a href="http://dl.dropbox.com/u/1139081/BitcoinImg/OpCheckSigDiagram.png" rel="nofollow">dropbox</a>.</p>

CC BY-SA 3.0
2017-03-23T18:22:13.970Z
This image is now dead and the links do not work. This is why you are supposed to POST THE IMAGE not links to an image.
Harry Pillsbury
CC BY-SA 3.0
2012-02-09T02:26:51.850Z
2012-02-09T02:26:51.850Z
Chris Moore
<p>As explained by <a href="https://bitcointalk.org/index.php?topic=54451.msg649253#msg649253" rel="noreferrer">Gavin Andersen on the forum</a> - in order to calculate the ID hash of the Tx that is used in the Merkle Tree, one needs to SHA hash <a href="https://en.bitcoin.it/wiki/Protocol_specification#tx" rel="noreferrer">the whole Tx message</a> as defined in <a href="https://en.bitcoin.it/wiki/Protocol_specification#tx" rel="noreferrer">the Protocol Specification wiki page</a> twice.</p>

<p>For example, for the only Tx from the <a href="http://blockexplorer.com/block/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" rel="noreferrer">Genesis Block</a> one would double hash the following array of bytes:</p>

<p>01000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000</p>

<p>And if double hashed correctly, it would give the following hash:</p>

<p><a href="http://blockexplorer.com/tx/4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b" rel="noreferrer">4A5E1E4BAAB89F3A32518A88C31BC87F618F76673E2CC77AB2127B7AFDEDA33B</a></p>

CC BY-SA 3.0
2016-03-07T00:42:52.257Z
Little endian! In most implementations that double-hash would yield `3b a3 ed fd 7a 7b 12 b2 7a c7 2c 3e 67 76 8f 61 7f c8 1b c3 88 8a 51 32 3a 9f b8 aa 4b 1e 5e 4a`, but you'll want to reverse that: `4a 5e 1e 4b aa b8 9f 3a 32 51 8a 88 c3 1b c8 7f 61 8f 76 67 3e 2c c7 7a b2 12 7b 7a fd ed a3 3b`, resulting in the actual hash that is *actually* used.
19h
CC BY-SA 3.0
2018-02-11T15:40:32.060Z
Thanks, here's my php implementation: `function getTransactionHash($transaction_in_hex)
{
    $bin = hex2bin($transaction_in_hex);
    $hash = hex2bin(hash('sha256', hex2bin(hash('sha256', $bin))));
    return bin2hex(strrev($hash));
}`
felix021
CC BY-SA 3.0
2011-12-14T01:00:12.590Z
2011-12-14T01:00:12.590Z
ThePiachu
<p>How does one calculate the hash of a Tx? Which fields need to be double SHA hashed and in what order?</p>

CC BY-SA 3.0
2011-12-08T03:32:55.277Z
Do you mean the transaction ID hash that the JSON-RPC send* methods return? It can be confusing because hashes for transactions are calculated and signed to transmit bitcoins, but the hashes that are signed are NOT the same as the transaction ID hashes....
gavinandresen
CC BY-SA 3.0
2011-12-08T11:16:38.773Z
I mean the hashes calculated for the merkle tree, but I suppose both would be useful.
ThePiachu
<transactions><protocol><hash><algorithms>
How to calculate a hash of a Tx?
ThePiachu
# How to calculate a hash of a Tx?

(Asked by: username_0 on 2011-12-07)

How does one calculate the hash of a Tx? Which fields need to be double SHA hashed and in what order?

**Tags:** transactions, protocol, hash, algorithms

> **Comments:**
> **username_1**: Do you mean the transaction ID hash that the JSON-RPC send* methods return? It can be confusing because hashes for transactions are calculated and signed to transmit bitcoins, but the hashes that are signed are NOT the same as the transaction ID hashes....  
> **username_0**: I mean the hashes calculated for the merkle tree, but I suppose both would be useful.  

---

## 3 answers

### Answer by username_0 on 2011-12-14. Score: 9 | Accepted answer

As explained by [Gavin Andersen on the forum](https://bitcointalk.org/index.php?topic=54451.msg649253#msg649253) - in order to calculate the ID hash of the Tx that is used in the Merkle Tree, one needs to SHA hash [the whole Tx message](https://en.bitcoin.it/wiki/Protocol_specification#tx) as defined in [the Protocol Specification wiki page](https://en.bitcoin.it/wiki/Protocol_specification#tx) twice.

For example, for the only Tx from the [Genesis Block](http://blockexplorer.com/block/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f) one would double hash the following array of bytes:

01000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000

And if double hashed correctly, it would give the following hash:

[4A5E1E4BAAB89F3A32518A88C31BC87F618F76673E2CC77AB2127B7AFDEDA33B](http://blockexplorer.com/tx/4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b)

> **Comments:**
> **username_6**: Little endian! In most implementations that double-hash would yield `3b a3 ed fd 7a 7b 12 b2 7a c7 2c 3e 67 76 8f 61 7f c8 1b c3 88 8a 51 32 3a 9f b8 aa 4b 1e 5e 4a`, but you'll want to reverse that: `4a 5e 1e 4b aa b8 9f 3a 32 51 8a 88 c3 1b c8 7f 61 8f 76 67 3e 2c c7 7a b2 12 7b 7a fd ed a3 3b`, resulting in the actual hash that is *actually* used.  
> **username_7**: Thanks, here's my php implementation: `function getTransactionHash($transaction_in_hex) {     $bin = hex2bin($transaction_in_hex);     $hash = hex2bin(hash('sha256', hex2bin(hash('sha256', $bin))));     return bin2hex(strrev($hash)); }`  

---

### Answer by username_2 on 2018-06-13. Score: 1

Here's a python implementation to find the doubleSHA256 hash for genesis block:

```
01000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000

import codecs

//switch the endianness of a given string
def revEndian(string):
    return ''.join(reversed([string[i:i+2] for i in range(0, len(string), 2)]))

//convert a bytebuffer into a string
def hashStr(bytebuffer):
    return str(codecs.encode(bytebuffer, 'hex'))[2:-1]

//find the double sha256 hash for a given hex string
def doubleSha256(hex): 
    bin = codecs.decode(hex, 'hex')
    hash = hashlib.sha256(bin).digest()
    hash2 = hashlib.sha256(hash).digest()
    return revEndian(hashStr(hash2))

```

> **Comments:**
> **username_3**: How is hex computed from the transaction?  

---

### Answer by username_4 on 2012-02-09. Score: -3

There's a diagram explaining the process of hashing a transaction when creating or verifying the signature of a bitcoin transaction on [the forum](https://bitcointalk.org/index.php?topic=29416.msg370321#msg370321), and a higher resolution version of the same image on [dropbox](http://dl.dropbox.com/u/1139081/BitcoinImg/OpCheckSigDiagram.png).

> **Comments:**
> **username_5**: This image is now dead and the links do not work. This is why you are supposed to POST THE IMAGE not links to an image.  

---

