⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 checksum.cpp

📁 《Visual C++视频技术方案宝典》配套光盘
💻 CPP
字号:
// Checksum.cpp: implementation of the CChecksum class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MD5Client.h"
#include "Checksum.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CChecksum::CChecksum()
{
	// zero members
	memset(m_Buffer, 0, 64);
	m_Count[0] = m_Count[1] = 0;

	// Load magic state initialization constants
	m_MD5[0] = MD5_INIT_STATE_0;
	m_MD5[1] = MD5_INIT_STATE_1;
	m_MD5[2] = MD5_INIT_STATE_2;
	m_MD5[3] = MD5_INIT_STATE_3;
}

CChecksum::~CChecksum()
{

}

CString CChecksum::GetMD5(const CString &strFilePath)
{
	//open the file as a binary file in readonly mode, denying write access 
	CFile File(strFilePath, CFile::modeRead | CFile::shareDenyWrite | CFile::typeBinary);

	//成功打开文件则返回文件的效验和
	return GetMD5(File);
}

CString CChecksum::GetMD5(CFile &File)
{
	try
	{
		CChecksum Checksum;	           	//checksum类对象	
		int nLength = 0;				//文件的字节数
		const int nBufferSize = 1024;	//以1024字节为数据块效验文件
		BYTE Buffer[nBufferSize];		//存储读取出文件数据的数组

		while ((nLength = File.Read( Buffer, nBufferSize )) > 0 )
		{
			Checksum.Update( Buffer, nLength );
		}

		//效验结束后返回效验和
		return Checksum.Final();
	}

	//report any file exceptions in debug mode only
	catch (CFileException* e )
	{
		TRACE0("GetMD5: CFileException caught");	
		throw e;
	}
}

void CChecksum::Update(BYTE *Input, ULONG nInputLen)
{
	//Compute number of bytes mod 64
	UINT nIndex = (UINT)((m_Count[0] >> 3) & 0x3F);

	//Update number of bits
	if ( ( m_Count[0] += nInputLen << 3 )  <  ( nInputLen << 3) )
	{
		m_Count[1]++;
	}
	m_Count[1] += (nInputLen >> 29);

	//Transform as many times as possible.
	UINT i=0;		
	UINT nPartLen = 64 - nIndex;
	if (nInputLen >= nPartLen) 	
	{
		memcpy( &m_Buffer[nIndex], Input, nPartLen );
		Transform( m_Buffer );
		for (i = nPartLen; i + 63 < nInputLen; i += 64) 
		{
			Transform( &Input[i] );
		}
		nIndex = 0;
	} 
	else 
	{
		i = 0;
	}

	// Buffer remaining input
	memcpy(&m_Buffer[nIndex], &Input[i], nInputLen-i);
}

DWORD CChecksum::RotateLeft(DWORD x, int n)
{
	//check that DWORD is 4 bytes long - true in Visual C++ 6 and 32 bit Windows
	ASSERT( sizeof(x) == 4 );

	//rotate and return x
	return (x << n) | (x >> (32-n));
}

void CChecksum::FF(DWORD &A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)
{
	DWORD F = (B & C) | (~B & D);
	A += F + X + T;
	A = RotateLeft(A, S);
	A += B;
}

void CChecksum::GG(DWORD &A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)
{
	DWORD G = (B & D) | (C & ~D);
	A += G + X + T;
	A = RotateLeft(A, S);
	A += B;
}

void CChecksum::HH(DWORD &A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)
{
	DWORD H = (B ^ C ^ D);
	A += H + X + T;
	A = RotateLeft(A, S);
	A += B;
}

void CChecksum::II(DWORD &A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)
{
	DWORD I = (C ^ (B | ~D));
	A += I + X + T;
	A = RotateLeft(A, S);
	A += B;
}

