Last active
January 3, 2016 07:59
-
-
Save carlozamagni/8432805 to your computer and use it in GitHub Desktop.
Adler32 checksum: .NET implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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