Hash algorithm learning record MD5 as an example

The hash algorithm is a bit difficult. I only understand 70%. But also for himself to write again one more time to practice understanding
learning from this Gangster following article
click into the
hashing algorithm (which is an irreversible algorithm, it is often used to verify data integrity)
HASH algorithm is a message digest Algorithm, not an encryption algorithm, has certain irreversibility. The hash algorithm is to convert the target text into an irreversible message digest with the same length.
The hash algorithm is irreversible. Here, irreversible is understood as there is no way to convert to the original target text, and given the hash result R, even if you know that the hash result of a piece of text S is R, you cannot assert that the original target text is S.
Encryption is different. Given the encrypted ciphertext R, there is a way to convert the R determined to the plaintext S before encryption.
(Hint: If the protected data is only used for comparison and verification, and you do n’t need to restore it to plain text later, use hash; if the protected data needs to be restored to plain text later, you need to use encryption.)
Below we use hash An example of an algorithm is MD5.
The MD5 algorithm is like a function, any binary string can be used as an argument to enter this "function", and then a binary string fixed at 128 bits will come out.
The algorithm starts:
Binary bit supplement: supplement
the binary code of the encrypted content. If the length of the input information (bit) surplus to 512 is not equal to 448, it needs to be filled so that the result of 512 is 448. The method of filling is to fill a 1 followed by all 0s. After filling, the length of the information is N 512 + 448.
Record the length of the information: use 64 bits to store the length of the information before filling.
The 64 bits storing the length of the padding information are added after the result of the first step, so that the information length becomes N
512 + 448 + 64 = (N + 1) * 512 bits
Load the standard magic number (four integers): the
standard magic number (physical order) is (A = (01234567) 16, B = (89ABCDEF) 16, C = (FEDCBA98) 16, D = (76543210) 16) . The definition in the program is: (A = 0X67452301L, B = 0XEFCDAB89L, C = 0X98BADCFEL, D = 0X10325476L)
Four rounds of loop operation: the number of loops is the number of groups (N + 1),
each 512 bytes is subdivided into 16 groups, each group is 64 bits (8 bytes) to
see the following four functions, (& is and, | is or, ~ is not, ^ is XOR)
F (X, Y, Z) = (X & Y) | ((~ X) & Z)
G (X, Y, Z) = (X & Z) | (Y & (~ Z))
H (X, Y, Z) = X Y Z
I (X, Y, Z) = Y ^ (X | (~ Z))
Let Mj denote the jth subgroup of the message (from 0 to 15)
FF (a, b, c, d, Mj, s, ti) denote a = b + ((a + F ( b, c, d) + Mj + ti) <<< s)
GG (a, b, c, d, Mj, s, ti) means a = b + ((a + G (b, c, d) + Mj + ti) <<< s)
HH (a, b, c, d, Mj, s, ti) means a = b + ((a + H (b, c, d) + Mj + ti) <<< s)
II (a, b, c, d, Mj, s, ti) means a = b + ((a + I (b, c, d) + Mj + ti) <<< s)
and then performs four rounds of calculations The
first round
a = FF (a, b, c, d, M0,7,0xd76aa478)
b=FF(d,a,b,c,M1,12,0xe8c7b756)
c=FF(c,d,a,b,M2,17,0x242070db)
d=FF(b,c,d,a,M3,22,0xc1bdceee)
a=FF(a,b,c,d,M4,7,0xf57c0faf)
b=FF(d,a,b,c,M5,12,0x4787c62a)
c=FF(c,d,a,b,M6,17,0xa8304613)
d=FF(b,c,d,a,M7,22,0xfd469501)
a=FF(a,b,c,d,M8,7,0x698098d8)
b=FF(d,a,b,c,M9,12,0x8b44f7af)
c=FF(c,d,a,b,M10,17,0xffff5bb1)
d=FF(b,c,d,a,M11,22,0x895cd7be)
a=FF(a,b,c,d,M12,7,0x6b901122)
b=FF(d,a,b,c,M13,12,0xfd987193)
c=FF(c,d,a,b,M14,17,0xa679438e)
d=FF(b,c,d,a,M15,22,0x49b40821)

第二轮
a=GG(a,b,c,d,M1,5,0xf61e2562)
b=GG(d,a,b,c,M6,9,0xc040b340)
c=GG(c,d,a,b,M11,14,0x265e5a51)
d=GG(b,c,d,a,M0,20,0xe9b6c7aa)
a=GG(a,b,c,d,M5,5,0xd62f105d)
b=GG(d,a,b,c,M10,9,0x02441453)
c=GG(c,d,a,b,M15,14,0xd8a1e681)
d=GG(b,c,d,a,M4,20,0xe7d3fbc8)
a=GG(a,b,c,d,M9,5,0x21e1cde6)
b=GG(d,a,b,c,M14,9,0xc33707d6)
c=GG(c,d,a,b,M3,14,0xf4d50d87)
d=GG(b,c,d,a,M8,20,0x455a14ed)
a=GG(a,b,c,d,M13,5,0xa9e3e905)
b=GG(d,a,b,c,M2,9,0xfcefa3f8)
c=GG(c,d,a,b,M7,14,0x676f02d9)
d=GG(b,c,d,a,M12,20,0x8d2a4c8a)

第三轮
a=HH(a,b,c,d,M5,4,0xfffa3942)
b=HH(d,a,b,c,M8,11,0x8771f681)
c=HH(c,d,a,b,M11,16,0x6d9d6122)
d=HH(b,c,d,a,M14,23,0xfde5380c)
a=HH(a,b,c,d,M1,4,0xa4beea44)
b=HH(d,a,b,c,M4,11,0x4bdecfa9)
c=HH(c,d,a,b,M7,16,0xf6bb4b60)
d=HH(b,c,d,a,M10,23,0xbebfbc70)
a=HH(a,b,c,d,M13,4,0x289b7ec6)
b=HH(d,a,b,c,M0,11,0xeaa127fa)
c=HH(c,d,a,b,M3,16,0xd4ef3085)
d=HH(b,c,d,a,M6,23,0x04881d05)
a=HH(a,b,c,d,M9,4,0xd9d4d039)
b=HH(d,a,b,c,M12,11,0xe6db99e5)
c=HH(c,d,a,b,M15,16,0x1fa27cf8)
d=HH(b,c,d,a,M2,23,0xc4ac5665)

Fourth round
a = II (a, b, c, d, M0,6,0xf4292244)
b = II (d, a, b, c, M7,10,0x432aff97)
c = II (c, d, a, b , M14,15,0xab9423a7)
d = II (b, c, d, a, M5,21,0xfc93a039)
a = II (a, b, c, d, M12,6,0x655b59c3)
b = II (d, a , b, c, M3,10,0x8f0ccc92)
c = II (c, d, a, b, M10,15,0xffeff47d)
d = II (b, c, d, a, M1,21,0x85845dd1)
a = II (a, b, c, d, M8,6,0x6fa87e4f)
b = II (d, a, b, c, M15,10,0xfe2ce6e0)
c = II (c, d, a, b, M6,15,0xa3014314 )
d = II (b, c, d, a, M13,21,0x4e0811a1)
a = II (a, b, c, d, M4,6,0xf7537e82)
b = II (d, a, b, c, M11 , 10,0xbd3af235)
c = II (c, d, a, b, M2,15,0x2ad7d2bb)
d = II (b, c, d, a, M9,21,0xeb86d391) After
each round of circulation, A, B , C, D add a, b, c, d, and then enter the next cycle.

Thanks for the following article! ! !

https://blog.csdn.net/u012611878/article/details/54000607?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

Published 9 original articles · won 7 · visited 1767

Guess you like

Origin blog.csdn.net/weixin_44906810/article/details/105168076