#include "md5.h"
#include "math.h"
string BinaryToHex(char* szBuf, int iLen)
{//for little endian
string strRet = "";
const char* szHexNumber = "0123456789ABCDEF";
for (int i=0; i<iLen; i++)
{
int iHigh = (szBuf[i] & 0xf0) >> 4;
int iLow = szBuf[i] & 0xf;
strRet = szHexNumber[iLow] + strRet;
strRet = szHexNumber[iHigh] + strRet;
}
return strRet;
}
int F(int X, int Y, int Z)
{
return (X & Y) | ((~X) & Z);
}
int G(int X, int Y, int Z)
{
return (X & Z) | (Y & (~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.length(); i<iLen; i++)
{
strRet += szAlphaTable[(int)(strHex[i])];
}
return strRet;
}
int S(unsigned int X, int iBit)
{
return (X << iBit) | (X >> (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);
static LoopFunc arrLoopFuncs[4] = {FF, GG, HH, II};
static unsigned int t[64] = {0};
const static unsigned int A = 0x67452301L;
const static unsigned int B = 0xEFCDAB89;
const static unsigned int C = 0x98BADCFE;
const static unsigned int D = 0x10325476;
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 > 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 <= 56? (56 - iRemainder): (64 - iRemainder) + 56;
for (int i=0; i<iPaddingByte; i++)
{
if (i == 0)
{
szBuf[iLen + i] = 0x80;
continue;
}
szBuf[iLen + i] = 0;
}
//here confused with endian.
char szTemp[4] = {0};
memcpy(szTemp, &iLen, 4);
szBuf[iLen + iPaddingByte] = szTemp[3];
szBuf[iLen + iPaddingByte + 1] = szTemp[2];
szBuf[iLen + iPaddingByte + 2] = szTemp[1];
szBuf[iLen + iPaddingByte + 3] = szTemp[0];
//memcpy(szBuf + iLen + iPaddingByte, &iLen, sizeof(iLen));
if (t[0] == 0)
{//initialize t
for (int i=1; i<=64; i++)
{
t[i - 1] = (unsigned int)(4294967296 * abs(sin(i)));
printf("t%-2d: %s\n", i, BinaryToHex((char*)(&t[i - 1]), 4).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<i64ByteNum; k++)
{
unsigned int M[16];
memcpy(M, szBuf + k * 64, 64);
unsigned int arrTemp[4];
memcpy(arrTemp, arrResult, 16);
for (int i=0; i<4; i++)
{
LoopFunc func = arrLoopFuncs[i];
for (int j=0; j<16; j++)
{
int iStIdx = ((-j) % 4 + 4) % 4;
func(arrTemp[iStIdx], arrTemp[(iStIdx + 1) % 4], arrTemp[(iStIdx + 2) % 4], arrTemp[(iStIdx + 3) % 4], M[arrMiIdxes[i][j]], arrLeftShitNum[i][j], t[i * 16 + j]);
}
}
arrResult[0] += arrTemp[0];
arrResult[1] += arrTemp[1];
arrResult[2] += arrTemp[2];
arrResult[3] += arrTemp[3];
}
delete[] szBuf;
string strRet = BinaryToHex((char*)arrResult, 16);
return strRet;
}
uj5u.com熱心網友回復:
int iHigh = (szBuf[i] & 0xf0) >> 4;
int iLow = szBuf[i] & 0xf;
strRet = szHexNumber[iLow] + strRet;
strRet = szHexNumber[iHigh] + strRet;
既然你是bin輸出,這個高低位元組序不用調整吧。
strRet += szHexNumber[iHigh];
strRet += szHexNumber[iLow];
別的還沒看。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/75910.html
標籤:基礎類
