.h文件如下:
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
typedef struct {
unsigned char digest[16];
unsigned long hHash;
} MD5Context;
BOOL CryptStartup();
void CryptCleanup();
void MD5Init(MD5Context *ctx);
void MD5Update(MD5Context *ctx, unsigned char const *buf, unsigned int len);
void MD5Final(MD5Context *ctx);
int CalculateMD5(CString FileName, CString &MD5);
int CalculateMD5(string FileName,string& MD5);
.cpp文件如下:
#include "stdafx.h"
#include "MD5.h"
HCRYPTPROV hCryptProv;
BOOL CryptStartup()
{
if(CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == 0){
if(GetLastError() == NTE_EXISTS){
if(CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0) == 0)
return FALSE;
}else return FALSE;
}
return TRUE;
}
void CryptCleanup()
{
if(hCryptProv) CryptReleaseContext(hCryptProv, 0);
hCryptProv = 0;
}
void MD5Init(MD5Context *ctx)
{
CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &ctx->hHash);
}
void MD5Update(MD5Context *ctx, unsigned char const *buf, unsigned int len)
{
CryptHashData(ctx->hHash, buf, len, 0);
}
void MD5Final(MD5Context *ctx)
{
DWORD dwCount = 16;
CryptGetHashParam(ctx->hHash, HP_HASHVAL, ctx->digest, &dwCount, 0);
if(ctx->hHash) CryptDestroyHash(ctx->hHash);
ctx->hHash = 0;
}
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];
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 - Invaild 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();
MD5 = CString(Value);////
delete file;
delete Value;
return 1;
}
int CalculateMD5(string FileName,string& MD5)
{
const size_t StringSize = FileName.length() + 1; //
size_t CharactersConverted = 0;
char *file = new char[StringSize];
//char file[1024];
wcstombs_s(&CharactersConverted, file, FileName.length()+1, CString(FileName.c_str()), _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 - Invaild 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();
MD5 = string(Value);////
delete file;
delete Value;
return 1;
}
评论