From http://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net
Inheritance: System.Security.Cryptography.HashAlgorithm
Example #1
0
        public void ParseFromValid()
        {
            Crc32 crc32 = new Crc32();

            string payload = "kafka";
            byte magic = 0;
            byte[] payloadData = Encoding.UTF8.GetBytes(payload);
            byte[] payloadSize = BitConverter.GetBytes(payloadData.Length);
            byte[] checksum = crc32.ComputeHash(payloadData);
            byte[] messageData = new byte[payloadData.Length + 1 + payloadSize.Length + checksum.Length];

            Buffer.BlockCopy(payloadSize, 0, messageData, 0, payloadSize.Length);
            messageData[4] = magic;
            Buffer.BlockCopy(checksum, 0, messageData, payloadSize.Length + 1, checksum.Length);
            Buffer.BlockCopy(payloadData, 0, messageData, payloadSize.Length + 1 + checksum.Length, payloadData.Length);

            Message message = Message.ParseFrom(messageData);

            Assert.IsNotNull(message);
            Assert.AreEqual(magic, message.Magic);
            Assert.IsTrue(payloadData.SequenceEqual(message.Payload));
            Assert.IsTrue(checksum.SequenceEqual(message.Checksum));
        }
Example #2
0
 /// <summary>
 /// Calculates the CRC32 checksum on the payload of the message.
 /// </summary>
 /// <returns>The checksum given the payload.</returns>
 private byte[] CalculateChecksum()
 {
     Crc32 crc32 = new Crc32();
     return crc32.ComputeHash(Payload);
 }
Example #3
0
 /// <summary>
 /// Calculates the CRC32 checksum on the payload of the message.
 /// </summary>
 /// <returns>The checksum given the payload.</returns>
 private byte[] CalculateChecksum()
 {
     Crc32 crc32 = new Crc32();
     return crc32.ComputeHash(GetMessageBytesWithoutCrc());
 }