The seven of Information Digest algorithm: Application of SHA in block chain

Source: Internet
Author: User

Bitcoin's popularity in recent years has led to a study of blockchain technology. Of course, we do not discuss the blockchain technology itself, but discuss the application of the SHA algorithm in the blockchain. We have already explained the SHA Series algorithm, and we will not repeat it here.

1 , in the block chain SHA

In the blockchain, Sha was used, but where did the SHA algorithm go? In order to solve this problem, we must first understand the structure of the block chain. Each chunk includes a constant called magic number, the size of the chunk, the number of blocks, the amount of transactions that the chunk contains, and some or all recent new deals. The specific structure of the block is as follows:

Data item

Describe

Length

Magic No (magic number)

Always 0xd9b4bef9.

4 bytes

Blocksize (chunk size)

The byte length to the end of the chunk

4 bytes

Blockheader (District block)

Consists of 6 data items

80 bytes

Transaction counter (number of trades)

Positive integer Vi=varint

1-9 bytes

Transactions (trading)

Trade list (non-empty)

<transaction counter>-Many deals

We don't seem to find Sha's trace from the above table. In addition to the block header in the table above, the meanings of the other parts are very clear. So next we need to look at the chunk head and see what it means. In fact, in each chunk, the block header is the determinant of the entire blockchain. In that case, what does it take to get a size? Its specific structure is as follows:

Data item

Objective

Update Time

Size (bytes)

Version (versions)

Block version number

After updating the software, it specifies a new version number

4

Hashprevblock (hash of the previous chunk)

256-bit hash value for the previous chunk

When a new chunk comes in

32

Hashmerkleroot Merkle (root node hash value)

256-bit hash value based on all trades in a chunk

When accepting a transaction

32

Time (timestamp)

Current timestamp in seconds, starting from 1970-01-01 00:00 UTC to Now

Updates every few seconds

4

Bits (the hash value of the current target)

Current target hash value in compressed format

When mining difficulty is adjusted

4

Nonce (random number)

32-bit random number starting from 0

Generate hash (Increase every time hash random number is generated)

4

As shown in the table above, in the structure of the block size, we finally see the trace of Sha, is the hash value, it is calculated by the SHA algorithm.

2 , how blockchain is used SHA

In the previous section, we understood the location of Sha in the block, so how is Sha used in the blockchain? Let's take a look at the format of the front chunk header, there are two 256-bit hash values: hashprevblock (hash of the previous chunk) and Hashmerkleroot Merkle (chunk hash value), how do I get the two hash values?

The hash value of the previous block is calculated using the following formula: The hash value of the previous block is =sha256 (The block header of the previous block), and the hash value of the block body is calculated as: the hash value of the chunk body is =sha256 (block body of this block).

From the above two formulas we can easily infer that a chunk is not only related to its own content, but also to the content of the previous block. In other words, the content of a block not only affects itself, but also affects subsequent chunks. Any change in content will result in the change in the hash value of this chunk, leading to changes in the chunk head. And the change of block head will cause the change of block head of subsequent chunks to have a ripple effect.

3 , why the blockchain is used SHA

We've already explained how hash values are used in the blockchain, so let's say why Sha is used. This is directly related to the technical requirements of the blockchain and the characteristics of the SHA algorithm.

First, the blockchain itself ensures that each piece is unique. Because there must be no two of the same data, and SHA256 's very low collision probability just guarantees this. The hash of each chunk is unique and can be used to identify the chunk.

Secondly, the blockchain guarantees the security of each record, and the SHA algorithm can guarantee a very different result, even if there are only insignificant differences. The structure of the blockchain, when any block changes, will affect all subsequent chunks, so if you want to modify the contents of a block, you must modify the contents of all blocks, which is almost impossible, thus ensuring the security of the block data.

Furthermore, the result length of SHA is fixed and not related to the content of the original text, so that the format of each chunk in the blockchain and the size of the chunk header used to identify the chunk are exactly the same, but they are never duplicated.

To sum up, the use of the block chain SHA algorithm is a technical necessity, even if not the use of SHA will be similar to SHA with other algorithms.

Welcome attention:

The seven of Information Digest algorithm: Application of SHA in block chain

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.