void CChecksum::Transform(BYTE Block[])
{
	//将效验和的初始值付给变量a、b、c、d
	ULONG a = m_MD5[0];
	ULONG b = m_MD5[1];
	ULONG c = m_MD5[2];
	ULONG d = m_MD5[3];

	//copy BYTES from input 'Block' to an array of ULONGS 'x'
	ULONG x[16];
	ByteToDWord(x, Block, 64);

	//第一轮转换
	FF(a, b, c, d, x[ 0], S11, 0xd76aa478); /* 01 */
	FF(d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 02 */
	FF(c, d, a, b, x[ 2], S13, 0x242070db); /* 03 */
	FF(b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 04 */
	FF(a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 05 */
	FF(d, a, b, c, x[ 5], S12, 0x4787c62a); /* 06 */
	FF(c, d, a, b, x[ 6], S13, 0xa8304613); /* 07 */
	FF(b, c, d, a, x[ 7], S14, 0xfd469501); /* 08 */
	FF(a, b, c, d, x[ 8], S11, 0x698098d8); /* 09 */
	FF(d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
	FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
	FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
	FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
	FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
	FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
	FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
	//第二轮转换
	GG(a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
	GG(d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
	GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
	GG(b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
	GG(a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
	GG(d, a, b, c, x[10], S22, 0x02441453); /* 22 */
	GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
	GG(b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
	GG(a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
	GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
	GG(c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
	GG(b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
	GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
	GG(d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
	GG(c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
	GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
	//第三轮转换
	HH(a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
	HH(d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
	HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
	HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
	HH(a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
	HH(d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
	HH(c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
	HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
	HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
	HH(d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
	HH(c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
	HH(b, c, d, a, x[ 6], S34, 0x04881d05); /* 44 */
	HH(a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
	HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
	HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
	HH(b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
	//第四轮转换
	II(a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
	II(d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
	II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
	II(b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
	II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
	II(d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
	II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
	II(b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
	II(a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
	II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
	II(c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
	II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
	II(a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
	II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
	II(c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
	II(b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ 

	//将转换后的值添加到效验和的数组中
	m_MD5[0] += a;
	m_MD5[1] += b;
	m_MD5[2] += c;
	m_MD5[3] += d;
}

void CChecksum::ByteToDWord(DWORD *Output, BYTE *Input, UINT nLength)
{
	//entry invariants
	ASSERT(nLength % 4 == 0);
	ASSERT(AfxIsValidAddress(Output, nLength/4, TRUE));
	ASSERT(AfxIsValidAddress(Input, nLength, FALSE));

	//transfer the data by shifting and copying
	for(UINT i=0,j=0;j<nLength;i++,j+=4)
	{
		Output[i] = (ULONG)Input[j]			| 
					(ULONG)Input[j+1] << 8	| 
					(ULONG)Input[j+2] << 16 | 
					(ULONG)Input[j+3] << 24;
	}
}

void CChecksum::DWordToByte(BYTE *Output, DWORD *Input, UINT nLength)
{
	//entry invariants
	ASSERT(nLength % 4 == 0);
	ASSERT(AfxIsValidAddress(Output, nLength, TRUE));
	ASSERT(AfxIsValidAddress(Input, nLength/4, FALSE));

	//transfer the data by shifting and copying
	for(UINT i=0,j=0;j<nLength;i++,j+=4) 
	{
		Output[j] =   (UCHAR)(Input[i] & 0xff);
		Output[j+1] = (UCHAR)((Input[i] >> 8) & 0xff);
		Output[j+2] = (UCHAR)((Input[i] >> 16) & 0xff);
		Output[j+3] = (UCHAR)((Input[i] >> 24) & 0xff);
	}
}

CString CChecksum::Final()
{
	//Save number of bits
	BYTE Bits[8];
	DWordToByte(Bits, m_Count, 8);

	//Pad out to 56 mod 64.
	UINT nIndex = (UINT)((m_Count[0] >> 3) & 0x3f);
	UINT nPadLen = (nIndex < 56) ? (56 - nIndex) : (120 - nIndex);
	Update(PADDING, nPadLen);

	//Append length (before padding)
	Update(Bits, 8);

	//Store final state in 'lpszMD5'
	const int nMD5Size = 16;
	unsigned char lpszMD5[ nMD5Size ];
	DWordToByte(lpszMD5, m_MD5, nMD5Size);

	//Convert the hexadecimal checksum to a CString
	CString strMD5;
	for(int i=0;i < nMD5Size;i++) 
	{
		CString Str;
		if (lpszMD5[i] == 0) 
		{
			Str = CString("00");
		}
		else if (lpszMD5[i] <= 15) 
		{
			Str.Format("0%x",lpszMD5[i]);
		}
		else
		{
			Str.Format("%x",lpszMD5[i]);
		}

		ASSERT(Str.GetLength() == 2);
		strMD5 += Str;
	}
	ASSERT(strMD5.GetLength() == 32);
	return strMD5;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -