(md5校验和类的实现文件)
/*****************************************************************************************
FUNCTION: CMD5Checksum::GetMD5
DETAILS: static, public
DESCRIPTION: Gets the MD5 checksum for a specified file
RETURNS: CString : the hexadecimal MD5 checksum for the specified file
ARGUMENTS: CString& strFilePath : the full pathname of the specified file
NOTES: Provides an interface to the CMD5Checksum class. ''''strFilePath'''' name should
hold the full pathname of the file, eg C:\My Documents\Arcticle.txt.
NB. If any problems occur with opening or reading this file, a CFileException
will be thrown; callers of this function should be ready to catch this
exception.
*****************************************************************************************/
CString CMD5Checksum::GetMD5(const CString& strFilePath)
{
//open the file as a binary file in readonly mode, denying write access
CFile File(strFilePath, CFile::shareDenyNone);
//the file has been successfully opened, so now get and return its checksum
return GetMD5(File);
}
/*****************************************************************************************
FUNCTION: CMD5Checksum::GetMD5
DETAILS: static, public
DESCRIPTION: Gets the MD5 checksum for a specified file
RETURNS: CString : the hexadecimal MD5 checksum for the specified file
ARGUMENTS: CFile& File : the specified file
NOTES: Provides an interface to the CMD5Checksum class. ''''File'''' should be open in
binary readonly mode before calling this function.
NB. Callers of this function should be ready to catch any CFileException
thrown by the CFile functions
*****************************************************************************************/
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::GetMD5
DETAILS: static,