12,882,142 members (29,341 online)
Add your own
alternative version

#### Stats

38.5K views
1.9K downloads
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

## Introduction

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 en.wikipedia.org)

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 by` ChecksumValue` 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
else
textVal.Text = "Unable to get checksum!"; //failure```

## Conclusion

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

## History

• Version 1.0.0.0, 29th October 2007 - First initial release

## License

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
Software developer since 1992

## Comments and Discussions

 View All Threads First Prev Next
 Accuracy seandillon12-Nov-07 1:47 seandillon 12-Nov-07 1:47
 Jukov, Did you check the accuracy of your Adler32 checksum? Had you done so you would have found "Wikipedia", which I assume was the source of your project, does not result in 0x11E60398 as it should. Your code only works (and slowly at that) for files that are an exact multiple of 0x0400 bytes in length. I'll let you figure out what is wrong -- a big clue tho' is to have a look at the size of the buffer passed to MakeForBuf. Try your code with a small file, say 9 bytes in length, and see what happens. D
 Last Visit: 31-Dec-99 18:00     Last Update: 23-Apr-17 9:57 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    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.170422.1 | Last Updated 29 Oct 2007
Article Copyright 2007 by jukov
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid