<p>A Bitcoin block is a container data structure that aggregates all the transactions. The block is made of a header (containing metadata), followed by a long list of transactions that are included in the block. The block header consists of merkle tree root, previous block header hash, timestamp amongst other metadata.</p>

<p>The Merkle tree root in simpler terms includes the summary of all the transactions in the block using the merkle tree. Every transaction in block is serialized and hashed twice with <code>SHA256</code> to get the transaction id or <code>txid</code>. These <code>txid</code>s of all the transactions in the block is then double-hashed pairwise, with <code>SHA256</code>, until there is only one node at the top called a merkle root. Since this merkle root is built from all the transactions in the block themselves, the root can be thought as containing the summary of all the transactions included. An illustrative example of Merkle root calculation is shown below:</p>

<p><a href="https://i.sstatic.net/CU3ME.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/CU3ME.png" alt="enter image description here"></a></p>

<p>The header portion of the block is hashed twice with <code>SHA256</code> to get the block header hash (in the mining process). This block header hash is always referenced in the next block in the <code>previousblockhash</code> field. Thus every block is linked to the previous block using its header hash forming a link that goes all the way back to the genesis block like the illustration shown below:</p>

<p><a href="https://i.sstatic.net/1lpk8.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/1lpk8.png" alt="enter image description here"></a></p>

<p>Since <code>SHA256</code> is a one way mathematical function, there is no way to reverse engineer the data by just looking at the hash. <code>SHA256</code> is also deterministic, in the sense that a particular data will always give a particular hash. Slightly changing the data will completely change the hash output.</p>

<p>So, if a party wants to change some data in a transaction that is included in a block somewhere in the blockchain, it will change the merkle root of that block. Since merkle root forms the part of the block header, changing the merkle root will completely change the block header hash. Since this hash is referenced in the next block, the current hash will be incompatible with the hash that was referenced and hence this block will not be accepted.</p>

<p>This is where proof of work in mining plays a key role. As I have said above that changing a data in a block completely changes the block header. So if you want to change a data in a past block, you will have to re-mine all subsequent blocks. So you will have to spend the electricity and compute cost to re-mine all the block AND outpace the network by forming the longest chain. The cost of doing such an action is very high, and may not outweigh the benefits that you derive from it, thus providing inherent security to Bitcoin.</p>

<p>Also, I would like to point out that you cannot change transaction data structure at your whims and fancies. Every transaction is signed with a private key that includes the entire transaction data as a message. So, slightly changing the transaction data will render the transaction invalid and including such invalid transactions in a block will render the block invalid. So, in a nutshell all that you can do is include/exclude certain transaction from previous blocks, or make slight cosmetic changes (like ECDSA signature malleability).</p>

CC BY-SA 4.0
2019-11-01T11:06:48.667Z
2019-11-02T07:33:48.630Z
2019-11-02T07:33:48.630Z
Ugam Kamat
<p>I am wondering how Bitcoin uses cryptography and secure hash algorithm such as SHA256 to link the records in the blockchain. And if possible I think it will give me a better understanding if there is a demonstration of a figure of blockchains.</p>

<blockchain><hash><sha256>
How does Bitcoin utilize cryptography and secure hash algorithm (SHA) to link records in the blockchain?
hypergogeta
# How does Bitcoin utilize cryptography and secure hash algorithm (SHA) to link records in the blockchain?

(Asked by: username_0 on 2019-11-01)

I am wondering how Bitcoin uses cryptography and secure hash algorithm such as SHA256 to link the records in the blockchain. And if possible I think it will give me a better understanding if there is a demonstration of a figure of blockchains.

**Tags:** blockchain, hash, sha256


---

## 1 answers

### Answer by username_1 on 2019-11-01. Score: 2 | Accepted answer

A Bitcoin block is a container data structure that aggregates all the transactions. The block is made of a header (containing metadata), followed by a long list of transactions that are included in the block. The block header consists of merkle tree root, previous block header hash, timestamp amongst other metadata.

The Merkle tree root in simpler terms includes the summary of all the transactions in the block using the merkle tree. Every transaction in block is serialized and hashed twice with `SHA256` to get the transaction id or `txid`. These `txid`s of all the transactions in the block is then double-hashed pairwise, with `SHA256`, until there is only one node at the top called a merkle root. Since this merkle root is built from all the transactions in the block themselves, the root can be thought as containing the summary of all the transactions included. An illustrative example of Merkle root calculation is shown below:

[![enter image description here](https://i.sstatic.net/CU3ME.png)](https://i.sstatic.net/CU3ME.png)

The header portion of the block is hashed twice with `SHA256` to get the block header hash (in the mining process). This block header hash is always referenced in the next block in the `previousblockhash` field. Thus every block is linked to the previous block using its header hash forming a link that goes all the way back to the genesis block like the illustration shown below:

[![enter image description here](https://i.sstatic.net/1lpk8.png)](https://i.sstatic.net/1lpk8.png)

Since `SHA256` is a one way mathematical function, there is no way to reverse engineer the data by just looking at the hash. `SHA256` is also deterministic, in the sense that a particular data will always give a particular hash. Slightly changing the data will completely change the hash output.

So, if a party wants to change some data in a transaction that is included in a block somewhere in the blockchain, it will change the merkle root of that block. Since merkle root forms the part of the block header, changing the merkle root will completely change the block header hash. Since this hash is referenced in the next block, the current hash will be incompatible with the hash that was referenced and hence this block will not be accepted.

This is where proof of work in mining plays a key role. As I have said above that changing a data in a block completely changes the block header. So if you want to change a data in a past block, you will have to re-mine all subsequent blocks. So you will have to spend the electricity and compute cost to re-mine all the block AND outpace the network by forming the longest chain. The cost of doing such an action is very high, and may not outweigh the benefits that you derive from it, thus providing inherent security to Bitcoin.

Also, I would like to point out that you cannot change transaction data structure at your whims and fancies. Every transaction is signed with a private key that includes the entire transaction data as a message. So, slightly changing the transaction data will render the transaction invalid and including such invalid transactions in a block will render the block invalid. So, in a nutshell all that you can do is include/exclude certain transaction from previous blocks, or make slight cosmetic changes (like ECDSA signature malleability).


---

