Beispiel #1
0
        public byte[] Build()
        {
            var payload = this.BuildPayload();

            byte[] checksum;
            using (var crc = new Crc32(Crc32.DefaultPolynomialReversed, Crc32.DefaultSeed))
            {
                checksum = crc.ComputeHash(payload);
                Array.Reverse(checksum);
            }

            var payloadLen = Buffer.ByteLength(payload);
            var result = new byte[6 + payloadLen];
            Buffer.SetByte(result, 0, 0x42); // "B"
            Buffer.SetByte(result, 1, 0x45); // "E"
            Buffer.BlockCopy(checksum, 0, result, 2, 4);
            Buffer.BlockCopy(payload, 0, result, 6, payloadLen);

            return result;
        }
Beispiel #2
0
        /// <summary>
        ///   Verifies that the CRC32 checksum of the datagram being processed is valid.
        /// </summary>
        /// <returns> True if the CRC32 checsum is valid; false otherwise. </returns>
        public bool VerifyCrc()
        {
            int payloadLength = Buffer.ByteLength(this.buffer) - 6;
            var payload = new byte[payloadLength];
            Buffer.BlockCopy(this.buffer, 6, payload, 0, payloadLength);
            byte[] computedChecksum;
            using (var crc = new Crc32(Crc32.DefaultPolynomialReversed, Crc32.DefaultSeed))
            {
                computedChecksum = crc.ComputeHash(payload);
                Array.Reverse(computedChecksum);
            }

            var originalChecksum = new byte[4];
            Buffer.BlockCopy(this.buffer, 2, originalChecksum, 0, 4);

            return computedChecksum.SequenceEqual(originalChecksum);
        }