Adler-32校验算法
Adler-32是Mark Adler发明的校验和算法,和32位CRC校验算法一样,都是保护数据防止意外更改的算法,但是这个算法较容易被伪造,所以是不安全的保护措施。但是比CRC好点的是,它计算的很快。这个算法那是从Fletcher校验和算法中修改过来的,原始的算法形式略快,但是可依赖性并不高。
Adler-32的一种滚动哈希版本被用在了rsync工具中
Adler-32通过求解两个16位的数值A、B实现,并将结果连结成一个32位整数.
A就是字符串中每个字节的和,而B是A在相加时每一步的阶段值之和。在Adler-32开始运行时,A初始化为1,B初始化为0,最后的校验和要模上65521(继216之后的最小素数)。
A = 1 + D1 + D2 + ... + Dn (mod 65521) B = (1 + D1) + (1 + D1 + D2) + ... + (1 + D1 + D2 + ... + Dn) (mod 65521) = n×D1 + (n-1)×D2 + (n-2)×D3 + ... + Dn + n (mod 65521) Adler-32(D) = B × 65536 + A 其中D为字符串的字节,n是D的字节长度
下面举例使用Adler-32校验算法产生字符串"Wikipedia"的校验和:
ASCII code A B W: 87 1 + 87 = 88 0 + 88 = 88 i: 105 88 + 105 = 193 88 + 193 = 281 k: 107 193 + 107 = 300 281 + 300 = 581 i: 105 300 + 105 = 405 581 + 405 = 986 p: 112 405 + 112 = 517 986 + 517 = 1503 e: 101 517 + 101 = 618 1503 + 618 = 2121 d: 100 618 + 100 = 718 2121 + 718 = 2839 i: 105 718 + 105 = 823 2839 + 823 = 3662 a: 97 823 + 97 = 920 3662 + 920 = 4582 A = 920 = 398 hex B = 4582 = 11E6 hex Output: 11E60398 hex 都默默的进行过了模65521操作了
例子(一个不高效,但是很直接的实现):
const int MOD_ADLER = 65521; uint32_t adler32(unsigned char *data, int32_t len) /* where data is the location of the data in physical memory and len is the length of the data in bytes */ { uint32_t a = 1, b = 0; int32_t index; /* Process each byte of the data in order */ for (index = 0; index < len; ++index) { a = (a + data[index]) % MOD_ADLER; b = (b + a) % MOD_ADLER; } return (b << 16) | a; }
Advantages and disadvantages[edit]Like the standard CRC-32, the Adler-32 checksum can be forged easily and is therefore unsafe for protecting against intentional modification.It's faster than CRC-32 on many platforms.Adler-32 has a weakness for short messages with few hundred bytes, because the checksums for these messages have a poor coverage of the 32 available bits.