github.com/petermattis/pebble@v0.0.0-20190905164901-ab51a2166067/internal/crc/crc.go (about) 1 // Copyright 2011 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 // Package crc implements the checksum algorithm used throughout pebble. 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 of arbitrary key/value data coincidentally containing bytes that 10 // look like a checksum. 11 // 12 // To calculate the uint32 checksum of some data: 13 // var u uint32 = crc.New(data).Value() 14 // In pebble, the uint32 value is then stored in little-endian format. 15 package crc // import "github.com/petermattis/pebble/internal/crc" 16 17 import ( 18 "hash/crc32" 19 ) 20 21 var table = crc32.MakeTable(crc32.Castagnoli) 22 23 type CRC uint32 24 25 func New(b []byte) CRC { 26 return CRC(0).Update(b) 27 } 28 29 func (c CRC) Update(b []byte) CRC { 30 return CRC(crc32.Update(uint32(c), table, b)) 31 } 32 33 func (c CRC) Value() uint32 { 34 return uint32(c>>15|c<<17) + 0xa282ead8 35 }