简体   繁体   中英

How to generate Crc-64 table having all negative integer Constants and checksum?

I have some example code for Crc-64 Table generator. I tried to check the unsigned integer's sign and discovered that it generates mixed table Constants both negative & positive integers. Same for the Crc-64 Checksum, it may be negative or positive. Is it possible to implement a modified Crc-64 Table generator that should produce all negative signed Constants and also the Checksum? Or otherwise all positive signed Constants and Checksum. Kindly help me with some information and example implementation.

Here is the example code for the Crc-64 Table generator:

public class Crc64
{
    public const UInt64 POLYNOMIAL = 0xD800000000000000;
    public UInt64[] CRCTable;

    public Crc64()
    {
            this.CRCTable = new ulong[256];
            for (int i = 0; i <= 255; i++)
            {
                int j;
                UInt64 part = (UInt64)i;
                for (j = 0; j < 8; j++)
                {
                    if ((part & 1) != 0)
                        part = (part >> 1) ^ POLYNOMIAL;
                    else
                        part >>= 1;
                }
                CRCTable[i] = part;
            }
    }
}

UPDATE: Kindly inform, is this implementation correct as per my question:

public static List<UInt64> generateCrcTable(UInt64 POLY)
{
    const ulong TOPBIT_MASK = 0x8000000000000000;
    const ulong LOWBIT_MASK = 0x0000000000000001;

    List<UInt64> list = new List<UInt64>();

    for (int i = 0; i <= 255; i++)
    {
        UInt64 part = (UInt64)(i);            // << 56;

        for (byte bit = 0; bit < 63; bit++)
        {
            if ((part & LOWBIT_MASK) != 0) // 0x8000000000000000) != 0) //1) != 0) // 0x8000000000000000) != 0)
            {
                part >>= 1;
                part ^= POLY;
            }
            else
            {
                part >>= 1;
                //currentValue |= 0x8000000000000000;
            }
        }

        part |= TOPBIT_MASK; // 0x8000000000000000;

        list.Add(part);
    }
    return list;
}

I got code to work. No table. crc2 is always zero. I run CRC twice. Once to create CRC then add CRC to original data. Then run again to validate.

if you only want 63 bits then change the shift from 56 to 55. Then use mask starting with 0x4 instead of 0x8. And then on the return AND with 0x7FFFFFFFFFFFFFFF

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rand = new Random();

            List<byte> data = Enumerable.Range(0, 1000).Select(x => (byte)rand.Next(0,256)).ToList();
            ulong crc = Crc64.ComputeCrc64(data);
            List<byte> results = new List<byte>();
            for (int i = 7; i >= 0; i--)
            {
                results.Add((byte)((crc >> (8 * i)) & 0xFF));
            }
            data.AddRange(results);

            ulong crc2 = Crc64.ComputeCrc64(data);
        }
    }
    public class Crc64
    {
        const ulong POLYNOMIAL = 0xD800000000000000;
        public static ulong ComputeCrc64(List<byte> data)
        {
            ulong crc = 0; /* CRC value is 64bit */
            foreach (byte b in data)
            {
                crc ^= (ulong)b << 56; /* move byte into MSB of 63bit CRC */
                for (int i = 0; i < 8; i++)
                {
                    if ((crc & 0x8000000000000000) != 0) /* test for MSB = bit 63 */
                    {
                        crc = (ulong)((crc << 1) ^ POLYNOMIAL);
                    }
                    else
                    {
                        crc <<= 1;
                    }
                }
            }
            return crc;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM