Ejemplo n.º 1
0
        public static byte[] CalculateCRC(IEnumerable <byte> bytes, uint crcInitialValue = 0x0, CRCType crcPolynomial = CRCType.CRC16CCITTPolynomial)
        {
            uint      crc       = crcInitialValue;
            CRCEngine crcEngine = new CRCEngine(crcPolynomial);
            var       crcLength = crcPolynomial.GetLength();

            // Byte little endian order
            switch (crcLength)
            {
            case 2:
                crc = crcEngine.CalculateCrc16(bytes, (ushort)crc);
                return(new[] { (byte)crc, (byte)(crc >> 8) });

            case 4:
                crc = crcEngine.CalculateCrc32(bytes, crc);
                return(new[] { (byte)crc, (byte)(crc >> 8), (byte)(crc >> 16), (byte)(crc >> 24) });

            default:
                Logger.Log(LogLevel.Error, "Cannot calculate CRC of invalid length {0}", crcLength);
                return(new byte[crcLength]);
            }
        }
Ejemplo n.º 2
0
 public CRCEngine(CRCType polynomial)
 {
     this.reversedPolynomial = polynomial.GetLength() == 4
         ? BitHelper.ReverseBits((uint)polynomial)
         : BitHelper.ReverseBits((ushort)polynomial); // this cast is needed to rotate only lower bits
 }