## 算法

```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
```

## 示例

ASCII字符串“ `Wikipedia` ”的Adler-32校验和计算如下：

(显示为10进制)
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 =  0x398  (16进制)
B = 4582 = 0x11E6

```

## 实现示例

C语言中，一个低效但直接的实现方式是：

```const uint32_t MOD_ADLER = 65521;

uint32_t adler32(unsigned char *data, size_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;
size_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;
}
```

## 优点和缺点

• 在许多平台上，它都比CRC-32更快。[4]

## 脚注

1. ^ First appearance of Adler-32 (see ChangeLog and adler32.c). [2020-06-12]. （原始内容存档于2020-09-17）.
2. ^ Revisiting Fletcher and Adler Checksums (PDF). [2020-06-12]. （原始内容存档 (PDF)于2020-09-17）.
3. ^ adler32.js. Sheet JS. 3 July 2019.
4. ^ Theresa C. Maxino, Philip J. Koopman. The Effectiveness of Checksums for Embedded Control Networks (PDF). IEEE Transactions on Dependable and Secure Computing. January 2009 [2020-06-12]. （原始内容存档 (PDF)于2013-10-21）.
5. ^
6. ^ 存档副本. [2020-06-12]. （原始内容存档于2012-11-29）.
7. ^ 存档副本. [2020-06-12]. （原始内容存档于2020-06-12）.