我有一些Crc-64表生成器的示例代码。我尝试检查无符号整数的符号,发现它会生成混合表常量(负整数和正整数)。与Crc-64校验和相同,它可以是负数或正数。是否有可能实现一个修改后的Crc-64表生成器,该生成器应生成所有带负号的常量以及校验和?否则,所有正签名的常量和校验和。请帮助我提供一些信息和示例实现。
这是Crc-64表生成器的示例代码:
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;
}
}
}
更新:请告知,根据我的问题,此实现是否正确:
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;
}
答案 0 :(得分:0)
我有代码可以工作。没有桌子crc2始终为零。我运行了两次CRC。一旦创建了CRC,然后将CRC添加到原始数据。然后再次运行以进行验证。
如果仅需要63位,则将移位从56更改为55。然后使用以0x4而不是0x8开头的掩码。然后返回AND与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;
}
}
}