The whole flow of MD5 hash algorithm and the realization of the "__" algorithm

Source: Internet
Author: User
Tags md5 md5 hash

Big homework ready to do MD5 hardware implementation, but only on the information on the Internet to write a MD5 program is quite time-consuming

Reprint please indicate http://blog.csdn.net/boksic if you have questions welcome message

The entire process description of the MD5 can refer to the RFC1321, which lists only the pseudocode for the hash algorithm (which is preceded by a bit padding)

   /* Process each 16-word block.
     * * For i = 0 to n/16-1 do/* Copy block I into X. */For j = 0 to-do Set x[j] to m[i*16+j]. End/* of Loop on J * *///Save A as AA, B as BB, C as CC, and D as DD. */AA = A BB = B CC = C D D = d/* Round 1. */* Let [ABCD K s i] denote the operation a = B + ((A + F (b,c,d) + x[k] + t[i)) <<< s). */* Do the following operations.  * [ABCD 0 7 1] [DABC 1 2] [Cdab 2 3] [BCDA 3 4] [ABCD 4 7 5] [DABC 5 6] [Cdab 6 7] [BCDA 7 8] [ABCD 8 7 9] [DABC 9] [Cdab] [BCDA] [ABCD 7] [D ABC] [Cdab] [BCDA]/* Round 2. */* Let [ABCD K s i] denote the operation a = B + ((A + G (b,c,d) + x[k] + t[i)) <<< s). */* Do the following operations.
