CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2009
    Posts
    3

    MD5 checksum for file

    Hi..

    I would like to get MD5 checksums of files in my program for verification purposes.. and I would
    like to use a very simple function.. like..

    bool generateMD5_for_file(FILE * fp, std::string generatedchecksum) ;

    Could anyone direct me to some appropriate wrapper routine which does this..

    Thanks!!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: MD5 checksum for file

    Did you check it out: http://www.google.com/search?sourcei...cksums+windows
    Or what exactly is your problem?
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2009
    Posts
    3

    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.!!!

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    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;
    }

  5. #5
    Join Date
    Dec 2009
    Posts
    3

    Re: MD5 checksum for file

    Thanks.. that was exactly what i was looking for..

  6. #6
    Join Date
    Nov 2011
    Posts
    1

    Exclamation 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".

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured