github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/hash/adler32/adler32.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package adler32 implements the Adler-32 checksum. 6 // 7 // It is defined in RFC 1950: 8 // 9 // Adler-32 is composed of two sums accumulated per byte: s1 is 10 // the sum of all bytes, s2 is the sum of all s1 values. Both sums 11 // are done modulo 65521. s1 is initialized to 1, s2 to zero. The 12 // Adler-32 checksum is stored as s2*65536 + s1 in most- 13 // significant-byte first (network) order. 14 package adler32 15 16 import ( 17 "errors" 18 "hash" 19 ) 20 21 const ( 22 // mod is the largest prime that is less than 65536. 23 mod = 65521 24 // nmax is the largest n such that 25 // 255 * n * (n+1) / 2 + (n+1) * (mod-1) <= 2^32-1. 26 // It is mentioned in RFC 1950 (search for "5552"). 27 nmax = 5552 28 ) 29 30 // The size of an Adler-32 checksum in bytes. 31 const Size = 4 32 33 // digest represents the partial evaluation of a checksum. 34 // The low 16 bits are s1, the high 16 bits are s2. 35 type digest uint32 36 37 func (d *digest) Reset() { *d = 1 } 38 39 // New returns a new hash.Hash32 computing the Adler-32 checksum. Its 40 // Sum method will lay the value out in big-endian byte order. The 41 // returned Hash32 also implements encoding.BinaryMarshaler and 42 // encoding.BinaryUnmarshaler to marshal and unmarshal the internal 43 // state of the hash. 44 func New() hash.Hash32 { 45 d := new(digest) 46 d.Reset() 47 return d 48 } 49 50 func (d *digest) Size() int { return Size } 51 52 func (d *digest) BlockSize() int { return 4 } 53 54 const ( 55 magic = "adl\x01" 56 marshaledSize = len(magic) + 4 57 ) 58 59 func (d *digest) MarshalBinary() ([]byte, error) { 60 b := make([]byte, 0, marshaledSize) 61 b = append(b, magic...) 62 b = appendUint32(b, uint32(*d)) 63 return b, nil 64 } 65 66 func (d *digest) UnmarshalBinary(b []byte) error { 67 if len(b) < len(magic) || string(b[:len(magic)]) != magic { 68 return errors.New("hash/adler32: invalid hash state identifier") 69 } 70 if len(b) != marshaledSize { 71 return errors.New("hash/adler32: invalid hash state size") 72 } 73 *d = digest(readUint32(b[len(magic):])) 74 return nil 75 } 76 77 func appendUint32(b []byte, x uint32) []byte { 78 a := [4]byte{ 79 byte(x >> 24), 80 byte(x >> 16), 81 byte(x >> 8), 82 byte(x), 83 } 84 return append(b, a[:]...) 85 } 86 87 func readUint32(b []byte) uint32 { 88 _ = b[3] 89 return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24 90 } 91 92 // Add p to the running checksum d. 93 func update(d digest, p []byte) digest { 94 s1, s2 := uint32(d&0xffff), uint32(d>>16) 95 for len(p) > 0 { 96 var q []byte 97 if len(p) > nmax { 98 p, q = p[:nmax], p[nmax:] 99 } 100 for len(p) >= 4 { 101 s1 += uint32(p[0]) 102 s2 += s1 103 s1 += uint32(p[1]) 104 s2 += s1 105 s1 += uint32(p[2]) 106 s2 += s1 107 s1 += uint32(p[3]) 108 s2 += s1 109 p = p[4:] 110 } 111 for _, x := range p { 112 s1 += uint32(x) 113 s2 += s1 114 } 115 s1 %= mod 116 s2 %= mod 117 p = q 118 } 119 return digest(s2<<16 | s1) 120 } 121 122 func (d *digest) Write(p []byte) (nn int, err error) { 123 *d = update(*d, p) 124 return len(p), nil 125 } 126 127 func (d *digest) Sum32() uint32 { return uint32(*d) } 128 129 func (d *digest) Sum(in []byte) []byte { 130 s := uint32(*d) 131 return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s)) 132 } 133 134 // Checksum returns the Adler-32 checksum of data. 135 func Checksum(data []byte) uint32 { return uint32(update(1, data)) }