// Package crc64 implements the Avro CRC-64 checksum.// See https://avro.apache.org/docs/current/spec.html#schema_fingerprints for information.
package crc64import ()func init() {buildTable()}// Size is the of a CRC-64 checksum in bytes.constSize = 8// ByteOrder denotes how integers are encoded into bytes. The ByteOrder// interface in encoding/binary cancels some optimizations, so use a more// direct implementation.typeByteOrderint// ByteOrder constants.const (LittleEndianByteOrder = iotaBigEndian)// Empty is the empty checksum.constEmpty = 0xc15d213aa4d7a795// Table is a 256-word table representing the polynomial for efficient processing.typeTable [256]uint64func makeTable() *Table { := new(Table)for := range256 { := uint64()forrange8 { = ( >> 1) ^ (Empty & -( & 1)) } [] = }return}var crc64Table *Tablefunc buildTable() {crc64Table = makeTable()}type digest struct { crc uint64 tab *Table byteOrder ByteOrder}// New creates a new hash.Hash64 computing the Avro CRC-64 checksum.// Its Sum method will lay the value out in big-endian byte order.func () hash.Hash64 {returnnewDigest(BigEndian)}// NewWithByteOrder creates a new hash.Hash64 computing the Avro CRC-64// checksum. Its Sum method will lay the value out in specified byte order.func ( ByteOrder) hash.Hash64 {returnnewDigest()}func newDigest( ByteOrder) *digest {return &digest{crc: Empty,tab: crc64Table,byteOrder: , }}// Size returns the bytes size of the checksum.func ( *digest) () int {returnSize}// BlockSize returns the block size of the checksum.func ( *digest) () int {return1}// Reset resets the hash instance.func ( *digest) () { .crc = Empty}// Write accumulatively adds the given data to the checksum.func ( *digest) ( []byte) ( int, error) {for := range { .crc = (.crc >> 8) ^ .tab[(int)(byte(.crc)^[])&0xff] }returnlen(), nil}// Sum64 returns the checksum as a uint64.func ( *digest) () uint64 {return .crc}// Sum returns the checksum as a byte slice, using the given byte slice.func ( *digest) ( []byte) []byte { := .sumBytes()returnappend(, [:]...)}// sumBytes returns the checksum as a byte array in digest byte order.func ( *digest) () [Size]byte { := .Sum64()switch .byteOrder {caseLittleEndian:return [Size]byte{byte(),byte( >> 8),byte( >> 16),byte( >> 24),byte( >> 32),byte( >> 40),byte( >> 48),byte( >> 56), }caseBigEndian:return [Size]byte{byte( >> 56),byte( >> 48),byte( >> 40),byte( >> 32),byte( >> 24),byte( >> 16),byte( >> 8),byte(), } }panic("unknown byte order")}// Sum returns the CRC64 checksum of the data, in big-endian byte order.func ( []byte) [Size]byte {returnSumWithByteOrder(, BigEndian)}// SumWithByteOrder returns the CRC64 checksum of the data, in specified byte// order.func ( []byte, ByteOrder) [Size]byte { := newDigest() _, _ = .Write()return .sumBytes()}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.