Click here to Skip to main content
14,325,129 members

CRC32: Generating a checksum for a file

Rate this:
4.90 (69 votes)
Please Sign up or sign in to vote.
4.90 (69 votes)
17 Dec 2001
How to generate a CRC32 based on a file

Sample Image - crc32.gif


Recently I wrote a program in which I wanted to generate a CRC for a given file. I did some checking on the web for sample CRC code, but found very few algorithms to help me. So I decided to learn more about CRCs and write my own code. This article describes what a CRC is, how to generate them, what they can be used for, and lastly source code showing how it's done.

What is a CRC

CRC is an acronym for Cyclic Redundancy Checksum or Cyclic Redundancy Check (depending on who you ask). A CRC is a "digital signature" representing data. The most common CRC is CRC32, in which the "digital signature" is a 32-bit number. The "data" that is being CRC'ed can be any data of any length; from a file, to a string, or even a block of memory. As long as the data can be represented as a series of bytes, it can be CRC'ed. There is no single CRC algorithm, there can be as many algorithms as there are programmers. The ideal CRC algorithm has several characteristics about it. First, if you CRC the same data more than once, you must get the same CRC every time. Secondly, if you CRC two different pieces of data you should get two very different CRC values. If you CRC the same data twice, you get the same digital signature. But if you CRC data that differs (even by a single byte) then you should get two very different digital signatures. With a 32-bit CRC there are over 4 billion possible CRC values. To be exact that's 232 or 4,294,967,296. With that many CRC values it's not difficult for every piece of data being CRC'ed to get a unique CRC value. However, it is possible for spurious hits to happen. In other words two completely different pieces of data can have the same CRC. This is rare, but not so rare that it won't happen.

Why use CRCs

Most of the time CRCs are used to compare data as an integrity check. Suppose there are two files that need to be compared to determine if they are identical. The first file is on Machine A and the other file is on Machine B. Each file is a rather large file (say 500 MB), and there is no network connection between the two machines. How do you compare the two files? The answer is CRC. You CRC each of the two files, which gives you two 32-bit numbers. You then compare those 32-bit numbers to see if they are identical. If the CRC values are different, then you can be 100% guaranteed that the files are not the same. If the CRC values are the same, then you can be 99% sure that the files are the same. Remember, because spurious hits can happen you cannot be positive that the two files are identical. The only way to be positive they are the same is to break down and do a comparison one byte at a time. But CRCs offer a quick way to be reasonably certain that two files are identical.

How to generate CRCs

Generating CRCs is a lot like cryptography in that involves a lot of mathematical theories. Since I don't fully understand it myself, I won't go into a lot of those details here. Instead I'll focus on how to program a CRC algorithm. Once you know how the algorithm works you should be able to write a CRC algorithm in any language on any platform. The first part of generating CRCs is the CRC lookup table. In CRC32 this is a table of 256 specific CRC numbers. These numbers are generated by a polynomial (the computation of these numbers and what polynomial to use are part of that math stuff I'm avoiding). The next part is a CRC lookup function. This function takes two things, a single byte of data to be CRC'ed and the current CRC value. It does a lookup in the CRC table according to the byte provided, and then does some math to apply that lookup value to the given CRC value resulting in a new CRC value. The last piece needed is the actual data that is to be CRC'ed. The CRC algorithm reads the first byte of data and calls the CRC lookup function which returns the CRC value for that single byte. It then calls the CRC lookup function with the next byte of data and passes the previous CRC value. After the second call, the CRC value represents the CRC of the first two bytes. You continuously call the CRC lookup function until all the bytes of the data have been processed. The resulting value is the CRC for the whole data.

Code Details

In this sample program I wanted to show that there are many different ways of generating CRCs. There are over 8 different CRC functions, all based on the above steps for generating CRCs. Each function differs slightly in it's intended use or optimization. There are four main CRC functions, each described below. There are also two separate CRC classes, but more on that later. And lastly there are a few helper functions that CRC strings.

C++ Streams: The first function represents the simplest CRC function. The file is opened using the C++ stream classes (ifstream). This function uses nothing but standard C++ calls, so this function should compile and run using any C++ compiler on any OS.

Win32 I/O: This function is more optimized in that it uses the Win32 API for file I/O; CreateFile, and ReadFile. This will speed up the processing, but by using the Win32 API the code is no longer platform independent.

Filemaps: This function uses memory mapped files to process the file. Filemaps can be used to greatly increase the speed with which files are accessed. They allow the contents of a file to be accessed as if it were in memory. No longer does the programmer need to call ReadFile and WriteFile.

Assembly: The final CRC function is one that is optimized using Intel Assembly. By hand writing the assembly code the algorithm can be optimized for speed, although at the sacrifice of being easy to read and understand.

Those are the four main CRC functions. But there are actually two versions of each function. There are two classes, CCrc32Dynamic and CCrc32Static, each of which have the above four functions for a total of eight. The only difference between the static and dynamic classes is the CRC table. With the static class the CRC table and all the functions in the class are static. The trade off is simple. The static class is simpler to use, but the dynamic class uses memory more efficiently because the CRC table (1K in size) is only allocated when needed.

// Using the static class is as easy as one line of code
dwErrorCode = CCrc32Static::FileCrc32Assembly(m_strFilename, dwCrc32);

// Whereas there is more involved when using the dynamic class
CCrc32Dynamic *pobCrc32Dynamic = new CCrc32Dynamic;
dwErrorCode = pobCrc32Dynamic->FileCrc32Assembly(m_strFilename, dwCrc32);
delete pobCrc32Dynamic;

Whenever you calculate a CRC you need to take into account the speed of the algorithm. Generating CRCs for files is both a CPU and a disk intensive task. Here is a table showing the time it took to CRC three different files. The columns are the different file sizes, the rows are the different CRC functions, and the table entries are in seconds. The system these numbers were captured on is a dual Pentium III at 1 GHz with a 10,000 RPM SCSI Ultra160 hard drive.

44 Kb34 Mb5 Gb
C++ Streams0.00130.80125
Win32 I/O0.00090.6085

As expected the C++ streams is the slowest function followed by the Win32 I/O. However, I was very surprised to see the filemaps were not faster than the Win32 I/O, in fact they are slower. After I thought about it some, I realized memory mapped files are designed to provide fast random access to files. But when you CRC you access the file sequentially. Thus filemaps are not faster, and the extra overhead of creating the "views" of the file are why it's slower. Filemaps do have one advantage that none of the other functions have. Memory mapped files are guaranteed to be able to access files up to the maximum file size in NT which is 264 or 18 exabytes. Although the Win32 I/O may handle files of this size, none of the documentation confirms this. [Note: The largest file I have CRC'ed is 40 GB, which all eight functions successfully CRC'ed, but took over 10 minutes each.]

If anyone who reads this article knows a way to improve the speed even more, please post the code or email me. Especially if you know of a speed improvement for the assembly code. I will bet there are further optimizations that can be made to the assembly code. After all I don't know Intel Assembly very well, therefore I'm sure there are optimizations I don't know about.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Brian Friesen
Web Developer
United States United States
No Biography provided

Comments and Discussions

GeneralThanks Pin
OffLineR31-Jan-06 21:20
memberOffLineR31-Jan-06 21:20 
GeneralDifferent CRC Pin
Sniper16722-Jan-06 10:56
memberSniper16722-Jan-06 10:56 
GeneralRe: Different CRC Pin
Brian Friesen22-Jan-06 12:09
memberBrian Friesen22-Jan-06 12:09 
GeneralRe: Different CRC Pin
VEMS9-Jan-07 9:49
memberVEMS9-Jan-07 9:49 
GeneralRe: Different CRC Pin
awneil14-Jul-08 3:30
memberawneil14-Jul-08 3:30 
Generalthis is not the way to use CRC Pin
jt_roxas10-Dec-05 14:30
memberjt_roxas10-Dec-05 14:30 
GeneralRe: this is not the way to use CRC Pin
hagaylevy11-Jul-09 4:04
memberhagaylevy11-Jul-09 4:04 
GeneralThe initial value of CRC32 Pin
Member 239066925-Oct-05 17:07
memberMember 239066925-Oct-05 17:07 
Hi, Sir

Thank's for your introduction and code, it does make great help to the junior about CRC field.

I have questions about the code, would you kindly answer them? Thank you very much. ^_^

1.Why you set the initial CRC32 value to be 0xFF FF FF FF? Could I use another numbers, such as 0x00 00 00 00 or else, to be the initial value?

2.After completing CRC32 calculation(doing a lot of XOR and shift)? I found that you invert the value which gets from CRC32 calculation and use this inverted value as the result of CRC32. For what reason?

Would you enlight the junior about CRC, thank you very much. Smile | :)

GOD Bless you
GeneralCCrc32Dynamic::Free(void) Pin
ally_s6-Sep-05 3:40
memberally_s6-Sep-05 3:40 
GeneralRe: CCrc32Dynamic::Free(void) Pin
Brian Friesen6-Sep-05 5:18
memberBrian Friesen6-Sep-05 5:18 
Generalboost::crc Pin
owmarr26-Jul-05 20:02
memberowmarr26-Jul-05 20:02 
GeneralDoesn't work for large files Pin
Anonymous1-Apr-05 8:41
memberAnonymous1-Apr-05 8:41 
GeneralRe: Doesn't work for large files Pin
Brian Friesen1-Apr-05 11:13
memberBrian Friesen1-Apr-05 11:13 
GeneralLink Error "undefined symbol __CrtDbgReport" Pin
kpatelPro14-Feb-05 14:04
memberkpatelPro14-Feb-05 14:04 
GeneralRe: Link Error "undefined symbol __CrtDbgReport" Pin
Brian Friesen14-Feb-05 15:38
memberBrian Friesen14-Feb-05 15:38 
GeneralRe: Link Error "undefined symbol __CrtDbgReport" Pin
kpatelPro14-Feb-05 17:45
memberkpatelPro14-Feb-05 17:45 
Questionhow to compare folders by using CRC Pin
greenjade80023-Aug-04 11:28
membergreenjade80023-Aug-04 11:28 
GeneralCRC used with serialization Pin
time_error11-Dec-03 20:58
membertime_error11-Dec-03 20:58 
QuestionCRC32's Question? Pin
david75329-Aug-03 23:35
memberdavid75329-Aug-03 23:35 
QuestionGetting consistent CRCs?? Pin
JohnDoeNumber230-Jul-03 7:53
memberJohnDoeNumber230-Jul-03 7:53 
AnswerRe: Getting consistent CRCs?? Pin
Brian Friesen30-Jul-03 9:47
memberBrian Friesen30-Jul-03 9:47 
GeneralRe: Getting consistent CRCs?? Pin
JohnDoeNumber230-Jul-03 10:25
memberJohnDoeNumber230-Jul-03 10:25 
GeneralRe: Getting consistent CRCs?? Pin
Wanderley Caloni25-May-05 8:01
professionalWanderley Caloni25-May-05 8:01 
AnswerRe: Getting consistent CRCs?? Pin
HPSI3-Aug-03 0:05
memberHPSI3-Aug-03 0:05 
GeneralRe: Getting consistent CRCs?? Pin
#realJSOP31-Oct-03 2:59
mve#realJSOP31-Oct-03 2:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 17 Dec 2001


161 bookmarked