From http://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net
Inheritance: System.Security.Cryptography.HashAlgorithm
Example #1
0
        public static byte[] Compute(byte[] bytes)
        {
            var hasher = new Crc32Hasher();
            var hash   = hasher.ComputeHash(bytes);

            return(hash);
        }
Example #2
0
        public void ParseFromValid()
        {
            Crc32Hasher crc32 = new Crc32Hasher();

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

            Buffer.BlockCopy(payloadSize, 0, messageData, 0, payloadSize.Length);
            messageData[4] = magic;
            messageData[5] = attributes;
            Buffer.BlockCopy(checksum, 0, messageData, payloadSize.Length + 2, checksum.Length);
            Buffer.BlockCopy(payloadData, 0, messageData, payloadSize.Length + 2 + 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 #3
0
 public static byte[] Compute(byte[] bytes)
 {
     var hasher = new Crc32Hasher();
     byte[] hash = hasher.ComputeHash(bytes);
     return hash;
 }
 internal static uint ComputeCrcUint32(byte[] bytes, int offset, int count)
 {
     var hasher = new Crc32Hasher();
     return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, bytes, offset, count);
 }
Example #5
0
        internal static uint ComputeCrcUint32(byte[] bytes, int offset, int count)
        {
            var hasher = new Crc32Hasher();

            return(~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, bytes, offset, count));
        }