(C) Damien Guard http://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net
Inheritance: System.Security.Cryptography.HashAlgorithm
Ejemplo n.º 1
0
        /// <summary>
        /// Factory method for creating message with payload
        /// </summary>
        public static Message CreateMessage(byte[] payload)
        {
            using (var crc32 = new Crc32())
            {
                var message = new Message();
                message.Magic = 1;
                message.Payload = payload;
                message.Crc = crc32.ComputeHash(payload);

                return message;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Validate message by computing CRC32 hash for payload and comparing with Crc property.
        /// Throws exception if validation fails.
        /// </summary>
        public void Validate()
        {
            using (var crc32 = new Crc32())
            {
                var crc = crc32.ComputeHash(Payload);

                if (!ByteArraysEqual(crc, Crc))
                    throw new CorruptedMessageException();
            }
        }