go.etcd.io/etcd@v3.3.27+incompatible/pkg/crc/crc.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 crc provides utility function for cyclic redundancy check 6 // algorithms. 7 package crc 8 9 import ( 10 "hash" 11 "hash/crc32" 12 ) 13 14 // The size of a CRC-32 checksum in bytes. 15 const Size = 4 16 17 type digest struct { 18 crc uint32 19 tab *crc32.Table 20 } 21 22 // New creates a new hash.Hash32 computing the CRC-32 checksum 23 // using the polynomial represented by the Table. 24 // Modified by xiangli to take a prevcrc. 25 func New(prev uint32, tab *crc32.Table) hash.Hash32 { return &digest{prev, tab} } 26 27 func (d *digest) Size() int { return Size } 28 29 func (d *digest) BlockSize() int { return 1 } 30 31 func (d *digest) Reset() { d.crc = 0 } 32 33 func (d *digest) Write(p []byte) (n int, err error) { 34 d.crc = crc32.Update(d.crc, d.tab, p) 35 return len(p), nil 36 } 37 38 func (d *digest) Sum32() uint32 { return d.crc } 39 40 func (d *digest) Sum(in []byte) []byte { 41 s := d.Sum32() 42 return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s)) 43 }