Skip to content

Instantly share code, notes, and snippets.

@carlozamagni
Last active January 3, 2016 07:59
Show Gist options
  • Save carlozamagni/8432805 to your computer and use it in GitHub Desktop.
Save carlozamagni/8432805 to your computer and use it in GitHub Desktop.
Adler32 checksum: .NET implementation
public class Adler32
{
public uint checksum = 1;
/// <summary>Performs the hash algorithm on given data array.</summary>
/// <param name="bytesArray">Input data.</param>
/// <param name="byteStart">The position to begin reading from.</param>
/// <param name="bytesToRead">How many bytes in the bytesArray to read.</param>
public void ComputeHash(byte[] bytesArray, int byteStart, int bytesToRead)
{
int n;
uint s1 = checksum & 0xFFFF;
uint s2 = checksum >> 16;
while (bytesToRead > 0)
{
n = (3800 > bytesToRead) ? bytesToRead : 3800;
bytesToRead -= n;
while (--n >= 0)
{
s1 = s1 + (uint)(bytesArray[byteStart++] & 0xFF);
s2 = s2 + s1;
}
s1 %= 65521;
s2 %= 65521;
}
checksum = (s2 << 16) | s1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment