Published Monday, June 05, 2006 10:29 PM by Aleph

[AlephFS] File checksum using Adler32

Like the hash provider I need a function that will check for the integrity of the files managed by the web service. But CRC, MD5 and other checksum algorithms eat up a lot of CPU time and memory.
For my checksum function I've decided to use the Adler32 algorithm.The method that I am posting here works with any file size, as a remark, if you have a lot of memory on the server where you are doing checksums, make the buffer as big as you can, it will move faster but will eat more resources.
public static long Adler32Checksum(string filePath, int bufferLenght)
{
    long output = 0;
    int read = 0;

    Adler32 alder = new Adler32();

    byte[] buffer = new byte[bufferLenght];
    if (File.Exists(filePath))
    {
        using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            while ((read = fs.Read(buffer, 0, bufferLenght)) > 0)
            {
                alder.Update(buffer, 0, read);
            }
        }
    }
    output = alder.Value;
    return output;
}



Filed under ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

Name  

Comments 
Type the characters you see in the picture on the left.
    



Note: HTML tags in comment messages are not supported.


About Aleph

VB.NET programmer '02
C# programmer '03
Software Analyst '05
Software Architect '06