Published
Monday, June 05, 2006 10:29 PM
by
Aleph
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;
}