Re: MD5 checksum for file
Did you check it out: http://www.google.com/search?sourcei...cksums+windows
Or what exactly is your problem?
Re: MD5 checksum for file
@Victor..
Lets say.. I have a table.. that has filepaths and their corresponding MD5 checksums..
I want to programatically open each file, compute the MD5 checksum for it... and then verify whether the MD5 thats given in the table is the right one or not.
So i just need a library or something.. where I can just supply the file path, or file pointer.. and then get the MD5 string for the file..
Thanks.!!!
Re: MD5 checksum for file
Something like this:
Code:
#include <wincrypt.h>
int CalcHash(FILE *f,char *md5sum)
{
HCRYPTPROV hProv;
HCRYPTHASH hHash;
unsigned char buf[1024];
unsigned char hsh[16];
unsigned long sz;
char byt[3];
int rc,err,i;
size_t fsz;
rc=CryptAcquireContext(&hProv,NULL,MS_STRONG_PROV,PROV_RSA_FULL,0);
if(!rc){
err=GetLastError();
if(err==0x80090016){
//first time using crypto API, need to create a new keyset
rc=CryptAcquireContext(&hProv,NULL,MS_STRONG_PROV,PROV_RSA_FULL,CRYPT_NEWKEYSET);
if(!rc){
err=GetLastError();
return 0;
}
}
}
CryptCreateHash(hProv,CALG_MD5,0,0,&hHash);
while((fsz=fread(buf,1,1024,f))!=0){
CryptHashData(hHash,(unsigned char *)buf,fsz,0);
}
sz=16;
CryptGetHashParam(hHash,HP_HASHVAL,hsh,&sz,0);
md5sum[0]=0;
for(i=0;i<sz;i++){
sprintf(byt,"%.2X",hsh[i]);
strcat(md5sum,byt);
}
CryptDestroyHash(hHash);
CryptReleaseContext(hProv,0);
return 1;
}
int main()
{
FILE *f;
char md5sum[17];
f=fopen("c:/test.bmp","rb");
CalcHash(f,md5sum);
fclose(f);
return 0;
}
Re: MD5 checksum for file
Thanks.. :) that was exactly what i was looking for..
Re: MD5 checksum for file
This fixes a crash on the fclose() call: The variable "md5sum" should be 33 bytes, not 17:
char md5sum[(16 * 2) + 1] = { 0 };
... because "sz" is set to 16, and the sprintf is writing two characters at a time, up to "sz".