Home > Back-end >  Md5 result is wrong, ask where is wrong.
Md5 result is wrong, ask where is wrong.

Time:09-25

# include "md5. H"
# include "math. H"
String BinaryToHex (char * szBuf, int iLen)
{//for little endian
String strRet="";
Const char * szHexNumber="0123456789 abcdef";
for (int i=0; i{
Int iHigh=(szBuf [I] & amp; 0 xf0) & gt;> 4.
Int iLow=szBuf [I] & amp; 0 xf.
StrRet=szHexNumber [iLow] + strRet;
StrRet=szHexNumber [iHigh] + strRet;
}

Return strRet.
}

Int F (int X, int Y, int Z)
{
Return (X & amp; Y) | ((~ X) & amp; Z);
}

Int G (int X, int Y, int Z)
{
Return (X & amp; (Z) | Y & amp; (~ Z));
}

Int H (int X, int Y, int Z)
{
Return X ^ ^ Y Z;
}

Int I (int X, int Y, int Z)
{
Return ^ Y (X | Z (~));
}

String HexToBinary (const string& StrHex)
{
Char * szAlphaTable [256].
SzAlphaTable [' 0 ']="0000";
SzAlphaTable [' 1 ']="0001";
SzAlphaTable (' 2 ')="0010";
SzAlphaTable [' 3 ']="0011";
SzAlphaTable [' 4 ']="0100";
SzAlphaTable [' 5 ']="0101";
SzAlphaTable [' 6 ']="0110";
SzAlphaTable [' 7 ']="0111";
SzAlphaTable [' 8 ']="1000";
SzAlphaTable [' 9 ']="1001";
SzAlphaTable [' A ']="1010";
SzAlphaTable [' B ']="1011";
SzAlphaTable [' C ']="1100";
SzAlphaTable [' D ']="1101";
SzAlphaTable [' E ']="1110";
SzAlphaTable [' F ']="1111";

String strRet="";
For (int I=0, iLen=strHex. The length (); i{
StrRet +=szAlphaTable [(int) (strHex [I])];
}
Return strRet.
}

Int S (unsigned int X, int iBit)
{
Return (X & lt; (32 - iBit));
}

Void FF (unsigned int& A, unsigned int b, unsigned int c, unsigned int d, unsigned int Mj, unsigned int s, unsigned int ti)
{
A=b + S (a + F (b, c, d) + Mj + ti, S);
}

Void GG (unsigned int& A, unsigned int b, unsigned int c, unsigned int d, unsigned int Mj, unsigned int s, unsigned int ti)
{
A=b + S (a + G (b, c, d) + Mj + ti, S);
}

Void HH (unsigned int& A, unsigned int b, unsigned int c, unsigned int d, unsigned int Mj, unsigned int s, unsigned int ti)
{
A=b + S (a + H (b, c, d) + Mj + ti, S);
}

Void II (unsigned int& A, unsigned int b, unsigned int c, unsigned int d, unsigned int Mj, unsigned int s, unsigned int ti)
{
A=b + S (a + I (b, c, d) + Mj + ti, S);
}

Typedef void (* LoopFunc) (unsigned int& , unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int);
The static LoopFunc arrLoopFuncs [4]={} FF, GG, HH, II;
The static unsigned int t [64]={0};
Const static unsigned int A=0 x67452301l;
Const static unsigned int B=0 xefcdab89;
Const static unsigned int C=0 x98badcfe;
Const static unsigned int D=0 x10325476;

String md5 (char * szContent, int iLen)
{
If (iLen==1)
{//- 1 is assigned as the default for iLen by declaration
ILen=strlen (szContent);
}
Int iRemainder=iLen % 64;
Int i64ByteNum=iRemainder & gt; 56? (iLen/64 + 2) : (iLen/64 + 1);
Char * szBuf=new char [i64ByteNum * 64];
Memset (szBuf, 0, i64ByteNum * 64);
Memcpy (szBuf, szContent iLen);
Int iPaddingByte=iRemainder & lt;=56? (56 - iRemainder) : (64 - iRemainder) + 56;
for (int i=0; i{
If (I==0)
{
SzBuf [iLen + I]=0 x80;
continue;
}

SzBuf [iLen + I]=0;
}
//here confused with endian.
Char szTemp [4]={0};
Memcpy (szTemp, & amp; ILen, 4);
SzBuf [iLen + iPaddingByte]=szTemp [3].
=szTemp szBuf [iPaddingByte iLen + + 1] [2].
SzBuf [iLen + iPaddingByte + 2]=szTemp [1].
SzBuf [iLen + iPaddingByte + 3]=szTemp [0].
//memcpy (szBuf + iLen + iPaddingByte, & amp; ILen, sizeof (iLen));

If (t [0]==0)
{//the initialize t
For (int I=1; i<=64; I++)
{
T [I - 1]=(unsigned int) (4294967296 * abs (sin (I)));
Printf (" \ n - 2 t % d: % s ", I, BinaryToHex ((char *) (& amp; T [I - 1]), 4). The c_str ());
}
}

Static int arrMiIdxes [4] [16]={
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12},
{5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2},
{0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9}
};

Static int arrLeftShitNum [4] [16]={
{7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22},
{5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20},
{4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23},
{6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21}
};

Unsigned int arrResult [4]={A, B, C, D};
For (int k=0; K{
Unsigned int M [16].
Memcpy (M, szBuf + k * 64, 64);

Unsigned int arrTemp [4].
Memcpy (arrTemp arrResult, 16);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related