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. 7package crc 8 9import ( 10 "hash" 11 "hash/crc32" 12) 13 14// The size of a CRC-32 checksum in bytes. 15const Size = 4 16 17type 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. 25func New(prev uint32, tab *crc32.Table) hash.Hash32 { return &digest{prev, tab} } 26 27func (d *digest) Size() int { return Size } 28 29func (d *digest) BlockSize() int { return 1 } 30 31func (d *digest) Reset() { d.crc = 0 } 32 33func (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 38func (d *digest) Sum32() uint32 { return d.crc } 39 40func (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} 44