主题:[转帖]自己动手编写MD5(C#)
//源文件:md5.cs
using System;
using System.Collections;
using System.IO;
public class MD5
{
//static state variables
private static UInt32 A;
private static UInt32 B;
private static UInt32 C;
private static UInt32 D;
//number of bits to rotate in tranforming
private const int S11 = 7;
private const int S12 = 12;
private const int S13 = 17;
private const int S14 = 22;
private const int S21 = 5;
private const int S22 = 9;
private const int S23 = 14;
private const int S24 = 20;
private const int S31 = 4;
private const int S32 = 11;
private const int S33 = 16;
private const int S34 = 23;
private const int S41 = 6;
private const int S42 = 10;
private const int S43 = 15;
private const int S44 = 21;
/* F, G, H and I are basic MD5 functions.
* 四个非线性函数:
*
* 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))
*
* (&与,|或,~非,^异或)
*/
private static UInt32 F(UInt32 x, UInt32 y, UInt32 z)
{
return (x & y) | ((~x) & z);
}
private static UInt32 G(UInt32 x, UInt32 y, UInt32 z)
{
return (x & z) | (y & (~z));
}
private static UInt32 H(UInt32 x, UInt32 y, UInt32 z)
{
return x ^ y ^ z;
}
private static UInt32 I(UInt32 x, UInt32 y, UInt32 z)
{
return y ^ (x | (~z));
}
/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* Rotation is separate from addition to prevent recomputation.
*/
private static void FF(ref UInt32 a, UInt32 b, UInt32 c, UInt32 d, UInt32 mj, int s, UInt32 ti)
{
a = a + F(b, c, d) + mj + ti;
a = a << s | a >> (32 - s);
a += b;
}
......
public static string TestSuite()
{
string s = "";
s += Test("");
s += Test("a");
s += Test("abc");
s += Test("message digest");
s += Test("abcdefghijklmnopqrstuvwxyz");
s += Test("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
s += Test("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
return s;
}
}
完整代码见附件
using System;
using System.Collections;
using System.IO;
public class MD5
{
//static state variables
private static UInt32 A;
private static UInt32 B;
private static UInt32 C;
private static UInt32 D;
//number of bits to rotate in tranforming
private const int S11 = 7;
private const int S12 = 12;
private const int S13 = 17;
private const int S14 = 22;
private const int S21 = 5;
private const int S22 = 9;
private const int S23 = 14;
private const int S24 = 20;
private const int S31 = 4;
private const int S32 = 11;
private const int S33 = 16;
private const int S34 = 23;
private const int S41 = 6;
private const int S42 = 10;
private const int S43 = 15;
private const int S44 = 21;
/* F, G, H and I are basic MD5 functions.
* 四个非线性函数:
*
* 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))
*
* (&与,|或,~非,^异或)
*/
private static UInt32 F(UInt32 x, UInt32 y, UInt32 z)
{
return (x & y) | ((~x) & z);
}
private static UInt32 G(UInt32 x, UInt32 y, UInt32 z)
{
return (x & z) | (y & (~z));
}
private static UInt32 H(UInt32 x, UInt32 y, UInt32 z)
{
return x ^ y ^ z;
}
private static UInt32 I(UInt32 x, UInt32 y, UInt32 z)
{
return y ^ (x | (~z));
}
/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* Rotation is separate from addition to prevent recomputation.
*/
private static void FF(ref UInt32 a, UInt32 b, UInt32 c, UInt32 d, UInt32 mj, int s, UInt32 ti)
{
a = a + F(b, c, d) + mj + ti;
a = a << s | a >> (32 - s);
a += b;
}
......
public static string TestSuite()
{
string s = "";
s += Test("");
s += Test("a");
s += Test("abc");
s += Test("message digest");
s += Test("abcdefghijklmnopqrstuvwxyz");
s += Test("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
s += Test("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
return s;
}
}
完整代码见附件