OLD | NEW |
(Empty) | |
| 1 // Copyright 2011 The Snappy-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 implements the checksum used in snappy's frame format. |
| 6 // |
| 7 // The algorithm is CRC-32 with Castagnoli's polynomial, followed by a bit |
| 8 // rotation and an additional delta. The additional processing is to lessen the |
| 9 // probability that arbitrary data coincidentally contains bytes that look like |
| 10 // a checksum. |
| 11 // |
| 12 // To calculate the uint32 checksum of some data: |
| 13 // var u uint32 = crc.New(data).Value() |
| 14 package crc |
| 15 |
| 16 import ( |
| 17 "hash/crc32" |
| 18 ) |
| 19 |
| 20 var table = crc32.MakeTable(crc32.Castagnoli) |
| 21 |
| 22 type CRC uint32 |
| 23 |
| 24 func New(b []byte) CRC { |
| 25 return CRC(0).Update(b) |
| 26 } |
| 27 |
| 28 func (c CRC) Update(b []byte) CRC { |
| 29 return CRC(crc32.Update(uint32(c), table, b)) |
| 30 } |
| 31 |
| 32 func (c CRC) Value() uint32 { |
| 33 return uint32(c>>15|c<<17) + 0xa282ead8 |
| 34 } |
OLD | NEW |