|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Md5_Calculator
{
class Md5Base
{
private static int blockCount = 0;
uint WordA = 0x67452301,
WordB = 0xefcdab89,
WordC = 0x98badcfe,
WordD = 0x10325476;
uint[] Table = new uint[64 ];
uint F(uint x, uint y, uint z) { return ((x & y) | ((~x) & z));}
uint G(uint x, uint y, uint z) { return (x & z) | ((~z) & y); }
uint H(uint x, uint y, uint z) { return x ^ y ^ z; }
uint I(uint x, uint y, uint z) { return (y ^ ((~z) | x)); }
uint RotateLeft(uint x, int s) { return (x << s) | (x >> (32 - s)); }
uint Round1(uint a, uint b, uint c, uint d, byte k, byte s, byte i)
{
return b + RotateLeft((a + F(b, c, d) + X[k] + Table[i - 1]), s);
}
uint Round2(uint a, uint b, uint c, uint d, byte k, byte s, byte i)
{
return b + RotateLeft((a + G(b, c, d) + X[k] + Table[i - 1]), s);
}
uint Round3(uint a, uint b, uint c, uint d, byte k, byte s, byte i)
{
return b + RotateLeft((a + H(b, c, d) + X[k] + Table[i - 1]), s);
}
uint Round4(uint a, uint b, uint c, uint d, byte k, byte s, byte i)
{
return b + RotateLeft((a + I(b, c, d) + X[k] + Table[i - 1]), s);
}
public void ProcessBlock(uint[] block)
{
Array.Copy(block, X, (long) 16);
uint A = WordA;
uint B = WordB;
uint C = WordC;
uint D = WordD;
#region Round1
A = Round1(A, B, C, D, 0, 7, 1);
D = Round1(D, A, B, C, 1, 12, 2);
C = Round1(C, D, A, B, 2, 17, 3);
B = Round1(B, C, D, A, 3, 22, 4);
A = Round1(A, B, C, D, 4, 7, 5);
D = Round1(D, A, B, C, 5, 12, 6);
C = Round1(C, D, A, B, 6, 17, 7);
B = Round1(B, C, D, A, 7, 22, 8);
A = Round1(A, B, C, D, 8, 7, 9);
D = Round1(D, A, B, C, 9, 12, 10);
C = Round1(C, D, A, B, 10, 17, 11);
B = Round1(B, C, D, A, 11, 22, 12);
A = Round1(A, B, C, D, 12, 7, 13);
D = Round1(D, A, B, C, 13, 12, 14);
C = Round1(C, D, A, B, 14, 17, 15);
B = Round1(B, C, D, A, 15, 22, 16);
#endregion
#region Round2
A = Round2(A, B, C, D, 1, 5, 17);
D = Round2(D, A, B, C, 6, 9, 18);
C = Round2(C, D, A, B, 11, 14, 19);
B = Round2(B, C, D, A, 0, 20, 20);
A = Round2(A, B, C, D, 5, 5, 21);
D = Round2(D, A, B, C, 10, 9, 22);
C = Round2(C, D, A, B, 15, 14, 23);
B = Round2(B, C, D, A, 4, 20, 24);
A = Round2(A, B, C, D, 9, 5, 25);
D = Round2(D, A, B, C, 14, 9, 26);
C = Round2(C, D, A, B, 3, 14, 27);
B = Round2(B, C, D, A, 8, 20, 28);
A = Round2(A, B, C, D, 13, 5, 29);
D = Round2(D, A, B, C, 2, 9, 30);
C = Round2(C, D, A, B, 7, 14, 31);
B = Round2(B, C, D, A, 12, 20, 32);
#endregion
#region Round3
A = Round3(A, B, C, D, 5, 4, 33);
D = Round3(D, A, B, C, 8, 11, 34);
C = Round3(C, D, A, B, 11, 16, 35);
B = Round3(B, C, D, A, 14, 23, 36);
A = Round3(A, B, C, D, 1, 4, 37);
D = Round3(D, A, B, C, 4, 11, 38);
C = Round3(C, D, A, B, 7, 16, 39);
B = Round3(B, C, D, A, 10, 23, 40);
A = Round3(A, B, C, D, 13, 4, 41);
D = Round3(D, A, B, C, 0, 11, 42);
C = Round3(C, D, A, B, 3, 16, 43);
B = Round3(B, C, D, A, 6, 23, 44);
A = Round3(A, B, C, D, 9, 4, 45);
D = Round3(D, A, B, C, 12, 11, 46);
C = Round3(C, D, A, B, 15, 16, 47);
B = Round3(B, C, D, A, 2, 23, 48);
#endregion
#region Round4
A = Round4(A, B, C, D, 0, 6, 49);
D = Round4(D, A, B, C, 7, 10, 50);
C = Round4(C, D, A, B, 14, 15, 51);
B = Round4(B, C, D, A, 5, 21, 52);
A = Round4(A, B, C, D, 12, 6, 53);
D = Round4(D, A, B, C, 3, 10, 54);
C = Round4(C, D, A, B, 10, 15, 55);
B = Round4(B, C, D, A, 1, 21, 56);
A = Round4(A, B, C, D, 8, 6, 57);
D = Round4(D, A, B, C, 15, 10, 58);
C = Round4(C, D, A, B, 6, 15, 59);
B = Round4(B, C, D, A, 13, 21, 60);
A = Round4(A, B, C, D, 4, 6, 61);
D = Round4(D, A, B, C, 11, 10, 62);
C = Round4(C, D, A, B, 2, 15, 63);
B = Round4(B, C, D, A, 9, 21, 64);
#endregion
WordA += A;
WordB += B;
WordC += C;
WordD += D;
blockCount++;
}
public Md5Base()
{
// Costruzione Tabella
for (int i = 1; i <= 64; i++)
Table[i - 1] = (uint)(0x100000000 * (double)Math.Abs(Math.Sin((double)i)));
}
public String returnDigest()
{
//Process String Rappresentation
string wA = null, wB = null, wC = null, wD = null;
for (int j = 0; j < 32; j += 8)
{
wA += ((byte)(WordA >> j)).ToString("X2");
wB += ((byte)(WordB >> j)).ToString("X2");
wC += ((byte)(WordC >> j)).ToString("X2");
wD += ((byte)(WordD >> j)).ToString("X2");
}
return wA + wB + wC + wD;
}
}
}
|
|