Solved

How to get a checksum for a disk file?

Posted on 1997-10-30
5
270 Views
Last Modified: 2013-12-03
I want to be able to quickly compare two disk files to see
if they are identical.  I have the ability to remember
information about the files, so it would be handy to keep
checksums for older files, and then just checksum new files
as they come in and compare the results.

Short of writing the checksumming code myself, is there a
way of generating checksums on Windows files?  (Appearances
to the contrary notwithstanding, this is not actually a
yes/no question; please do not respond unless you can
actually point me at something useful. :-)

I'm using Borland C++ on Win32.

Extra credit (but no extra points :-):  It'd be nice to be
able to generate the checksums on buffers in memory as well.
0
Comment
Question by:pbonney
  • 3
5 Comments
 
LVL 7

Expert Comment

by:faster
ID: 1408094
For normal checksum is easy, for example, if you have a buffer which is n Bytes long:

unsigned char checksum = 0;

for (int i=0; i<n; i++)
   checksum += buf[i];

That's it.

If you want better digital digest, you can use MD5.  There is also another option: CRC, which is better than checksum, but MD5 is the best of these three.



0
 

Author Comment

by:pbonney
ID: 1408095
Nothing personal -- this is my first time using this service
(and I realize you're volunteering your help) -- but this is
hardly the "expert" answer I was hoping for.

Your solution provides a non-unique checksum for in-memory
buffers.  My question was focused on a useful solution for disk
files, and (while I didn't constrain the answer to any particular
checksum algorithm) the question's designation as 'hard' I'd
think rules out an algorithm that doesn't deliver a pretty
unique fingerprint.

Moreover, I can certainly implement CRC or MD5 (or, more likely
since security isn't an issue, MD4) myself.  However, My query
was an attempt to see if I could save myself the effort by
finding something either in the windows APIs or off-the-shelf.

I apologize if, in my efforts to be succinct in my phrasing of
the question, I was unclear in my intent.  I'm looking for a
pointer to a windows API call, or a cost-effective freeware/
shareware/commercial package (source and/or binary).

Thanks for responding!
0
 
LVL 7

Expert Comment

by:faster
ID: 1408096
It is OK, I don't know what purpose you want for a "checksum", you know all the options I mentioned are useful in respective situations.

I am using md5, since our application must be secure and I am using a product of a company called "algorithmic research".  I am  very sure that win32 does not have such an API, but I don't know whether there is a freeware.  Most probably someone has written the code in some book.
0
 
LVL 7

Expert Comment

by:faster
ID: 1408097
You might want to have a look at:

http://www.cs.umd.edu/~harry/jotp/

it has md4 and md5 implemented in java.
0
 

Accepted Solution

by:
dmetzler earned 200 total points
ID: 1408098
//
// GetFileChecksum returns a BYTE which contains a file checksum.
// Returns 0 for files not found.
//

BYTE GetFileChecksum(char *szFile)
{
      BYTE      bSum = 0 ;
      CFile      theFile ;
      DWORD      dwFileSize ;
      DWORD      idx ;
      char      buffer[255] ;
                CFileStatus  theFS ;

      CFile::GetStatus(szFile, theFS) ;

      dwFileSize = theFS.m_size ;

      if (dwFileSize > 0)
      {
            if (theFile.Open(szFile, CFile::modeRead))
            {
                  char *pByte = new char[dwFileSize + 1] ;
                  if (!pByte)
                        return (0) ;

                  theFile.Read(pByte, dwFileSize) ;

                  idx = 0 ;

                  while (dwFileSize > 0)
                  {
                        bSum += pByte[idx] ;
                        dwFileSize-- ;
                        idx++ ;
                  }

                  delete [] pByte ;

                  theFile.Close() ;
            }
      }

      return (bSum) ;
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now