16,471
社区成员
发帖
与我相关
我的任务
分享
wcstombs_s(&CharactersConverted, file,FileName.GetLength()+1,FileName, _TRUNCATE);
int CalculateMD5(CString FileName, CString &MD5)
{
const size_t StringSize = FileName.GetLength() + 1;
size_t CharactersConverted = 0;
char *file = new char[StringSize];
//char file[1024];
//file=FileName.GetBuffer(FileName.GetLength());
wcstombs_s(&CharactersConverted, file,FileName.GetLength()+1, FileName, _TRUNCATE);
int i, j;
FILE *fInput;
MD5Context md5Hash;
unsigned char bBuffer[4096];
unsigned char b;
char c;
if(!CryptStartup())
{
MessageBoxW(0, L"Could not start crypto library",
L"MD5", MB_ICONERROR);
return 0;
}
fInput = fopen(file, "rb");
if(!fInput)
{
MessageBoxW(0, L"Failed to open - Invalid File",
L"MD5", MB_ICONERROR);
CryptCleanup();
return 0;
}
memset(&md5Hash, 0, sizeof(MD5Context));
MD5Init(&md5Hash);
while(!feof(fInput)){
unsigned int nCount = fread(bBuffer, sizeof(unsigned char),
4096, fInput);
MD5Update(&md5Hash, bBuffer, nCount);
}
MD5Final(&md5Hash);
fclose(fInput);
//printf("\nChecksum of '%s' is: ", argv[1]);
char *Value = new char[1024];int k = 0;
for(i = 0; i < 16; i++)
{
b = md5Hash.digest[i];
for(j = 4; j >= 0; j -= 4)
{
c = ((char)(b >> j) & 0x0F);
if(c < 10) c += '0';
else c = ('a' + (c - 10));
//printf("%c", c);
Value[k] = c;
k++;
}
}
Value[k] = '\0';
CryptCleanup();
//CString cString;
MD5 = CString(Value);
//MessageBox(cString);
delete file;
delete Value;
return 1;
}