Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze and implement SHA256 algorithm in Hash algorithm

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to analyze and implement the SHA256 algorithm in the hash algorithm, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

In the eyes of many technicians, blockchain is not a new technology, but the combined use of computer technology over the past many years. In the application of all aspects of technology, the encryption algorithm based on cryptography can be said to be the foundation of the performance of various characteristics of the block chain, once the currently used encryption algorithm has been proved to be cracked, then the existing block chain technology is likely to collapse. What this article wants to talk about is the most widely used encryption algorithm in the block chain: SHA256.

SHA is a family of cryptographic hash functions, which stands for Secure Hash Algorithm. Designed by the National Security Agency (NSA) and released by the National Institute of Standards and Technology (NIST). The protagonist of this article, SHA256 algorithm, is a member of this family. Prior to this, SHA0,SHA1 has been proved to be crackable, but SHA2 and SHA3 have not yet been proved to be crackable. It is extended here that in the definition of cryptography, the so-called breakable means that the computational complexity is less than that required by brute force cracking. The summary length of this algorithm is taken by 256in SHA256. The following will talk about the implementation principle of SHA256 in detail.

The design idea of SHA256 algorithm mainly depends on the characteristics of an excellent HASH hash algorithm: any small input may have a great impact on the output, and the HASH algorithm has a very low collision probability.

Constant definition:

The SHA256 algorithm firstly specifies 8 hash initial values and 64 hash constants. The initial values of 8 hashes take the pre-32bit of the decimal part of the square root of the first 8 primes in the natural number (2, 3, 5, 7, 11, 13, 17, 19). For example:

Take the decimal part: 0.414213562373095048 (in hexadecimal) = 6\ times16 ^ {- 1} + a\ times16 ^ {- 2} + 0\ times16 ^ {- 3} +.

So the first of the eight hashes can be expressed as: H0: = 0X6a09e667. The final 8 hash initial values are:

H0 = 0x6a09e667 H1 = 0xbb67ae85 H2 = 0x3c6ef372 H3 = 0xa54ff53a H4 = 0x510e527f H5 = 0x9b05688c H6 = 0x1f83d9ab H7 = 0x5be0cd19

Looking at the definition of the previous 8 hash initial values, 64 hash constants are defined as the pre-32bit values of the decimal part of the cube root of the first 64 primes in the natural number. It will not be posted here, just understand the definition. Careful readers should be able to find that the total length of the eight initial hashes, each 32 bits, corresponds to the length of the final result of the SHA256 algorithm.

Logical function definition:

The SHA256 algorithm defines 6 logic functions. The functions of these six logic functions are simply understood here, and the following code section will see how to use them.

Note: AND stands for "and" operation, NOT for "OR" operation, and XOR for "XOR" operation. ROTR ^ 2 (X) means to cycle two digits to the right of X, and SHR ^ 3 (X) means to move three digits to the right of X. The input to the function is the word 32bit.

CH (x, y, z) = (x AND y) XOR ((NOT x) AND z) MAJ (x, y) Z) = (x AND y) XOR (x AND z) XOR (y AND z) BSIG0 (x) = Rotr ^ 2 (x) XOR Rotr ^ 13 (x) XOR Rotr ^ 22 (x) BSIG1 (x) = Rotr ^ 6 (x) XOR Rotr ^ 11 (x) XOR Rotr ^ 25 (x) SSIG0 (x) = Rotr ^ 7 (x) XOR ROR ^ 18 (x) XOR RHR ^ 3 (x) SSIG1 (x) = ROTR ^ 17 (x) XOR TRO ^ 19 (x) XOR HR10 (x) algorithm process:

The purpose of the complement is to make the message length satisfy the formula (original message length + 1cm K) mod 512 = 448. Why 448? the purpose is to leave a 64-bit length to represent the length of the message. The message length supported by SHA256 is no more than 2 to the power of 64. 1 represents a bit to be filled in the first step of the complement, and K represents the number of zeros to be filled. The first step in filling is to add a 1 at the end regardless of the length of the message (even if the demoulding for 512 equals 448).

Suppose the original message goes like this: 01111101 01110000 11110000 00000001

