1
0
Protocol/crypto/crc.go
2025-09-02 10:18:09 -05:00

33 lines
736 B
Go

package crypto
import (
"encoding/binary"
"hash/crc32"
)
// Standard CRC32 polynomial
var crcTable = crc32.MakeTable(0xEDB88320)
// Fixed key used by EQ2
var EQ2CRCKey = 0x33624702
// CalculateCRC gets the CRC for a given byte slice using a custom key
func CalculateCRC(data []byte, key uint32) uint16 {
// Pre-process the key (4 rounds of CRC on key bytes)
crc := uint32(0xFFFFFFFF)
keyBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(keyBytes, key)
for _, b := range keyBytes {
crc = crcTable[(crc^uint32(b))&0xFF] ^ (crc >> 8)
}
// Process actual data
for _, b := range data {
crc = crcTable[(crc^uint32(b))&0xFF] ^ (crc >> 8)
}
// Return lower 16 bits of inverted result
return uint16(^crc & 0xFFFF)
}