LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Package adler32 implements the Adler-32 checksum. | 5 // Package adler32 implements the Adler-32 checksum. |
6 // | 6 // |
7 // It is defined in RFC 1950: | 7 // It is defined in RFC 1950: |
8 // Adler-32 is composed of two sums accumulated per byte: s1 is | 8 // Adler-32 is composed of two sums accumulated per byte: s1 is |
9 // the sum of all bytes, s2 is the sum of all s1 values. Both sums | 9 // the sum of all bytes, s2 is the sum of all s1 values. Both sums |
10 // are done modulo 65521. s1 is initialized to 1, s2 to zero. The | 10 // are done modulo 65521. s1 is initialized to 1, s2 to zero. The |
11 // Adler-32 checksum is stored as s2*65536 + s1 in most- | 11 // Adler-32 checksum is stored as s2*65536 + s1 in most- |
12 // significant-byte first (network) order. | 12 // significant-byte first (network) order. |
13 package adler32 | 13 package adler32 |
14 | 14 |
15 import "hash" | 15 import "hash" |
16 | 16 |
17 const ( | 17 const ( |
18 // mod is the largest prime that is less than 65536. | 18 // mod is the largest prime that is less than 65536. |
19 mod = 65521 | 19 mod = 65521 |
20 // nmax is the largest n such that | 20 // nmax is the largest n such that |
21 // 255 * n * (n+1) / 2 + (n+1) * (mod-1) <= 2^32-1. | 21 // 255 * n * (n+1) / 2 + (n+1) * (mod-1) <= 2^32-1. |
| 22 // It is mentioned in RFC 1950 (search for "5552"). |
22 nmax = 5552 | 23 nmax = 5552 |
23 ) | 24 ) |
24 | 25 |
25 // The size of an Adler-32 checksum in bytes. | 26 // The size of an Adler-32 checksum in bytes. |
26 const Size = 4 | 27 const Size = 4 |
27 | 28 |
28 // digest represents the partial evaluation of a checksum. | 29 // digest represents the partial evaluation of a checksum. |
29 // The low 16 bits are s1, the high 16 bits are s2. | 30 // The low 16 bits are s1, the high 16 bits are s2. |
30 type digest uint32 | 31 type digest uint32 |
31 | 32 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 | 69 |
69 func (d *digest) Sum32() uint32 { return uint32(*d) } | 70 func (d *digest) Sum32() uint32 { return uint32(*d) } |
70 | 71 |
71 func (d *digest) Sum(in []byte) []byte { | 72 func (d *digest) Sum(in []byte) []byte { |
72 s := uint32(*d) | 73 s := uint32(*d) |
73 return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s)) | 74 return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s)) |
74 } | 75 } |
75 | 76 |
76 // Checksum returns the Adler-32 checksum of data. | 77 // Checksum returns the Adler-32 checksum of data. |
77 func Checksum(data []byte) uint32 { return uint32(update(1, data)) } | 78 func Checksum(data []byte) uint32 { return uint32(update(1, data)) } |
LEFT | RIGHT |