* [ABCD 1 5] [DABC 6 9] [Cdab] [BCDA 0 20 20]     [ABCD 5 5 21]  [DABC 10 9 22]  [Cdab 15 14 23]
     [BCDA 4 20 24]  [ABCD 9 5 25]  [DABC 14 9 26]  [Cdab 3 14 27]
     [BCDA 8 20 28]  [ABCD 13 5 29]  [DABC 2 9 30]  [Cdab 7 14 31] [BCDA]/* Round 3. */* Let [ABCD K s T] denote the operation a = B + ((A + H (b,c,d) + x[k] + t[i)) <<< s). */* Do the following operations.  * [ABCD 5 4] [DABC 8] [CDAB] [BCDA] [ABCD 1 4] [DABC 4] [Cdab 7 4 [BCDA] [ABCD] [dabc 0] [Cdab 3] [BCDA 6] [ABCD 9 4] [DA BC] [Cdab] [BCDA 2]/* Round 4. */* Let [ABCD K s T] denote the operation a = B + ((A + I (b,c,d) + x[k] + t[i]) <<< s). */* Do the following operations.  * [ABCD 0 6] [dabc 7] [Cdab] [BCDA 5] [ABCD 6] [DABC 3] [Cdab 10 [BCDA 1 21[ABCD 8 6] [DABC] [Cdab 6] [BCDA] [ABCD 4 6 m] [DABC] [Cdab- 2] [BCDA 9]/* Then perform the following additions. (That's increment each of the ' four registers by the ' value it had before this ' is started.)

 /a = a + AA B = b + BB c = c + CC d = d + DD End/* of loop on I * *

Although Python uses built-in functions to produce results in one sentence, it is better to understand that the algorithm writes out the code or goes through the process.

The first is the number of constants and functions involved in the definition


From math import * in_str= "a" F = lambda x, y, Z: (((x) & (y)) |
((~x) & (z)) G = lambda x, y, Z: (((x) & (z)) | ((y) & (~z))) H = lambda x, y, Z: ((x) ^ (y) ^ (z)) I = lambda x, y, Z: ((y) ^ (x) | 
(~z))) RL = Lambda X,n: (((x) << (n)) | ((x) >> (32-(n))) ff= Lambda A,b,c,d,x,s,ac:func def FF (A, B, C, D, X, S, ac): a = (A+f ((b), (c), (d)) + (x) + (a
	c) &0xffffffff; &0xffffffff)
	A = RL ((a), (s)) &0xffffffff; A = (a+b) &0xffffffff return a def GG (A, B, C, D, X, S, ac): a = (A+g ((b), (c), (d)) + (x) + (AC) &0xffffffff) &am
	P;0XFFFFFFFF;
	A = RL ((a), (s)) &0xffffffff; A = (a+b) &0xffffffff return a def HH (A, B, C, D, X, S, ac): a = (A+h ((b), (c), (d)) + (x) + (AC) &0xffffffff) &am
	P;0XFFFFFFFF;
	A = RL ((a), (s)) &0xffffffff; A = (a+b) &0xffffffff return a def II (A, B, C, D, X, S, ac): a = (A+i ((b), (c), (d)) + (x) + (AC) &0xffffffff) &am
	P;0XFFFFFFFF;
	A = RL ((a), (s)) &0xffffffff; A = (a+b) &0xffffffff reTurn a func=[ff,gg,hh,ii] s_data=[[7,12,17,22],[5,9,14,20],[4,11,16,23],[6,10,15,21]] buf=[0x67452301,0xefcdab89, 0x98badcfe,0x10325476] in_data= '. Join ("%.2x"%ord (i) for I in In_str)

Ff,gg,hh,ii is a few of the non-linear functions used,

S11-S44 is the shift constant, In_data is the constant used in each step of the 64 step.

and the initial value buf the whole process is to deal with this buf

For the input fill part because the code is very simple to write, the function is too limited, so it is not posted


Core algorithm Section

For I in range (4): for
	J in range:
		inbufn= (i==0) *j+ (i==1) * (1+5*j)%16+ (i==2) * (5+3*j)%16+ (i==3) * (7*J)%16
		Temp_buf[-j%4]=func[i] (temp_buf[-j%4],temp_buf[(1-j)%4],temp_buf[(2-j)%4],temp_buf[(3-j)%4],IN_BUF[INBUFN ],S_DATA[I][J%4],T_DATA[I*16+J])
		print hex (temp_buf[0]), Hex (temp_buf[1), Hex (temp_buf[2)), Hex (temp_buf[3))
Note that the Inbuf serial number INBUFN the relationship with the wheel

Test

Hash for "a"

Input string is a Hex format:0x61 pad length is padding in_data:618000000000000000000000000000000000000000000000 0000000000 0000000000000000000000000000000000000000000000000000000800000000000000 In_buf is:
00008061000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000800000000 t_data= 0xd76aa478l 0xe8c7b756L 0x242070db 0xc1bdceeeL 0XF57C0FAFL 0x4787c62a 0xa8304613l 0xfd469501l 0x698098d8 0x8b44f7afl 0xffff5bb1l 0x895cd7beL 0x6b901122 0xfd987193L 0xa 679438eL 0x49b40821 0xf61e2562l 0xc040b340l 0x265e5a51 0xe9b6c7aal 0xd62f105dl 0x2441453 0xd8a1e681l 0xe7d3fbc8L 0x21e1c  De6 0xc33707d6l 0xf4d50d87l 0x455a14ed 0xa9e3e905l 0xfcefa3f8l 0x676f02d9 0x8d2a4c8al 0xfffa3942L 0x8771f681L 0x6d9d6122 0xfde5380cl 0xa4beea44l 0x4bdecfa9 0xf6bb4b60l 0xbebfbc70l 0x289b7ec6 0xeaa127fal 0xd4ef3085L 0x4881d05 0xd9d4d039L 0xe 6db99e5l 0x1fa27cf8 0xc4ac5665l 0xf4292244l 0x432aff97 0xab9423a7l 0xfc93a039l 0x655b59c3 0x8f0ccc92L 0xffeff47dL 0x85845dd1l 0x6fa87e4f 0xfe2ce6e0l 0xa3014314l 0x4e0811a1 0xf7537e82l 0xbd3af235l 0X2AD7D2BB 0xeb86d391L A b c D =========round 1============== 0xa56017f4l 0xefcdab89l 0x98badcfel 0x10325476 0xa56017f4l 0x efcdab89l 0x98badcfel 0xf2d58361l 0xa56017f4l 0xefcdab89l 0xe65857a7l 0xf2d58361l 0xa56017f4L 0x607d9686L 0xe65857a7L 0xf2d58361l 0x3a9d5bccl 0x607d9686l 0xe65857a7l 0xf2d58361l 0x3a9d5bccl 0x607d9686l 0xe65857a7L 0xe0a07db7L 0x3a9d5bccL 0x607d9686l 0xd31ddc83l 0xe0a07db7l 0x3a9d5bccl 0xa8af6da5l 0xd31ddc83l 0xe0a07db7l 0xbe580957L 0xa8af6da5L 0xd31ddc83L 0xe0a07db7l 0xbe580957l 0xa8af6da5l 0xd31ddc83l 0xf386bea6l 0xbe580957l 0xa8af6da5l 0xf5fdd933L 0xf386bea6L 0xbe580957L 0x68493d6al 0xf5fdd933l 0xf386bea6l 0x44244cf8l 0x68493d6al 0xf5fdd933l 0xf386bea6l 0x44244cf8L 0x68493d6aL 0xf5fdd933L 0xd0fe9b27l 0x44244cf8l 0x68493d6al 0x6360a45fl 0xd0fe9b27l 0x44244cf8l 0xf01e3ce2l 0x6360a45fL 0xd0fe9b27L ========= Round 2============== 0x9c341767l 0xf01e3ce2l 0x6360a45fl 0xd0fe9b27l 0x9c341767l 0xf01e3ce2l 0x6360a45fl 0x970ab3a9l 0x9c341767l 0xf01e3ce2L 0xe39ffd23L 0x970ab3a9l 0x9c341767l 0x8d25cc66l 0xe39ffd23l 0x970ab3a9l 0x8c444930l 0x8d25cc66l 0xe39ffd23L 0x970ab3a9L 0x8c444930L 0x8d25cc66l 0xe39ffd23l 0x7267097al 0x8c444930l 0x8d25cc66l 0x2dacb8a3l 0x7267097al 0x8c444930L 0x373beab0L 0x2dacb8a3L 0x7267097al 0xf175e3adl 0x373beab0l 0x2dacb8a3l 0x7267097al 0xf175e3adl 0x373beab0l 0x2dacb8a3L 0x9d5df67eL 0xf175e3adL 0x373beab0l 0x87b7f475l 0x9d5df67el 0xf175e3adl 0xc8f891b4l 0x87b7f475l 0x9d5df67el 0x93842e98L 0xc8f891b4L 0x87b7f475L 0x9d5df67el 0x93842e98l 0xc8f891b4l 0x87b7f475l 0xc7043b64l 0x93842e98l 0xc8f891b4l 0x94a2ebeeL 0xc7043b64L 0x93842e98L
0X3745961FL 0x94a2ebeel 0xc7043b64l =========round 3============== 0xbd607d1el 0x3745961fl 0x94a2ebeeL 0xc7043b64L 0xbd607d1el 0x3745961fl 0x94a2ebeel 0xa6f72085l 0xbd607d1el 0x3745961fl 0xbf8b4f98l 0xa6f72085L 0xbd607d1eL 0xdaf7f308L 0xbf8b4f98l 0xa6f72085l 0x35a82a7al 0xdaf7f308l 0xbf8b4f98l 0xa6f72085l 0x35a82a7al 0xdaf7f308l 0xbf8b4f98l 0x89e0ec97l 0x35a82a7al 0xdaf7f308l 0x5abe099cL 0x89e0ec97L 0x35a82a7al 0xcf7e60dbl 0x5abe099cl 0x89e0ec97l 0x75c151e2l 0xcf7e60dbl 0x5abe099cl 0x89e0ec97L 0x75c151e2L 0xcf7e60dbL
0X5ABE099CL 0x942e0c86l 0x75c151e2l 0xcf7e60dbl 0xc0e6ac4l 0x942e0c86l 0x75c151e2l 0xcc6f5e9eL 0xc0e6ac4L 0x942e0c86L 0xac50e18l 0xcc6f5e9el 0xc0e6ac4l 0x942e0c86l 0xac50e18l 0xcc6f5e9el 0xc0e6ac4l 0x79ca7845l 0xac50e18L 0xcc6f5e9eL 0x8a4a6356l 0x79ca7845l 0xac50e18l 0x918f93bbl 0x8a4a6356l 0x79ca7845l =========round 4============== 0xcab8fe42L 0X918F93BBL 0x8a4a6356l 0x79ca7845l 0xcab8fe42l 0x918f93bbl 0x8a4a6356l 0x6a4daeeel 0xcab8fe42L 0x918f93bbL 0x36269c3fL 0x6a4daeeel 0xcab8fe42l 0x1ee405ebl 0x36269c3fl 0x6a4daeeel 0x982c7861l 0x1ee405ebl 0x36269c3fL 0x6a4daeeeL 0x982c7861L 0x1ee405ebl 0x36269c3fl 0x6812a362l 0x982c7861l 0x1ee405ebl 0x71fc7709l 0x6812a362l 0x982c7861L 0x893501c0L 0x71fc7709L 0x6812a362l 0XFEBD62FDL 0x893501c0l 0x71fc7709l 0x6812a362L 0XFEBD62FDL 0x893501c0l 0x71fc7709l 0x28936a74l 0xfebd62fdl 0x893501c0l 0x53e33526l 0x28936a74L 0xfebd62fdL 0xaa4d8ae3l 0x53e33526l 0x28936a74l 0x52309e0bl 0xaa4d8ae3l 0x53e33526l 0x28936a74l 0x52309e0bL 0xaa4d8ae3L 0x53e33526L 0X50F422F3L 0X52309E0BL 0xaa4d8ae3l 0x49dee633l 0x50f422f3l 0x52309e0bl 0xb8e94637l 0x49dee633L 0x50f422f3L =========
 Result is:0cc175b9c0f1b6a831c399e269772661============
0cc175b9c0f1b6a831c399e269772661 the results are correct.
So, the next write Verilog code can have a process reference


Combined results of additional Verilog


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.