Introduction to Hash Algorithm

First, the Hash algorithm figure

As you can see, when generating the Bitcoin address (mentioned in chapter 4 of "Proficient Bitcoin") and the unique identifier of the generated block ("proficient in Bitcoin" mentioned in Chapter 7): Block Hash value (ie mining The process) uses Hash algorithm, especially SHA256 algorithm. The Bitcoin system itself is a derivative of the encryption algorithm.

Second, SHA256 algorithm process

This article briefly describes the process of the SHA256 algorithm. The next article will briefly explain its theoretical basis.

Input information x

SHA256 requires that the length of the packet be no more than 2^64 bits.

2. Complement

Because the packet encryption method is used, the length of each group is 512 bits, and the original information x is not necessarily a multiple of 512, that is, it may not be exactly divided into 512 bits per group. At this time, you need to fill in.

After x, make a 1 and then make a number of 0, until the total length (x and the number of bits) modulo 512 is 448

3. Make up the length

Why the second step does not directly fill the total length to a multiple of 512, because it will be followed by 64 bits, recording the number of bits of the original information x. (64 + 448) % 512 = 0. It can be seen that only 64 bits represent the number of bits of the original information x, so the maximum length of x is 2^64.

4. Calculate the hash value

The previous message has been filled up to a multiple of 512 and the total length has become 512 * N. The basic idea of ​​calculating the hash value is: first divide the processed message into N 512-bit data blocks: M[1], M[2],..., M[N], and then process one by one: initial value of hash H[0] obtains H[1] through the first data block M[1], H[1] obtains H[2],... through the second data block M[2], and processes in sequence to obtain H[ N]. Each hash value, such as H[0], consists of 8 32 bits, which is also referred to as a word. That is, each hash value consists of 8 words. Finally, connect the 8 words of H[N] to the final value of 256 bits (8 * 32 bits). As for why we can do this, we will introduce it again in the next article. Let's look at the process first.

1) Hash initial value H[0]

The hash initial value H[0] (a total of 8 32 bits) used in the SHA256 algorithm is:

H[0][0] = 0x6A09E667

H[0][1] = 0xBB67AE85

H[0][2] = 0x3C6EF372

H[0][3] = 0xA54FF53A

H[0][4] = 0x510E527F

H[0][5] = 0x9B05688C

H[0][6] = 0x1F83D9AB

H[0][7] = 0x5BE0CD19

0x represents hexadecimal, and one digit in hexadecimal represents 4 bits.

Note: These initial values ​​are taken from the first 32 bits of the fractional part of the square root of the first eight prime numbers 2, 3, 5, 7 and 11 in the natural number.

2) Cycle calculation

# M's subscript starts from 1, M is M[1], ..., M[N]

For i in range(1, N + 1):

# The first step is to generate 64 32bits based on 16 32bit original messages.

W = [0] * 64

For t in range(0, 64):

If t < 16:

W[t] = M[i][t]

Else:

W[t] = SSIG1(W[t - 2]) + W[t-7] + SSIG0(W[t-15]) + W[t-16]

# In the second step, the last hash value, that is, 8 32 bits, is assigned to 8 temporary variables, and these 8 temporary variables are used to calculate

# The original hash value still needs to be used at the end of the current round

a = H[i - 1][0]

b = H[i - 1][1]

c = H[i - 1][2]

d = H[i - 1][3]

e = H[i - 1][4]

f = H[i - 1][5]

g = H[i - 1][6]

h = H[i - 1][7]

# The third step, perform the hash calculation, the inner loop is 64 rounds, uses the above 64 W, and defines 64 K in advance

For t in range(0, 64):

T1 = h + BSIG1(e) + CH(e, f, g) + K[t] + W[t]

T2 = BSIG0(a) + MAJ(a,b,c)

h = g

g = f

f = e

e = d + t1

d = c

c = b

b = a

a = t1 + t2

# 4th step, sum the newly calculated temporary value and the previous hash value of the 64 inner loops as the final hash value of the current round.

H[i][0] = a + H[i - 1][0]

H[i][1] = b + H[i - 1][1]

H[i][2] = c + H[i - 1][2]

H[i][3] = d + H[i - 1][3]

H[i][4] = e + H[i - 1][4]

H[i][5] = f + H[i - 1][5]

H[i][6] = g + H[i - 1][6]

H[i][7] = h + H[i - 1][7]

# 8 32-bit return of H[N] finally, that is SHA256 result of the message

3) Description of functions and constants

(1) The 64 constants K are:

K = [

0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,

0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,

0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,

0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,

0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,

0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,

0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,

0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]

Note: The values ​​of these constants are taken from the first 32 bits from the fractional part of the cube root of the first 64 prime numbers in the natural number.

(2) The function used is:

# This section of code does not use python syntax

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 ROTR^18(x) XOR SHR^3(x)

SSIG1(x) = ROTR^17(x) XOR ROTR^19(x) XOR SHR^10(x)

Among them, XOR is XOR, except with or without, ROTR^n is rotated right by n bits, SHR^n is right by n bits.

Third, summary

The above is the SHA256 calculation process.

Infrared Thermometer

With more than 15+ yrs rich MFG experience, you can definitely trust in and cooperate with.
Provide you with the supply of Personal Protective Equipment. to help you safely get back to your daily routine.
Our products include pulse Oximeter Finger, Forehead Thermometer, Automatic foam soap dispenser, etc.
Our strict quality control protocol thoroughly vets every aspect of production, storage, and shipments all the way way to our end customers.

infrared thermometers wholesale, forehead thermometer wholesale,wholesale thermometer suppliers

TOPNOTCH INTERNATIONAL GROUP LIMITED , https://www.itopnoobluetoothes.com