Exemple #1
0
        public static unsafe uint CalculateCRCForChunk(string chunkType, byte[] chunkData)
        {
            byte[] chunkTypeBytes = Encoding.UTF8.GetBytes(chunkType);

            Crc32 crc32Calculator = new Crc32();

            crc32Calculator.Append(chunkTypeBytes);
            crc32Calculator.Append(chunkData);

            Span <byte> hashData = stackalloc byte[4];

            crc32Calculator.GetCurrentHash(hashData);
            return(BinaryPrimitives.ReadUInt32LittleEndian(hashData));
        }
Exemple #2
0
        /// <summary>
        /// Computes CRC-32 (IEEE 802.3) digest from given data.
        /// </summary>
        /// <param name="value">Unicode text for performing hash functions.</param>
        /// <param name="useAsciiEncoding">If True, ASCII encoding is used instead of Unicode.</param>
        public static int ComputeCrc(string value, bool useAsciiEncoding)
        {
            Crc32 crc = new Crc32();

            crc.Append(value, useAsciiEncoding);
            return(crc.Digest);
        }
Exemple #3
0
        /// <summary>
        /// Computes CRC-32 (IEEE 802.3) digest from given data.
        /// </summary>
        /// <param name="value">Value.</param>
        /// <param name="index">A 32-bit integer that represents the index at which data begins.</param>
        /// <param name="length">A 32-bit integer that represents the number of elements.</param>
        public static int ComputeCrc(byte[] value, int index, int length)
        {
            Crc32 crc = new Crc32();

            crc.Append(value, index, length);
            return(crc.Digest);
        }
Exemple #4
0
        /// <summary>
        /// Computes CRC-32 (IEEE 802.3) digest from given data.
        /// </summary>
        /// <param name="value">Value.</param>
        public static int ComputeCrc(byte[] value)
        {
            Crc32 crc = new Crc32();

            crc.Append(value);
            return(crc.Digest);
        }
Exemple #5
0
        public void Crc32_Ieee()
        {
            string expected  = "1A657BE2";
            Crc32  actualCrc = Crc32.GetIeee();

            actualCrc.Append("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", true);
            string actual = actualCrc.Digest.ToString("X8");

            Assert.AreEqual(expected, actual);
        }
Exemple #6
0
 public void TestCrc32(string name, uint polynomial, uint initialState, uint xorOutput, bool reverseInput, bool reverseOutput, uint check)
 {
     using (Crc32 crc = new Crc32(polynomial, initialState, xorOutput, reverseInput, reverseOutput))
     {
         crc.Initialize();
         var bytes = Encoding.UTF8.GetBytes(data);
         for (int i = 0; i < bytes.Length; i++)
         {
             crc.Append(bytes[i]);
         }
         uint output = crc.CurrentOutput;
         Assert.AreEqual(check, output, 0);
     }
 }