github.com/richardwilkes/toolbox@v1.121.0/xmath/crc/crc.go (about) 1 // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved. 2 // 3 // This Source Code Form is subject to the terms of the Mozilla Public 4 // License, version 2.0. If a copy of the MPL was not distributed with 5 // this file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 // 7 // This Source Code Form is "Incompatible With Secondary Licenses", as 8 // defined by the Mozilla Public License, version 2.0. 9 10 package crc 11 12 import ( 13 "hash/crc64" 14 ) 15 16 var crcTable = crc64.MakeTable(crc64.ECMA) 17 18 // Bool returns the CRC-64 value for the given data starting with the given crc value. 19 func Bool(crc uint64, b bool) uint64 { 20 var data [1]byte 21 if b { 22 data[0] = 1 23 } 24 return crc64.Update(crc, crcTable, data[:]) 25 } 26 27 // Bytes returns the CRC-64 value for the given data starting with the given crc value. 28 func Bytes(crc uint64, data []byte) uint64 { 29 return crc64.Update(crc, crcTable, data) 30 } 31 32 // String returns the CRC-64 value for the given data starting with the given crc value. 33 func String(crc uint64, data string) uint64 { 34 return crc64.Update(crc, crcTable, []byte(data)) 35 } 36 37 // Byte returns the CRC-64 value for the given data starting with the given crc value. 38 func Byte(crc uint64, data byte) uint64 { 39 var buffer [1]byte 40 buffer[0] = data 41 return crc64.Update(crc, crcTable, buffer[:]) 42 } 43 44 // Number returns the CRC-64 value for the given data starting with the given crc value. 45 func Number[T ~int64 | ~uint64 | ~int | ~uint](crc uint64, data T) uint64 { 46 var buffer [8]byte 47 d := uint64(data) 48 buffer[0] = byte(d) 49 buffer[1] = byte(d >> 8) 50 buffer[2] = byte(d >> 16) 51 buffer[3] = byte(d >> 24) 52 buffer[4] = byte(d >> 32) 53 buffer[5] = byte(d >> 40) 54 buffer[6] = byte(d >> 48) 55 buffer[7] = byte(d >> 56) 56 return crc64.Update(crc, crcTable, buffer[:]) 57 }