📄 md5.cpp
字号:
PRINT(d, a, b, c, x[ 9], S12, 0x8b44f7af,10); /* 10 */ FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
PRINT(c, d, a, b, x[10], S13, 0xffff5bb1,11); /* 11 */ FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
PRINT(b, c, d, a, x[11], S14, 0x895cd7be,12); /* 12 */ FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
PRINT(a, b, c, d, x[12], S11, 0x6b901122,13); /* 13 */ FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
PRINT(d, a, b, c, x[13], S12, 0xfd987193,14); /* 14 */ FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
PRINT(c, d, a, b, x[14], S13, 0xa679438e,15); /* 15 */ FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
PRINT(b, c, d, a, x[15], S14, 0x49b40821,16); /* 16 */ FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
/* 第二轮循环 */
PRINT(a, b, c, d, x[ 1], S21, 0xf61e2562,17); /* 17 */ GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
PRINT(d, a, b, c, x[ 6], S22, 0xc040b340,18); /* 18 */ GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
PRINT(c, d, a, b, x[11], S23, 0x265e5a51,19); /* 19 */ GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
PRINT(b, c, d, a, x[ 0], S24, 0xe9b6c7aa,20); /* 20 */ GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
PRINT(a, b, c, d, x[ 5], S21, 0xd62f105d,21); /* 21 */ GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
PRINT(d, a, b, c, x[10], S22, 0x2441453,22); /* 22 */ GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
PRINT(c, d, a, b, x[15], S23, 0xd8a1e681,23); /* 23 */ GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
PRINT(b, c, d, a, x[ 4], S24, 0xe7d3fbc8,24); /* 24 */ GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
PRINT(a, b, c, d, x[ 9], S21, 0x21e1cde6,25); /* 25 */ GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
PRINT(d, a, b, c, x[14], S22, 0xc33707d6,26); /* 26 */ GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
PRINT(c, d, a, b, x[ 3], S23, 0xf4d50d87,27); /* 27 */ GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
PRINT(b, c, d, a, x[ 8], S24, 0x455a14ed,28); /* 28 */ GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
PRINT(a, b, c, d, x[13], S21, 0xa9e3e905,29); /* 29 */ GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
PRINT(d, a, b, c, x[ 2], S22, 0xfcefa3f8,30); /* 30 */ GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
PRINT(c, d, a, b, x[ 7], S23, 0x676f02d9,31); /* 31 */ GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
PRINT(b, c, d, a, x[12], S24, 0x8d2a4c8a,32); /* 32 */ GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
/* 第三轮循环 */
PRINT(a, b, c, d, x[ 5], S31, 0xfffa3942,33); /* 33 */ HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
PRINT(d, a, b, c, x[ 8], S32, 0x8771f681,34); /* 34 */ HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
PRINT(c, d, a, b, x[11], S33, 0x6d9d6122,35); /* 35 */ HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
PRINT(b, c, d, a, x[ 14], S34, 0xfde5380c,36); /* 36 */ HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
PRINT(a, b, c, d, x[ 1], S31, 0xa4beea44,37); /* 37 */ HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
PRINT(d, a, b, c, x[ 4], S32, 0x4bdecfa9,38); /* 38 */ HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
PRINT(c, d, a, b, x[ 7], S33, 0xf6bb4b60,39); /* 39 */ HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
PRINT(b, c, d, a, x[10], S34, 0xbebfbc70,40);; /* 40 */ HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
PRINT(a, b, c, d, x[13], S31, 0x289b7ec6,41);; /* 41 */ HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
PRINT(d, a, b, c, x[ 0], S32, 0xeaa127fa,42);; /* 42 */ HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
PRINT(c, d, a, b, x[ 3], S33, 0xd4ef3085,43);; /* 43 */ HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
PRINT(b, c, d, a, x[ 6], S34, 0x4881d05,44);; /* 44 */ HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
PRINT(a, b, c, d, x[ 9], S31, 0xd9d4d039,45);; /* 45 */ HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
PRINT(d, a, b, c, x[12], S32, 0xe6db99e5,46);; /* 46 */ HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
PRINT(c, d, a, b, x[15], S33, 0x1fa27cf8,47);; /* 47 */ HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
PRINT(b, c, d, a, x[ 2], S34, 0xc4ac5665,48);; /* 48 */ HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
/* 第四轮循环 */
PRINT(a, b, c, d, x[ 0], S41, 0xf4292244,49);; /* 49 */ II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
PRINT(d, a, b, c, x[ 7], S42, 0x432aff97,50);; /* 50 */ II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
PRINT(c, d, a, b, x[14], S43, 0xab9423a7,51);; /* 51 */ II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
PRINT(b, c, d, a, x[ 5], S44, 0xfc93a039,52);; /* 52 */ II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
PRINT(a, b, c, d, x[12], S41, 0x655b59c3,53);; /* 53 */ II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
PRINT(d, a, b, c, x[ 3], S42, 0x8f0ccc92,54);; /* 54 */ II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
PRINT(c, d, a, b, x[10], S43, 0xffeff47d,55);; /* 55 */ II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
PRINT(b, c, d, a, x[ 1], S44, 0x85845dd1,56);; /* 56 */ II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
PRINT(a, b, c, d, x[ 8], S41, 0x6fa87e4f,57);; /* 57 */ II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
PRINT(d, a, b, c, x[15], S42, 0xfe2ce6e0,58);; /* 58 */ II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
PRINT(c, d, a, b, x[ 6], S43, 0xa3014314,59);; /* 59 */ II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
PRINT(b, c, d, a, x[13], S44, 0x4e0811a1,60);; /* 60 */ II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
PRINT(a, b, c, d, x[ 4], S41, 0xf7537e82,61);; /* 61 */ II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
PRINT(d, a, b, c, x[11], S42, 0xbd3af235,62);; /* 62 */ II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
PRINT(c, d, a, b, x[ 2], S43, 0x2ad7d2bb,63);; /* 63 */ II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
PRINT(b, c, d, a, x[ 9], S44, 0xeb86d391,64);; /* 64 */ II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
pstroutput+=sprintf(pstroutput,"\n");
pstroutput+=sprintf(pstroutput,"a:\t\t\t\t\t");
printfaint(a);
pstroutput+=sprintf(pstroutput,"b:\t\t\t\t\t");
printfaint(b);
pstroutput+=sprintf(pstroutput,"c:\t\t\t\t\t");
printfaint(c);
pstroutput+=sprintf(pstroutput,"d:\t\t\t\t\t");
printfaint(d);
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
pstroutput+=sprintf(pstroutput,"\n");
pstroutput+=sprintf(pstroutput,"状态0=状态0+a:\t\t\t\t");
printfaint(state[0]);
pstroutput+=sprintf(pstroutput,"状态1=状态1+b:\t\t\t\t");
printfaint(state[1]);
pstroutput+=sprintf(pstroutput,"状态2=状态3+c:\t\t\t\t");
printfaint(state[2]);
pstroutput+=sprintf(pstroutput,"状态3=状态3+d:\t\t\t\t");
printfaint(state[3]);//16个素的整数组为重置零
MD5_memset ((POINTER)x, 0, sizeof (x));
}
//开始进行MD5计算
void MD5Init (MD5_CTX *context )
{
context->count[0] = context->count[1] = 0; //两计数器者初化0 64 位长度位
//在这里定义四个常数,也就是我们刚才讲到的四个特征数.
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
for(int i=0 ;i<4;i++)
{
unsigned int uintTempSwap=ByteSwap(context->state[i]);
unsigned int uintTempSawpLen=sizeof(uintTempSwap);
// printf("context->state[%d]:\t",i);
pstroutput+=sprintf(pstroutput,"\n\t\t");
pstroutput+=sprintf(pstroutput,"context->state[%d]:\t",i);
OutputBitOfBytes((unsigned char *)(&uintTempSwap),uintTempSawpLen);
//printf("\n");
}
}
void MD5Update (MD5_CTX *context,
unsigned char *input,
unsigned int inputLen ) //所以实际上可哈希长度2^32-1字节
//2^40bits
{
unsigned int i, index, nRequireLen;
//count[0]是低位,index是context->buffer的当前指针,指向字节>>3是/8将位转为字节
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
// index:0x3F=00111111=63
//index:& 0x3F=%64 模运算的另种表示
// index:首次调用时为0
// printf("index:\t %d\n",index);
pstroutput+=sprintf(pstroutput,"\nindex:\t %d\n",index);
//下句好象初看上去恒不成立,count[0]>=0 实际上加法运算含%2^32=0x 1 0000 0000
//0x2 000 0001=2^29+1=inputLen;inputLen << 3=8(*2^3 %2^32);count[0]=8 ;
//inputLen >>29 =1(/2^29 %2^32);count[1]=1;长度位即此两值;
//实际上前者位数模2^32的余数。字节数/2^29的倍数;两者并未真实反映实长;
//大于2^29部分去了,再右移3位得余数
//count[1]>0只能说明inputLen >2^29=512MB 左移面9位得倍数
//一个数乘8 模2^32 后
//在C;C++实现时不超2^32能反映真实长度
//context->count[0]=0 第一次调用恒不成立;
//第二次调用时;inputLen<64 (inputLen<<3 ) <512
//要count[0]+(inputLen<<3 ) < (inputLen<<3 ) 只有 2^32-count[0] <512
//即count[0]>0xFFFF FE00
//这时记录长度已无意义
context->count[0] += ((UINT4)inputLen << 3);
//twx注释
/* if (
( context->count[0] += ((UINT4)inputLen << 3) ) < ((UINT4)inputLen << 3))
context->count[1]++;
*/
//context->count[0]首次调用时:如果inputLen<=2^29则记录了位数
//附加调用时,长度位已编码固定了,也累积记录了全部位数
//context->count[0] +=(inputLen*2^3)%2^32 以以位的形式来记录
//count[0]是低位
// printf("context->count[0]:\t %d bits \n",context->count[0]);
pstroutput+=sprintf(pstroutput,"context->count[0]:\t %d bits \n",context->count[0]);
// printf("context->count[1]:\t %d\n",context->count[1]);
pstroutput+=sprintf(pstroutput,"context->count[1]:\t %d\n",context->count[1]);
context->count[1] += ((UINT4)inputLen >> 29); //context->count[1] 存字数 量纲2^29
//隐含%2^32
// printf("context->count[1]:\t %d\n",context->count[1]);
pstroutput+=sprintf(pstroutput,"context->count[1]:\t %d\n",context->count[1]);
nRequireLen = 64 - index; //附加位时调用,表示还要nRequireLen字节才能构成一分组,
// printf("nRequireLen = 64 - index:\t %d\n",nRequireLen);
pstroutput+=sprintf(pstroutput,"nRequireLen = 64 - index:\t %d\n",nRequireLen);
// printf("inputLen :\t %d\n",inputLen);
pstroutput+=sprintf(pstroutput,"inputLen :\t %d\n",inputLen);
if (inputLen >= nRequireLen) { //如果输入不比需要的小,则能划成分组
//对"abc"或不足分组时进不来,
//从输入中复制所需长度到缓存,
// printf("inputLen >= nRequireLen is true \n");
pstroutput+=sprintf(pstroutput,"inputLen >= nRequireLen is true \n");
MD5_memcpy((POINTER)&context->buffer[index], (POINTER)input, nRequireLen);
for(int k=0 ;k<64;k++)
{
// unsigned int uintTempSwap=ByteSwap(context->state[i]);
if((k%2==0) ){// printf("\n");
pstroutput+=sprintf(pstroutput,"\n\t\t\t\t\t");}
unsigned int uintTempSawpLen=sizeof(context->buffer[k]);
// pstroutput+=sprintf(pstroutput,);
// printf("buffer[%d]:\t",k);
pstroutput+=sprintf(pstroutput,"buffer[%d]:\t",k);
OutputBitOfBytes((unsigned char *)(&context->buffer[k]),uintTempSawpLen);
}
//MD5变换
MD5Transform (context->state, context->buffer);
//输入长度如比所需要长,则继续处理下分组
//i为长消息中的位置64k k>=1
for (i = nRequireLen; i + 63 < inputLen; i += 64)
MD5Transform (context->state, &input[i]);
index = 0;
}
else //不够一个分组 //i为长消息中的位置
{
// printf("inputLen >= nRequireLen is False \n");
pstroutput+=sprintf(pstroutput,"inputLen >= nRequireLen is False \n");
i = 0;
}
//附加调用时,将输入累积到分组中的第buffer[index]位置
//首次调用时,将不足一分组的字节累累积到缓存
MD5_memcpy((POINTER)&context->buffer[index], (POINTER)&input[i],
inputLen-i); //对于"abc" i=0;
for(int k=0 ;k<inputLen-i+index;k++)
{
// unsigned int uintTempSwap=ByteSwap(context->state[i]);
if((k%2==0)) {//printf("\n");
pstroutput+=sprintf(pstroutput,"\n\t\t\t\t\t");}
unsigned int uintTempSawpLen=sizeof(context->buffer[k]);
//printf("buffer[%d]:\t",k);
pstroutput+=sprintf(pstroutput,"buffer[%d]:\t",k);
OutputBitOfBytes((unsigned char *)(&context->buffer[k]),uintTempSawpLen);
//
}
// printf("\n");
pstroutput+=sprintf(pstroutput,"\n");
}
//处理最后的一个分组
void MD5Final (unsigned char digest[16],MD5_CTX *context)
{
unsigned char bits[8];
unsigned int index, padLen;
//将64bit 长度位转换成 字节串;
Encode (bits, context->count, 8);
index = (unsigned int)((context->count[0] >> 3) & 0x3f);
//一个分组填不下,再加一分组
//index =57;padLen=128-8-57=63
padLen = (index < 56) ? (56 - index) : (120 - index);
//填充长度=64-8-3=53 //8bytes 长度位,3bytes是最后一个分组的长度
// printf("padLen:%d\n",padLen);
pstroutput+=sprintf(pstroutput,"padLen:%d\n",padLen);
//附加填充位
MD5Update (context, PADDING, padLen);
//附加长度位
MD5Update (context, bits, 8);
//将状态置转为字节型串输出为哈希
pstroutput+=sprintf(pstroutput,"\n");
pstroutput+=sprintf(pstroutput,"状态1:\t\t\t\t\t");
printfaint(context->state[0]);
pstroutput+=sprintf(pstroutput,"状态2:\t\t\t\t\t");
printfaint(context->state[1]);
pstroutput+=sprintf(pstroutput,"状态3:\t\t\t\t\t");
printfaint(context->state[2]);
pstroutput+=sprintf(pstroutput,"状态4:\t\t\t\t\t");
printfaint(context->state[3]);
Encode (digest, context->state, 16);
MD5_memset ((POINTER)context, 0, sizeof (*context));
}
void MD5(char * pstr)
{
unsigned char hashvalue[16];
pstroutput+=sprintf(pstroutput,"\nhashed message s is \t%s\n",pstr);
unsigned int lenMessage=strlen((const char *)pstr);
pstroutput+=sprintf(pstroutput,"strlen(s):\t%d\n",lenMessage);
for (int i=0;i<lenMessage;i++) //二进制格式
{
if(i%2==0 ) pstroutput+=sprintf(pstroutput,"\n\t\t\t\t\t");
pstroutput+=sprintf(pstroutput,"pstr[%d]:\t",i);
OutputBitOfByte(pstr[i]);
pstroutput+=sprintf(pstroutput,"\t");
}
MD5_CTX* pMd5_ctx=new MD5_CTX;
//初始化MD5环境
MD5Init(pMd5_ctx);
//输入Mesage 及其长度。
//将可哈多哈希的分组做掉
MD5Update(pMd5_ctx,(unsigned char *)pstr,lenMessage);
//附加填充位,长度位 再做最后一个分组
MD5Final(hashvalue,pMd5_ctx);
//输出HASH值;
for (i=0;i<16;i++) //二进制格式
{
if(i%4==0 ) pstroutput+=sprintf(pstroutput,"\n哈希值组%d:\t\t\t",int(i/4+1));
pstroutput+=sprintf(pstroutput,"\t");
OutputBitOfByte(hashvalue[i]);
}//左补零输出
for ( i=0;i<16;i++) //十六进制格式
{
if(i%4==0 ) pstroutput+=sprintf(pstroutput,"\n哈希值组%d:\t\t\t",int(i/4+1));
pstroutput+=sprintf(pstroutput,"\t");
pstroutput+=sprintf(pstroutput,"%02X",hashvalue[i]);
}//左补零输出
delete pMd5_ctx;
}
int main(int argc, char* argv[])
{
//64bytes
char message4[128]="0123456789012345678901234567890123456789012345678901234567890abc";
//73bytes
char message1[128]="1234567890123456789012345678901234567890123456789012345678901234567890abc";
//3bytes
char message2[128]="abc";
//60bytes
char message3[128]="456789012345678901234567890123456789012345678901234567890abc";
MD5(message2);
MD5(message3);
MD5(message4);
MD5(message1);
//说明整型在内存中的反序存储
unsigned char str[]="ABCDEF";
unsigned char *pstr=str;
unsigned int uintTemp=0x01020304;
unsigned int *puintTemp=&uintTemp;
unsigned int uintTempSwap=ByteSwap(uintTemp);
unsigned int *puintTempSwap=&uintTempSwap;
unsigned int nBytes=sizeof(puintTempSwap);
OutputBitOfBytes((unsigned char * )puintTempSwap,nBytes);
fstream filetemp;
char strtempfile[256]={0};
GetCurrentDirectory(256,strtempfile);
char strfilename[256]="\\md5.txt";
strcat(strtempfile,strfilename);
filetemp.open(strtempfile,ios::out,filebuf::openprot);
filetemp.write((unsigned char const *)pstroutput0,pstroutput-pstroutput0);
filetemp.close();
free(pstroutput0);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -