Search code examples
c#cryptographymifarecrc32desfire

C# Crc32 implementation


I´ve got the following example:

3D020000000F0000112233445566778899AABBCCDDEEFF - Crc32 Input
280ACDA5 - Crc32 Result

But with the following Calculators: sunshine2k and zorc.breitbandkatze I can´t reconstruct the result. The documentation says: Polynomial-04C11DB7, Reverse-EDB88320, Initvalue-FFFFFFFF and also the Result should not be reversed.

I´ve searched for libraries on NuGet and found Nito but do not get the right result either. I´ve also tried every combination with flags and polynomials (04C11DB7, EDB88320).

Can someone tell what I´m doing wrong? Are the calculators and the lib wrong? Also I´ve tried same combination of polinom and flags on both sides and the lib but got different result :-( may someone tell a lib which is definatly reight?

Here my Testcode:

        [Theory]
        [InlineData("3D 00 00 00 00 10 00 00 23 00 00 00 00 00 00 08 12 34 56 78 00 00 00 00", "7B 8A 60 0F")]
        [InlineData("3D020000000F0000112233445566778899AABBCCDDEEFF", "280ACDA5")]
        public void Crc32_Test(string input, string assumedCrc32)
        {
            var definition = new Definition
            {
                Initializer = 0xFFFFFFFF,
                TruncatedPolynomial = 0xEDB88320,
                ReverseResultBeforeFinalXor = false,
                ReverseDataBytes = false,
            };

            var whow = new Nito.HashAlgorithms.CRC32(definition);

            var inputArray = DESFireSessionHandler.StringToByte(input);
            var assumedCrc32Array = DESFireSessionHandler.StringToByte(assumedCrc32);

            var crc32 = whow.ComputeHash(inputArray);

            if (crc32.SequenceEqual(assumedCrc32Array))
                Assert.True(true);
            else if (crc32.Reverse().SequenceEqual(assumedCrc32Array))
                Assert.True(true);
            else Assert.False(true);
}

Solution

  • The following parameters produce the desired result 280ACDA5:

    Initializer = 0xFFFFFFFF,
    TruncatedPolynomial = 0x04C11DB7,
    FinalXorValue = 0x00000000,
    ReverseResultBeforeFinalXor = true,
    ReverseDataBytes = true
    

    This is identical (except for the order of the bytes) to the online tools:

    https://crccalc.com/

    enter image description here

    http://www.sunshine2k.de/coding/javascript/crc/crc_js.html

    enter image description here


    The complete C# code is:

    using System;
    using static Nito.HashAlgorithms.CRC32;
                        
    public class Program
    {
        public static void Main()
        {
            var definition = new Definition
            {
                Initializer = 0xFFFFFFFF,
                TruncatedPolynomial = 0x04C11DB7,
                FinalXorValue = 0x00000000,
                ReverseResultBeforeFinalXor = true,
                ReverseDataBytes = true
            };
    
            var inputHex = "3D020000000F0000112233445566778899AABBCCDDEEFF";
            var input = Convert.FromHexString(inputHex);
            var whow = new Nito.HashAlgorithms.CRC32(definition);
            
            var crc32 = whow.ComputeHash(input);
            var crc32Hex = Convert.ToHexString(crc32);
            Console.WriteLine(crc32Hex); // 280ACDA5
        }
    }