gpt4 book ai didi

c# - 没有等同于 CommonCrypto 的 CRC64 实现?

转载 作者:太空宇宙 更新时间:2023-11-03 21:23:11 32 4
gpt4 key购买 nike

我正在将一些代码从 OSX 上的 C 移植到使用 CommonCrypto 和 kCN_CRC_64_ECMA_182 CRC64 实现的 C#。例如,使用 CommonCrypto,CRC 将通过以下方式计算:

CNRCC(kCN_CRC_64_ECMA_182, bytes, bytesLen, &crcResult)

这会输出正确的值。当使用 C# 库 HashLib(或任何其他代码)时,输出是完全不同的,例如,上面使用 HashLib 的等价物是:

var checksum = HashFactory.Checksum.CreateCRC64(0x42F0E1EBA9EA3693UL);//ECMA 182
var result = checksum.ComputeBytes(bytes);

有什么想法吗?在 C# 中是否有一个在输出方面等同于 Apple 的 CommonCrypto 的实现?

最佳答案

下面是一些计算 ECMA-182 CRC 的简单 C 代码:

#include <stddef.h>
#include <stdint.h>

#define POLY UINT64_C(0x42f0e1eba9ea3693)
#define TOP UINT64_C(0x8000000000000000)

/* Return crc updated with buf[0..len-1]. If buf is NULL, return the initial
crc. So, initialize with crc = crc64_ecma182(0, NULL, 0); and follow with
one or more applications of crc = crc64_ecma182(crc, buf, len); */
int64_t crc64_ecma182(int64_t crc, unsigned char *buf, size_t len)
{
int k;

if (buf == NULL)
return 0;
while (len--) {
crc ^= (uint64_t)(*buf++) << 56;
for (k = 0; k < 8; k++)
crc = crc & TOP ? (crc << 1) ^ POLY : crc << 1;
}
return crc;
}

我认为 HashLib 完全错误,根据 what I found on github 判断.它做的是CRC reflected,而ECMA-182定义的CRC64没有reflected。

关于c# - 没有等同于 CommonCrypto 的 CRC64 实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29227892/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com