Message after the first step of filling: 01111101 01110000 11110000 00000001 1

According to the above explanation, it is easy to calculate the need to add 415 zeros.

Complement length:

The supplementary length is generally a fixed 64bit, which is used to represent the initial length of the message. The length of the input above is 32 bits, and the conversion to hexadecimal is 100000. So 100000 will be patched after the above message, and less than 64 bits will be filled with 0.

Calculation process:

The overall idea: divide the message into N parts of large and small 512bit, take the data of the first data block, and divide it into 16 32bit arrays. The first eight hash initial values H (0) get H (1) through the first message block data operation, and the second message block data operation get H (2), a loop until H (N), which is the final information summary.

The detailed process is as follows:

1) first use a 256-bit cache to store the intermediate and final results of the hash function. We mentioned earlier that there are eight initial values defined in SHA256. These eight initial values are represented by a _

A=0x6a09e667, b=0xbb67ae85

C=0x3c6ef372, d=0xa54ff53a

E=0x510e527f, f=0x9b05688

C, g=0x1f83d9ab, h=0x5be0cd19 .

2) split the supplemented message into N pieces of 512bit data. First of all, there is a large loop outside the calculation function, such as: For I = 1 to N

3) in each cycle, we need to assign a value to the result where the input t of the function W (t) is 0 to 63, that is, W (0), W (1), W (2). W (63). Where the values of W (0) to W (15) are the 16 values of the current fast message being split (each block mentioned earlier is divided into 16 32bit data). Then the values from W (16) to W (63) are generated depending on the first 16 values. The specific algorithm is as follows:

For t = 0 to 15Wt = M (I) t / / assign the first 16 values of W (t) For t = 16 to 63Wt = SSIG1 (W (tmur2)) + W (tMui 7) + SSIG0 (w (tMube 15)) + W (tMAE 16) / / calculate the latter values using the first 16 values of W (t)

4) 64 values of W (t) obtained in the third step are used for 64 cycles to disrupt the original order. There are some intermediate variables in the process, but the purpose is to re-assign the value to arecrum, brecec, pr, d, e, and f, g, h. Code process:

For t = 0 to 63 T1 = h + BSIG1 (e) + CH (eMagnefMagneg) + Kt + Wt / / the Wt here is the 64 hash constants defined at the beginning of the article. T2 = BSIG0 (a) + MAJ (a) h = gg = ff = ee = d + T1d = cc = bb = aa = T1 + T2

As you can see, using the logic function introduced earlier and the calculation result of the third step, we recalculate the value of a recalculated brecedence, crecedence, drecedence, e-recalculating, freguency, grecoh.

5) the final step is to get the SHA256 result of this block based on the eight variables re-assigned in step 4.

The values of H (I) 0 to H (I) 7 are calculated from the values of H (imur1) 0 to H (imur1) 7 in the previous step.

H (I) 0 = a + H (iMur1) 0 / / an is a value of 32bit H (iMub 1) 0 is also a 32bit value H (I) 1 = b + H (iMui 1) 1H (I) 2 = c + H (iMui 1) 2H (I) 3 = d + H (iMui 1) 3H (I) 4 = e + H (iMub 1) 4H (I) 5 = f + H (iMui 1) 5H (I) 6 = g + H (iMui 1) 6H (I) 7 = h + H (iMut 1) 7

At this point, the first cycle is over, remember the previous mention of "there is a big cycle outside, such as: For I = 1 to N"? In SHA256, the longer the message, the more times there are loops. The final result of SHA256 algorithm is obtained by concatenating eight 32-bit numbers of H (N) _ 0 and H (N) _ 7, which is a 256-bit message digest.

In Bitcoin, SHA256 is used in the generation of wallet addresses, and it is also the main means to achieve pow consensus mechanism. At present, the 256 power of 2 is similar to the 77 power of 10. How big is the power of 77 of 10? So far, the number of atoms in the observable universe is about 10 to the 80th power, and it is almost impossible for this order of magnitude hashing algorithm to attack through collisions. This is also one of the reasons why SHA256 algorithm is widely used at present.

After reading the above, have you mastered how to analyze and implement the SHA256 algorithm in the hash algorithm? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report