12,451,896 members (27,088 online)
alternative version

#### Stats

36.6K views
25 bookmarked
Posted

, 29 Oct 2007 CPOL
 Rate this:
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

• 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:

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

## Share

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

## You may also be interested in...

 Pro Pro

 First Prev Next
 what type of hashing is used in it zameer990915-Apr-10 13:00 zameer9909 15-Apr-10 13:00
 Accuracy seandillon12-Nov-07 1:47 seandillon 12-Nov-07 1:47
 Apalling. GZero5-Nov-07 1:01 GZero 5-Nov-07 1:01
 This is dreadful code, you've taken an Algorithm built for speed and coded it so badly that it is MUCH slower than every CRC32 implementation I've seen on this site. CRC32 is known to be a MUCH slower Algorithm (and more accurate) Do not consider using this in a production environment, I'm still killing w3wp processes that are quietly going through 50mb of files byte by byte. I say again, this is dreadful code.
 Re: Apalling. merlin9817-Nov-07 4:37 merlin981 7-Nov-07 4:37
 Last Visit: 31-Dec-99 18:00     Last Update: 28-Aug-16 8:41 Refresh 1