package crypto

import (
	"encoding/binary"
	"hash/crc32"
)

// EQ2CRCKey is likely the reverse engineered security key for the protocol
const EQ2CRCKey uint32 = 0x33624702

// CalculateCRC computes CRC32( little_endian(key) || data ) and returns the low 16 bits
// of the final ~CRC, matching the EQ2 behavior
func CalculateCRC(data []byte, key uint32) uint16 {
	crc := ^uint32(0)

	var k [4]byte
	binary.LittleEndian.PutUint32(k[:], key)

	crc = crc32.Update(crc, crc32.IEEETable, k[:])
	crc = crc32.Update(crc, crc32.IEEETable, data)

	return uint16(^crc)
}
