namespace skyscraper8.GSE;

public static class DvbCrc8
{
    private const byte Polynomial = 0xD5; // x^8 + x^7 + x^6 + x^4 + x^2 + 1

    public static byte Compute(ReadOnlySpan<byte> data)
    {
        byte crc = 0x00;

        foreach (byte b in data)
        {
            crc ^= b; // XOR byte into top of crc register

            for (int i = 0; i < 8; i++)
            {
                if ((crc & 0x80) != 0)
                    crc = (byte)((crc << 1) ^ Polynomial);
                else
                    crc <<= 1;
            }
        }

        return crc;
    }
}