Skip to content

Commit 563bf52

Browse files
author
linzhijun
committed
添加 md5 代码实现
1 parent 825b842 commit 563bf52

File tree

1 file changed

+290
-0
lines changed
  • csharp/ToolGood.Algorithm2/Internals

1 file changed

+290
-0
lines changed
Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
//using System;
2+
//using System.Collections.Generic;
3+
//using System.Text;
4+
5+
//namespace ToolGood.Algorithm.Internals
6+
//{
7+
// using System;
8+
// using System.Collections;
9+
// using System.IO;
10+
11+
// public class MD5
12+
// {
13+
// //static state variables
14+
// private static UInt32 A;
15+
// private static UInt32 B;
16+
// private static UInt32 C;
17+
// private static UInt32 D;
18+
19+
// //number of bits to rotate in tranforming
20+
// private const int S11 = 7;
21+
// private const int S12 = 12;
22+
// private const int S13 = 17;
23+
// private const int S14 = 22;
24+
// private const int S21 = 5;
25+
// private const int S22 = 9;
26+
// private const int S23 = 14;
27+
// private const int S24 = 20;
28+
// private const int S31 = 4;
29+
// private const int S32 = 11;
30+
// private const int S33 = 16;
31+
// private const int S34 = 23;
32+
// private const int S41 = 6;
33+
// private const int S42 = 10;
34+
// private const int S43 = 15;
35+
// private const int S44 = 21;
36+
37+
38+
// /* F, G, H and I are basic MD5 functions.
39+
// * 四个非线性函数:
40+
// *
41+
// * F(X,Y,Z) =(X&Y)|((~X)&Z)
42+
// * G(X,Y,Z) =(X&Z)|(Y&(~Z))
43+
// * H(X,Y,Z) =X^Y^Z
44+
// * I(X,Y,Z)=Y^(X|(~Z))
45+
// *
46+
// * (&与,|或,~非,^异或)
47+
// */
48+
// private static UInt32 F(UInt32 x, UInt32 y, UInt32 z)
49+
// {
50+
// return (x & y) | ((~x) & z);
51+
// }
52+
// private static UInt32 G(UInt32 x, UInt32 y, UInt32 z)
53+
// {
54+
// return (x & z) | (y & (~z));
55+
// }
56+
// private static UInt32 H(UInt32 x, UInt32 y, UInt32 z)
57+
// {
58+
// return x ^ y ^ z;
59+
// }
60+
// private static UInt32 I(UInt32 x, UInt32 y, UInt32 z)
61+
// {
62+
// return y ^ (x | (~z));
63+
// }
64+
65+
// /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
66+
// * Rotation is separate from addition to prevent recomputation.
67+
// */
68+
// private static void FF(ref UInt32 a, UInt32 b, UInt32 c, UInt32 d, UInt32 mj, int s, UInt32 ti)
69+
// {
70+
// a = a + F(b, c, d) + mj + ti;
71+
// a = a << s | a >> (32 - s);
72+
// a += b;
73+
// }
74+
// private static void GG(ref UInt32 a, UInt32 b, UInt32 c, UInt32 d, UInt32 mj, int s, UInt32 ti)
75+
// {
76+
// a = a + G(b, c, d) + mj + ti;
77+
// a = a << s | a >> (32 - s);
78+
// a += b;
79+
// }
80+
// private static void HH(ref UInt32 a, UInt32 b, UInt32 c, UInt32 d, UInt32 mj, int s, UInt32 ti)
81+
// {
82+
// a = a + H(b, c, d) + mj + ti;
83+
// a = a << s | a >> (32 - s);
84+
// a += b;
85+
// }
86+
// private static void II(ref UInt32 a, UInt32 b, UInt32 c, UInt32 d, UInt32 mj, int s, UInt32 ti)
87+
// {
88+
// a = a + I(b, c, d) + mj + ti;
89+
// a = a << s | a >> (32 - s);
90+
// a += b;
91+
// }
92+
93+
// private static void MD5_Init()
94+
// {
95+
// A = 0x67452301; //in memory, this is 0x01234567
96+
// B = 0xefcdab89; //in memory, this is 0x89abcdef
97+
// C = 0x98badcfe; //in memory, this is 0xfedcba98
98+
// D = 0x10325476; //in memory, this is 0x76543210
99+
// }
100+
101+
// private static UInt32[] MD5_Append(byte[] input)
102+
// {
103+
// int zeros = 0;
104+
// int ones = 1;
105+
// int size = 0;
106+
// int n = input.Length;
107+
// int m = n % 64;
108+
// if (m < 56) {
109+
// zeros = 55 - m;
110+
// size = n - m + 64;
111+
// } else if (m == 56) {
112+
// zeros = 0;
113+
// ones = 0;
114+
// size = n + 8;
115+
// } else {
116+
// zeros = 63 - m + 56;
117+
// size = n + 64 - m + 64;
118+
// }
119+
120+
// ArrayList bs = new ArrayList(input);
121+
// if (ones == 1) {
122+
// bs.Add((byte)0x80); // 0x80 = $10000000
123+
// }
124+
// for (int i = 0; i < zeros; i++) {
125+
// bs.Add((byte)0);
126+
// }
127+
128+
// UInt64 N = (UInt64)n * 8;
129+
// byte h1 = (byte)(N & 0xFF);
130+
// byte h2 = (byte)((N >> 8) & 0xFF);
131+
// byte h3 = (byte)((N >> 16) & 0xFF);
132+
// byte h4 = (byte)((N >> 24) & 0xFF);
133+
// byte h5 = (byte)((N >> 32) & 0xFF);
134+
// byte h6 = (byte)((N >> 40) & 0xFF);
135+
// byte h7 = (byte)((N >> 48) & 0xFF);
136+
// byte h8 = (byte)(N >> 56);
137+
// bs.Add(h1);
138+
// bs.Add(h2);
139+
// bs.Add(h3);
140+
// bs.Add(h4);
141+
// bs.Add(h5);
142+
// bs.Add(h6);
143+
// bs.Add(h7);
144+
// bs.Add(h8);
145+
// byte[] ts = (byte[])bs.ToArray(typeof(byte));
146+
147+
// /* Decodes input (byte[]) into output (UInt32[]). Assumes len is
148+
// * a multiple of 4.
149+
// */
150+
// UInt32[] output = new UInt32[size / 4];
151+
// for (Int64 i = 0, j = 0; i < size; j++, i += 4) {
152+
// output[j] = (UInt32)(ts[i] | ts[i + 1] << 8 | ts[i + 2] << 16 | ts[i + 3] << 24);
153+
// }
154+
// return output;
155+
// }
156+
// private static UInt32[] MD5_Trasform(UInt32[] x)
157+
// {
158+
159+
// UInt32 a, b, c, d;
160+
161+
// for (int k = 0; k < x.Length; k += 16) {
162+
// a = A;
163+
// b = B;
164+
// c = C;
165+
// d = D;
166+
167+
// /* Round 1 */
168+
// FF(ref a, b, c, d, x[k + 0], S11, 0xd76aa478); /* 1 */
169+
// FF(ref d, a, b, c, x[k + 1], S12, 0xe8c7b756); /* 2 */
170+
// FF(ref c, d, a, b, x[k + 2], S13, 0x242070db); /* 3 */
171+
// FF(ref b, c, d, a, x[k + 3], S14, 0xc1bdceee); /* 4 */
172+
// FF(ref a, b, c, d, x[k + 4], S11, 0xf57c0faf); /* 5 */
173+
// FF(ref d, a, b, c, x[k + 5], S12, 0x4787c62a); /* 6 */
174+
// FF(ref c, d, a, b, x[k + 6], S13, 0xa8304613); /* 7 */
175+
// FF(ref b, c, d, a, x[k + 7], S14, 0xfd469501); /* 8 */
176+
// FF(ref a, b, c, d, x[k + 8], S11, 0x698098d8); /* 9 */
177+
// FF(ref d, a, b, c, x[k + 9], S12, 0x8b44f7af); /* 10 */
178+
// FF(ref c, d, a, b, x[k + 10], S13, 0xffff5bb1); /* 11 */
179+
// FF(ref b, c, d, a, x[k + 11], S14, 0x895cd7be); /* 12 */
180+
// FF(ref a, b, c, d, x[k + 12], S11, 0x6b901122); /* 13 */
181+
// FF(ref d, a, b, c, x[k + 13], S12, 0xfd987193); /* 14 */
182+
// FF(ref c, d, a, b, x[k + 14], S13, 0xa679438e); /* 15 */
183+
// FF(ref b, c, d, a, x[k + 15], S14, 0x49b40821); /* 16 */
184+
185+
// /* Round 2 */
186+
// GG(ref a, b, c, d, x[k + 1], S21, 0xf61e2562); /* 17 */
187+
// GG(ref d, a, b, c, x[k + 6], S22, 0xc040b340); /* 18 */
188+
// GG(ref c, d, a, b, x[k + 11], S23, 0x265e5a51); /* 19 */
189+
// GG(ref b, c, d, a, x[k + 0], S24, 0xe9b6c7aa); /* 20 */
190+
// GG(ref a, b, c, d, x[k + 5], S21, 0xd62f105d); /* 21 */
191+
// GG(ref d, a, b, c, x[k + 10], S22, 0x2441453); /* 22 */
192+
// GG(ref c, d, a, b, x[k + 15], S23, 0xd8a1e681); /* 23 */
193+
// GG(ref b, c, d, a, x[k + 4], S24, 0xe7d3fbc8); /* 24 */
194+
// GG(ref a, b, c, d, x[k + 9], S21, 0x21e1cde6); /* 25 */
195+
// GG(ref d, a, b, c, x[k + 14], S22, 0xc33707d6); /* 26 */
196+
// GG(ref c, d, a, b, x[k + 3], S23, 0xf4d50d87); /* 27 */
197+
// GG(ref b, c, d, a, x[k + 8], S24, 0x455a14ed); /* 28 */
198+
// GG(ref a, b, c, d, x[k + 13], S21, 0xa9e3e905); /* 29 */
199+
// GG(ref d, a, b, c, x[k + 2], S22, 0xfcefa3f8); /* 30 */
200+
// GG(ref c, d, a, b, x[k + 7], S23, 0x676f02d9); /* 31 */
201+
// GG(ref b, c, d, a, x[k + 12], S24, 0x8d2a4c8a); /* 32 */
202+
203+
// /* Round 3 */
204+
// HH(ref a, b, c, d, x[k + 5], S31, 0xfffa3942); /* 33 */
205+
// HH(ref d, a, b, c, x[k + 8], S32, 0x8771f681); /* 34 */
206+
// HH(ref c, d, a, b, x[k + 11], S33, 0x6d9d6122); /* 35 */
207+
// HH(ref b, c, d, a, x[k + 14], S34, 0xfde5380c); /* 36 */
208+
// HH(ref a, b, c, d, x[k + 1], S31, 0xa4beea44); /* 37 */
209+
// HH(ref d, a, b, c, x[k + 4], S32, 0x4bdecfa9); /* 38 */
210+
// HH(ref c, d, a, b, x[k + 7], S33, 0xf6bb4b60); /* 39 */
211+
// HH(ref b, c, d, a, x[k + 10], S34, 0xbebfbc70); /* 40 */
212+
// HH(ref a, b, c, d, x[k + 13], S31, 0x289b7ec6); /* 41 */
213+
// HH(ref d, a, b, c, x[k + 0], S32, 0xeaa127fa); /* 42 */
214+
// HH(ref c, d, a, b, x[k + 3], S33, 0xd4ef3085); /* 43 */
215+
// HH(ref b, c, d, a, x[k + 6], S34, 0x4881d05); /* 44 */
216+
// HH(ref a, b, c, d, x[k + 9], S31, 0xd9d4d039); /* 45 */
217+
// HH(ref d, a, b, c, x[k + 12], S32, 0xe6db99e5); /* 46 */
218+
// HH(ref c, d, a, b, x[k + 15], S33, 0x1fa27cf8); /* 47 */
219+
// HH(ref b, c, d, a, x[k + 2], S34, 0xc4ac5665); /* 48 */
220+
221+
// /* Round 4 */
222+
// II(ref a, b, c, d, x[k + 0], S41, 0xf4292244); /* 49 */
223+
// II(ref d, a, b, c, x[k + 7], S42, 0x432aff97); /* 50 */
224+
// II(ref c, d, a, b, x[k + 14], S43, 0xab9423a7); /* 51 */
225+
// II(ref b, c, d, a, x[k + 5], S44, 0xfc93a039); /* 52 */
226+
// II(ref a, b, c, d, x[k + 12], S41, 0x655b59c3); /* 53 */
227+
// II(ref d, a, b, c, x[k + 3], S42, 0x8f0ccc92); /* 54 */
228+
// II(ref c, d, a, b, x[k + 10], S43, 0xffeff47d); /* 55 */
229+
// II(ref b, c, d, a, x[k + 1], S44, 0x85845dd1); /* 56 */
230+
// II(ref a, b, c, d, x[k + 8], S41, 0x6fa87e4f); /* 57 */
231+
// II(ref d, a, b, c, x[k + 15], S42, 0xfe2ce6e0); /* 58 */
232+
// II(ref c, d, a, b, x[k + 6], S43, 0xa3014314); /* 59 */
233+
// II(ref b, c, d, a, x[k + 13], S44, 0x4e0811a1); /* 60 */
234+
// II(ref a, b, c, d, x[k + 4], S41, 0xf7537e82); /* 61 */
235+
// II(ref d, a, b, c, x[k + 11], S42, 0xbd3af235); /* 62 */
236+
// II(ref c, d, a, b, x[k + 2], S43, 0x2ad7d2bb); /* 63 */
237+
// II(ref b, c, d, a, x[k + 9], S44, 0xeb86d391); /* 64 */
238+
239+
// A += a;
240+
// B += b;
241+
// C += c;
242+
// D += d;
243+
// }
244+
// return new UInt32[] { A, B, C, D };
245+
// }
246+
// public static byte[] MD5Array(byte[] input)
247+
// {
248+
// MD5_Init();
249+
// UInt32[] block = MD5_Append(input);
250+
// UInt32[] bits = MD5_Trasform(block);
251+
252+
// /* Encodes bits (UInt32[]) into output (byte[]). Assumes len is
253+
// * a multiple of 4.
254+
// */
255+
// byte[] output = new byte[bits.Length * 4];
256+
// for (int i = 0, j = 0; i < bits.Length; i++, j += 4) {
257+
// output[j] = (byte)(bits[i] & 0xff);
258+
// output[j + 1] = (byte)((bits[i] >> 8) & 0xff);
259+
// output[j + 2] = (byte)((bits[i] >> 16) & 0xff);
260+
// output[j + 3] = (byte)((bits[i] >> 24) & 0xff);
261+
// }
262+
// return output;
263+
// }
264+
265+
// public static string ArrayToHexString(byte[] array, bool uppercase)
266+
// {
267+
// string hexString = "";
268+
// string format = "x2";
269+
// if (uppercase) {
270+
// format = "X2";
271+
// }
272+
// foreach (byte b in array) {
273+
// hexString += b.ToString(format);
274+
// }
275+
// return hexString;
276+
// }
277+
278+
// public static string MDString(string message)
279+
// {
280+
// char[] c = message.ToCharArray();
281+
// byte[] b = new byte[c.Length];
282+
// for (int i = 0; i < c.Length; i++) {
283+
// b[i] = (byte)c[i];
284+
// }
285+
// byte[] digest = MD5Array(b);
286+
// return ArrayToHexString(digest, false);
287+
// }
288+
289+
// }
290+
//}

0 commit comments

Comments
 (0)