Adler-32校验算法
Adler-32是Mark Adler发明的校验和算法,和32位CRC校验算法一样,都是保护数据防止意外更改的算法,但是这个算法较容易被伪造,所以是不安全的保护措施。但是比CRC好点的是,它计算的很快。这个算法那是从Fletcher校验和算法中修改过来的,原始的算法形式略快,但是可依赖性并不高。
namespace sdk.utils
{
public class Adler32
{
public static 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 static uint 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;
return checksum;
}
}
}