Click here to Skip to main content
12,813,994 members (34,493 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 29 Oct 2007

Adler-32 Checksum Calculation

, 29 Oct 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Presents a C# implementation of Adler-32 checksum calculation for use in the .NET Framework


Checksums are a common way to ensure data integrity. Adler-32 checksum may be used for revealing the damaged files or to compare two files for identity.

Background (from

Adler-32 is a checksum algorithm which was invented by Mark Adler. It is almost as reliable as a 32-bit cyclic redundancy check for protecting against accidental modification of data, such as distortions occurring during a transmission.

An Adler-32 checksum is obtained by calculating two 16-bit checksums A and B and concatenating their bits into a 32-bit integer. A is the sum of all bytes in the string; B is the sum of the individual values of A from each step. At the beginning of an Adler-32 run, A is initialized to 1, B to 0. The sums are done modulo 65521 (the largest prime number smaller than 216). The bytes are stored in network order (big endian), B occupying the two most significant bytes. The function may be expressed as A = 1 + D1 + D2 + ... + DN (mod 65521) B = (1 + D1) + (1 + D1 + D2) + ... + (1 + D1 + D2 + ... + DN) (mod 65521) = N×D1 + (N-1)×D2 + (N-2)×D3 + ... + DN + N (mod 65521) Adler-32(D) = B * 65536 + A where D is the string of bytes for which the checksum is to be calculated, and N is the length of D.

Using the Code

Here are the steps needed to add Adler-32 checksum into your program without worrying about how it works:

  • Add the source code AdlerChecksum.cs to your project and add to your source file using FileHelper;
  • Create an instance of the AdlerChecksum class.
  • Call MakeForFile method for checksum calculation.
  • Get Adler-32 checksum value byChecksumValue property. For printing method ToString can be used.

See example code below:

// Adler-32 checksum using
 AdlerChecksum acs = new AdlerChecksum();
 if (acs.MakeForFile(textPath.Text))
    textVal.Text = acs.ToString(); //success
    textVal.Text = "Unable to get checksum!"; //failure


I hope this code will be useful! Thanks all for the comments and critique. New suggestions and ideas are welcome.


  • Version, 29th October 2007 - First initial release


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Elmo Motion Control
Israel Israel
Software developer since 1992

You may also be interested in...

Comments and Discussions

Questionwhat type of hashing is used in it Pin
zameer990915-Apr-10 14:00
memberzameer990915-Apr-10 14:00 
GeneralAccuracy Pin
seandillon12-Nov-07 2:47
memberseandillon12-Nov-07 2:47 
GeneralApalling. Pin
GZero5-Nov-07 2:01
memberGZero5-Nov-07 2:01 
GeneralRe: Apalling. Pin
merlin9817-Nov-07 5:37
membermerlin9817-Nov-07 5:37 
First, this is a good idea, but it was implemented a little poorly. There are few comments (only XML function comments).

The reason the file checksum was performing so badly is that he is reading the file one byte at a time!

I rewrote the file checksum function (see below). The only problem with this rewrite is handling larger files, as reading all the bytes will cause your app to use that much memory (a 50M file will cause your app to use and additional 50M during this call). For those large files, I included a GarbageCollection in the finally block, to clear up the used memory. The buffer will almost always be in Gen 0 (sometimes Gen 1), so GC should happen very fast.

        public bool MakeForFile(string sPath)
            // holds the file buffer
            byte[] bytesBuff = null;

                // exit if no file
                if (!File.Exists(sPath))
                    m_unChecksumValue = 0;
                    return false;

                // get all the file bytes into the buffer
                bytesBuff = System.IO.File.ReadAllBytes(sPath);

                // if nothing loaded, then exit
                if ((bytesBuff == null) || (bytesBuff.Length == 0))
                    m_unChecksumValue = 0;
                    return false;

                // get the checksum
                return MakeForBuff(bytesBuff, AdlerStart);

                m_unChecksumValue = 0;
                return false;

                    // do garbage collection on the array if it is too large (> 5M)
                    bool DoGC = bytesBuff.Length > 5000000;

                    if (DoGC)
                        // find out what generation the array lives in
                        int Gen = GC.GetGeneration(bytesBuff);

                        // garbage collect on bytesBuff generation

                catch (Exception excep)
                    // garbage collection error handling here

                    // clear the buffer
                    if (bytesBuff != null)
                        bytesBuff = null;

            return true;

Rhabot - World of Warcraft Bot

Long URL's get neat with

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 29 Oct 2007
Article Copyright 2007 by jukov
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid