下面的代码是<<加密解密>>第一版随书光盘 关于计算MD5的一段代码:
我的问题是:由于我是在标准C++下编程,但最后计算MD5的代码是MFC下实现的,我该如何修改下面的代码
//User interface: Caculate MD5 value of target file name
// and return in CString style
CString MD5_Caculate_File ( CString szFileName )
{
CString MD5_Value_String = "" ;
HANDLE hFile = CreateFile ( szFileName, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, \
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ) ;
if ( hFile == INVALID_HANDLE_VALUE )
{
MD5_Value_String.Format ( "CreateFile Error ( code:%d )!", GetLastError() );
return MD5_Value_String;
}
HANDLE hFileMap = CreateFileMapping ( hFile, NULL, PAGE_READWRITE, 0, 0, NULL ) ;
if ( hFileMap == NULL )
{
MD5_Value_String.Format ( "CreateFileMapping Error ( code:%d )!", GetLastError() );
CloseHandle ( hFile ) ;
return MD5_Value_String ;
}
PVOID pMapView = MapViewOfFile ( hFileMap, FILE_MAP_READ, 0, 0, 0 ) ;
if ( pMapView == NULL )
{
MD5_Value_String.Format ( "MapViewOfFile Error ( code:%d )!", GetLastError() );
CloseHandle ( hFileMap ) ;
CloseHandle ( hFile ) ;
return MD5_Value_String ;
}
DWORD dwLowDWord = 0, dwHighDWord = 0 ;
dwLowDWord = GetFileSize ( hFile, &dwHighDWord ) ;
UINT64 uFileSize = ( dwHighDWord << 32 ) + dwLowDWord ;
MD5_CONTEXT MD5_Value ;
memset ( &MD5_Value, 0, sizeof(MD5_Value) ) ;
if ( MD5_Caculate( &MD5_Value, (BYTE*)pMapView, uFileSize*8) )
{
CString TempString ;
MD5_Value_String = "" ;
for ( int i = 0; i < 16; i++ )
{
TempString.Format ( "%02X", ((byte*)&MD5_Value)[i] ) ;
MD5_Value_String += TempString ;
}
}
UnmapViewOfFile ( pMapView ) ;
CloseHandle ( hFileMap ) ;
CloseHandle ( hFile ) ;
return MD5_Value_String ;
}
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!