Native CRC

I don’t suppose we could get, or is there already a native CRC function? CRC16 would be dandy.

Thanks

http://docs.ghielectronics.com/software/tinyclr/tutorials/hashing.html

Are you planning to add crc32 implementation?

We actually already have it internally. Not sure why we didn’t expose it.

Please expose it.

2 Likes

Are you expose native crc32 in release 2.0 ?

Can you use MD5 or your project require has to be CRC?

I was planning to use CRC

Sorry, it is too late in v2.0.0.

In 2.0.1 maybe?

99% is Yes.

Why not use md5?

in managed code


//https://stackoverflow.com/questions/8128/how-do-i-calculate-crc32-of-a-string?noredirect=1&lq=1

using System;

namespace TinyCLRCRC32
{
     class CRC32
    {
        private uint[] ChecksumTable;
        private uint Polynomial = 0xEDB88320;

        public CRC32()
        {
            ChecksumTable = new uint[0x100];

            for (uint index = 0; index < 0x100; ++index)
            {
                uint item = index;
                for (int bit = 0; bit < 8; ++bit)
                    item = ((item & 1) != 0) ? (Polynomial ^ (item >> 1)) : (item >> 1);
                ChecksumTable[index] = item;
            }
        }

        public byte[] ComputeHash(byte[] stream)
        {
            UInt32 result = 0xFFFFFFFF;
            int current;

            for (int i = 0; i < stream.Length; i++)
            {
                current = stream[i];
                result = ChecksumTable[(result & 0xFF) ^ (byte)current] ^ (result >> 8);
            }

            byte[] hash = BitConverter.GetBytes(~result);
            byte[] reverseHash = Reverse(hash);

            return Reverse(reverseHash);
        }

        private byte[] Reverse(byte[] Array)
        {
            byte[] toReverse = new byte[Array.Length];

            int j = 0;

            for (int i = Array.Length - 1; i >= 0; i--)
            {
                toReverse[j] = Array[i];
                j++;
            }
            return toReverse;
        }
    }
}

usage


using System;
using System.Diagnostics;
using System.Text;

namespace TinyCLRCRC32
{
    class Program
    {
        static void Main()
        {
            Debug.WriteLine("CRC32 Test 1!");

            var crc = new CRC32();

            // Convert a C# string to a byte array  
            string author = "Valon Hoti";


            // Convert a C# string to a byte array  
            ////convert binary file to byte[] ...
            byte[] bytes = Encoding.UTF8.GetBytes(author);
            string myRes = "";
            foreach (byte s in crc.ComputeHash(bytes))
            {
                myRes += s.ToString("x2").ToUpper();
            }

            Debug.WriteLine(myRes); //need to reverse it because MCU and DESKTOP act differently
            Debug.WriteLine(stringReverseString(myRes));

            while(true)
            {

            }
        }

    public static string stringReverseString(string str)
        {
            char[] chars = str.ToCharArray();
            for (int i = 0, j = str.Length - 1; i < j; i++, j--)
            {
                char c = chars[i];
                chars[i] = chars[j];
                chars[j] = c;
            }
            return new string(chars);
        }
    }
}

REMARKS.
**TESTED ON DESKTOP AND TINYCLR but results are different **

CODE

@Dat_Tran , @Gus_Issa

      var crc = new CRC32();

            // Convert a C# string to a byte array  
            string author = "Valon Hoti";

         ////convert binary file to byte[] ...
            byte[] bytes = Encoding.UTF8.GetBytes(author);
            string myRes = "";
            foreach (byte s in crc.ComputeHash(bytes))
            {
                myRes += s.ToString("x2").ToUpper();
            }

            Debug.WriteLine(myRes);

gave

result : 770B5A89 on DESKTOP
result: 895A0B77 on TINYCLR

after i add stringReverseString to correct it

1 Like

but slower than native

Correct but SITCore is 30 times faster than G120

1 Like

In 2.1.0 - Preview1 I can’t find native crc32 implementation

It is Crc16 only for now.

            var crc16 = new Crc16();

            var data = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

            var crcVal1 = crc16.ComputeHash(data, 0, data.Length);
            var crcVal2 = crc16.ComputeHash(data, 0, data.Length);

            crc16.Reset();

           var crcVal3 = crc16.ComputeHash(data, 0, data.Length); // After Reset(), crcVal3 same as crcVal1 
1 Like

Maybe we will add CRC32 but we now have CRC16 for fast needs and MD5 for robust needs. Why is the need for CRC32?

I use it in old projects to control the integrity of the coefficients stored in EEPROM

Then why not use MD5? I am just curious here.