void CMD5TestDlg::OnButtonCheck()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,"All Files (*.*)|*.*||");
if (dlg.DoModal() == IDOK )
{
m_FilePathName = dlg.GetPathName();
}
if(m_FilePathName == "")
{
AfxMessageBox("请选择要校验的文件!");
return;
}
CFile mFile;
if(mFile.Open(m_FilePathName,CFile::modeRead)==0)
return;
CString strMD5 = CMD5Checksum ::GetMD5(mFile);
GetDlgItem(IDC_EDIT_MD5)->SetWindowText(strMD5);
}
主要用到了CMD5Checksum 的GetMD5函数。
以下是关于CMD5Checksum的代码:
MD5Checksum.h:
/*****************************************************************************************
*****************************************************************************************/
#if !defined(AFX_MD5CHECKSUM_H__2BC7928E_4C15_11D3_B2EE_A4A60E20D2C3__INCLUDED_)
#define AFX_MD5CHECKSUM_H__2BC7928E_4C15_11D3_B2EE_A4A60E20D2C3__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/*****************************************************************************************
*****************************************************************************************/
class CMD5Checksum
{
public:
static CString GetMD5OfString(CString strString);
//interface functions for the RSA MD5 calculation
static CString GetMD5(CFile& File);
//static CString GetMD5(const CString& strFilePath);
protected:
//constructor/destructor
CMD5Checksum();
virtual ~CMD5Checksum() {};
//RSA MD5 implementation
void Transform(BYTE Block[64]);
void Update(BYTE* Input, ULONG nInputLen);
CString Final();
inline DWORD RotateLeft(DWORD x, int n);
inline void FF( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T);
inline void GG( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T);
inline void HH( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T);
inline void II( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T);
//utility functions
inline void DWordToByte(BYTE* Output, DWORD* Input, UINT nLength);
inline void ByteToDWord(DWORD* Output, BYTE* Input, UINT nLength);
private:
BYTE m_lpszBuffer[64]; //input buffer
ULONG m_nCount[2]; //number of bits, modulo 2^64 (lsb first)
ULONG m_lMD5[4]; //MD5 checksum
};
#endif // !defined(AFX_MD5CHECKSUM_H__2BC7928E_4C15_11D3_B2EE_A4A60E20D2C3__INCLUDED_)
MD5Checksum.cpp:
/*****************************************************************************************
*****************************************************************************************/
#include "stdafx.h"
#include "MD5Checksum.h"
#include "MD5ChecksumDefines.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/*****************************************************************************************
*****************************************************************************************/
CString CMD5Checksum::GetMD5(CFile& File)
{
try
{
CMD5Checksum MD5Checksum; //checksum object
int nLength = 0; //number of bytes read from the file
const int nBufferSize = 1024; //checksum the file in blocks of 1024 bytes
BYTE Buffer[nBufferSize]; //buffer for data read from the file
//checksum the file in blocks of 1024 bytes
while ((nLength = File.Read( Buffer, nBufferSize )) > 0 )
{
MD5Checksum.Update( Buffer, nLength );
}
//finalise the checksum and return it
return MD5Checksum.Final();
}
//report any file exceptions in debug mode only
catch (CFileException* e )
{
TRACE0("CMD5Checksum::GetMD5: CFileException caught");
throw e;
}
}
/*****************************************************************************************
FUNCTION: CMD5Checksum::RotateLeft
DETAILS: private
DESCRIPTION: Rotates the bits in a 32 bit DWORD left by a specified amount
RETURNS: The rotated DWORD
ARGUMENTS: DWORD x : the value to be rotated
int n : the number of bits to rotate by
*****************************************************************************************/
DWORD CMD5Checksum::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));
}
/*****************************************************************************************
FUNCTION: CMD5Ch