MD5算法实现收集------为什么linux和windows下MD5计算结果不一样

本文探讨了在Linux和Windows操作系统中计算MD5校验和时出现结果不一致的原因,通过实例分析和比较两种环境下的MD5实现,揭示了隐藏在背后的差异和可能的解决方案。
摘要由CSDN通过智能技术生成

实现一:

//#include <iostream>  
//#include "md5.h"  
//using namespace std;
//
//int main()
//{
//	cout << MD5("abc").toString() << endl;
//
//	return 0;
//}

#include   <stdio.h>  
#include   <stdlib.h> 
#include   <time.h>  
#include   <string.h>  

typedef   unsigned   char   *POINTER;
typedef   unsigned   short   int   UINT2;
typedef   unsigned   long   int   UINT4;

typedef   struct
{
	UINT4   state[4];
	UINT4   count[2];
	unsigned   char   buffer[64];
}   MD5_CTX;

void   MD5Init(MD5_CTX   *);
void   MD5Update(MD5_CTX   *, unsigned   char   *, unsigned   int);
void   MD5Final(unsigned   char[16], MD5_CTX   *);

#define   S11   7  
#define   S12   12  
#define   S13   17  
#define   S14   22  
#define   S21   5  
#define   S22   9  
#define   S23   14  
#define   S24   20  
#define   S31   4  
#define   S32   11  
#define   S33   16  
#define   S34   23  
#define   S41   6  
#define   S42   10  
#define   S43   15  
#define   S44   21  

static   unsigned   char   PADDING[64] = {
	0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

#define   F(x,   y,   z)   (((x)   &   (y))   |   ((~x)   &   (z)))  
#define   G(x,   y,   z)   (((x)   &   (z))   |   ((y)   &   (~z)))  
#define   H(x,   y,   z)   ((x)   ^   (y)   ^   (z))  
#define   I(x,   y,   z)   ((y)   ^   ((x)   |   (~z)))  

#define   ROTATE_LEFT(x,   n)   (((x)   <<   (n))   |   ((x)   >>   (32-(n))))  

#define   FF(a,   b,   c,   d,   x,   s,   ac)   {     (a)   +=   F   ((b),   (c),   (d))   +   (x)   +   (UINT4)(ac);     (a)   =   ROTATE_LEFT   ((a),   (s));     (a)   +=   (b);       }  
#define   GG(a,   b,   c,   d,   x,   s,   ac)   {     (a)   +=   G   ((b),   (c),   (d))   +   (x)   +   (UINT4)(ac);     (a)   =   ROTATE_LEFT   ((a),   (s));     (a)   +=   (b);       }  
#define   HH(a,   b,   c,   d,   x,   s,   ac)   {     (a)   +=   H   ((b),   (c),   (d))   +   (x)   +   (UINT4)(ac);     (a)   =   ROTATE_LEFT   ((a),   (s));     (a)   +=   (b);       }  
#define   II(a,   b,   c,   d,   x,   s,   ac)   {     (a)   +=   I   ((b),   (c),   (d))   +   (x)   +   (UINT4)(ac);     (a)   =   ROTATE_LEFT   ((a),   (s));     (a)   +=   (b);   }  


inline   void   Encode(unsigned   char   *output, UINT4   *input, unsigned   int   len)
{
	unsigned   int   i, j;

	for (i = 0, j = 0; j < len; i++, j += 4)   {
		output[j] = (unsigned   char)(input[i] & 0xff);
		output[j + 1] = (unsigned   char)((input[i] >> 8) & 0xff);
		output[j + 2] = (unsigned   char)((input[i] >> 16) & 0xff);
		output[j + 3] = (unsigned   char)((input[i] >> 24) & 0xff);
	}
}

inline   void   Decode(UINT4   *output, unsigned   char   *input, unsigned   int   len)
{
	unsigned   int   i, j;

	for (i = 0, j = 0; j < len; i++, j += 4)
		output[i] = ((UINT4)input[j]) | (((UINT4)input[j + 1]) << 8) |
		(((UINT4)input[j + 2]) << 16) | (((UINT4)input[j + 3]) << 24);
}

inline   void   MD5Transform(UINT4   state[4], unsigned   char   block[64])
{
	UINT4   a = state[0], b = state[1], c = state[2], d = state[3], x[16];
	Decode(x, block, 64);
	FF(a, b, c, d, x[0], S11, 0xd76aa478);   /*   1   */
	FF(d, a, b, c, x[1], S12, 0xe8c7b756);   /*   2   */
	FF(c, d, a, b, x[2], S13, 0x242070db);   /*   3   */
	FF(b, c, d, a, x[3], S14, 0xc1bdceee);   /*   4   */
	FF(a, b, c, d, x[4], S11, 0xf57c0faf);   /*   5   */
	FF(d, a, b, c, x[5], S12, 0x4787c62a);   /*   6   */
	FF(c, d, a, b, x[6], S13, 0xa8304613);   /*   7   */
	FF(b, c, d, a, x[7], S14, 0xfd469501);   /*   8   */
	FF(a, b, c, d, x[8], S11, 0x698098d8);   /*   9   */
	FF(d, a, b, c, x[9], S12, 
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值