-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrc32.go
32 lines (26 loc) · 899 Bytes
/
crc32.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package iotc4i
import "hash/crc32"
// CalculateCRC32 calculates the CRC32 checksum of the given data using a specified polynomial.
func CalculateCRC32(data []byte) uint32 {
// Create a new CRC32 table based on the polynomial
table := crc32.MakeTable(crc32.IEEE)
// Calculate the CRC32 checksum
checksum := crc32.Checksum(data, table)
return checksum
}
func CalculateHashWithZerofill(data []byte, fields []Field) uint32 {
zerofilledData := make([]byte, len(data))
// Zero-fill the specified fields to calculate the CRC32 checksum
copy(zerofilledData, data)
for _, field := range fields {
if field.Zerofill {
for i := field.StartIdx; i <= field.EndIdx; i++ {
zerofilledData[i] = 0
}
}
}
zerofilledData[len(zerofilledData)-4] = 0
// Calculate the CRC32 checksum of the zero-filled data
hash := CalculateCRC32(zerofilledData[1 : len(zerofilledData)-4+1])
return hash
}