private static byte[] GenerateTable(Crc8Type type) { byte[] csTable = new byte[256]; for (int i = 0; i < 256; ++i) { int curr = i; for (int j = 0; j < 8; ++j) { if ((curr & 0x80) != 0) { curr = (curr << 1) ^ (int)type; } else { curr <<= 1; } } csTable[i] = (byte)curr; } return(csTable); }
public Crc8(Crc8Type type, Endianness endianness) { _polynomial = GetPolynomialByType(type); if (endianness == Endianness.Big) { _polynomial = FlipPolynomial(_polynomial); _endianness = endianness; } Reset(); }
/// <summary> /// Calculate crc /// </summary> /// <param name="type">Polinom type</param> /// <param name="source">source for calculation</param> /// <param name="length">num of elements</param> /// <param name="offset">array offset from begining</param> /// <returns></returns> public static byte Calculate(Crc8Type type, byte[] source, int length, int offset) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (source.Length < offset + length) { throw new ArgumentException($"{nameof(offset)} and {nameof(length)}"); } byte[] table = GenerateTable(type); byte c = 0; for (int i = 0; i < length; i++) { c = table[c ^ source[i + offset]]; } return(c); }
private byte GetPolynomialByType(Crc8Type type) { switch (type) { case Crc8Type.AUTOSAR: return(0xF4); case Crc8Type.Bluetooth: return(0xE5); case Crc8Type.CCITT: return(0xE0); case Crc8Type.Dallas: return(0x8C); case Crc8Type.DARC: return(0x9C); case Crc8Type.DVB: return(0xAB); case Crc8Type.GSM_B: return(0x92); case Crc8Type.Maxim: return(0x8C); case Crc8Type.SAE_J1850: return(0xB8); case Crc8Type.WCDMA: return(0xD9); default: throw new ArgumentOutOfRangeException("type", "The type parameter must be a valid Crc8Type."); } }
public Crc8(Crc8Type type) { _polynomial = GetPolynomialByType(type); Reset(); }