public static void CRC32(byte[] buffer, UInt32 size, byte[] crc_byte) { UInt32 crc = 0; UInt32 index = 0; if ((null != buffer) && (size > 0)) { while (0 < size--) { crc = (UInt32)(crc ^ (UInt32)buffer[index++] << 8); for (UInt16 i = 0; i < 8; i++) { if (0 != (crc & 0x8000)) { crc = crc << 1 ^ 0x1021; } else { crc = crc << 1; } } } } crc = crc & 0x0000FFFF; crc_byte[0] = (byte)crc; crc_byte[1] = (byte)(crc >> 8); crc_byte[2] = (byte)(crc >> 16); crc_byte[3] = (byte)(crc >> 24); } 转为python语言,用crcmod库的mkcrcfun进行实现
时间: 2024-02-10 18:13:39 浏览: 314
可以使用Python的crcmod库来实现相同的CRC32算法,代码如下:
```python
import crcmod
def CRC32(buffer, size, crc_byte):
crc_func = crcmod.mkCrcFun(0x104C11DB7, initCrc=0, rev=True, xorOut=0xFFFFFFFF) # 定义CRC函数
crc = crc_func(buffer[:size]) # 计算CRC值
crc_byte[0] = crc & 0xFF
crc_byte[1] = (crc >> 8) & 0xFF
crc_byte[2] = (crc >> 16) & 0xFF
crc_byte[3] = (crc >> 24) & 0xFF
```
需要注意的是,Python的crcmod库使用的是反转多项式,因此在定义CRC函数时需要将多项式反转,并将rev参数设置为True。同时,输入数据不需要反转,输出数据需要反转,因此需要在计算CRC值后对结果进行反转。
相关问题
public static void CRC32(byte[] buffer, UInt32 size, byte[] crc_byte) { UInt32 crc = 0; UInt32 index = 0; if ((null != buffer) && (size > 0)) { while (0 < size--) { crc = (UInt32)(crc ^ (UInt32)buffer[index++] << 8); for (UInt16 i = 0; i < 8; i++) { if (0 != (crc & 0x8000)) { crc = crc << 1 ^ 0x1021; } else { crc = crc << 1; } } } } crc = crc & 0x0000FFFF; crc_byte[0] = (byte)crc; crc_byte[1] = (byte)(crc >> 8); crc_byte[2] = (byte)(crc >> 16); crc_byte[3] = (byte)(crc >> 24); } C3转为python
以下是将该C#代码转换为Python的代码:
def CRC32(buffer, size, crc_byte):
crc = 0
index = 0
if buffer and size > 0:
while size > 0:
size -= 1
crc ^= buffer[index] << 8
index += 1
for i in range(8):
if crc & 0x8000:
crc = (crc << 1) ^ 0x1021
else:
crc = crc << 1
crc &= 0x0000FFFF
crc_byte[0] = crc & 0xFF
crc_byte[1] = (crc >> 8) & 0xFF
crc_byte[2] = (crc >> 16) & 0xFF
crc_byte[3] = (crc >> 24) & 0xFF
C# CRC32 算法
CRC32(循环冗余校验)算法是一种常用的数据校验算法,它可以用于检测数据传输过程中是否出现了错误。
在 C# 中,可以使用 System.Security.Cryptography 命名空间下的 CRC32 类来实现 CRC32 算法。下面是一个简单的示例代码:
```csharp
using System;
using System.Security.Cryptography;
public class CRC32Example
{
public static string GetCRC32(string input)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input);
using (var crc32 = new CRC32())
{
byte[] hash = crc32.ComputeHash(bytes);
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
}
}
public class CRC32 : HashAlgorithm
{
private const uint DefaultPolynomial = 0xedb88320u;
private const uint DefaultSeed = 0xffffffffu;
private uint hash;
private readonly uint seed;
private readonly uint[] table;
private static uint[] defaultTable;
public CRC32()
{
table = InitializeTable(DefaultPolynomial);
seed = DefaultSeed;
Initialize();
}
public CRC32(uint polynomial, uint seed)
{
table = InitializeTable(polynomial);
this.seed = seed;
Initialize();
}
public override void Initialize()
{
hash = seed;
}
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
hash = CalculateHash(table, hash, array, ibStart, cbSize);
}
protected override byte[] HashFinal()
{
byte[] hashBuffer = UInt32ToBigEndianBytes(~hash);
this.HashValue = hashBuffer;
return hashBuffer;
}
public override int HashSize => 32;
private static uint[] InitializeTable(uint polynomial)
{
if (polynomial == DefaultPolynomial && defaultTable != null)
return defaultTable;
uint[] createTable = new uint[256];
for (int i = 0; i < 256; i++)
{
uint entry = (uint)i;
for (int j = 0; j < 8; j++)
if ((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial;
else
entry = entry >> 1;
createTable[i] = entry;
}
if (polynomial == DefaultPolynomial)
defaultTable = createTable;
return createTable;
}
private static uint CalculateHash(uint[] table, uint seed, byte[] buffer, int start, int size)
{
uint crc = seed;
for (int i = start; i < size; i++)
unchecked
{
crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
}
return crc;
}
private static byte[] UInt32ToBigEndianBytes(uint x)
{
return new byte[]
{
(byte)((x >> 24) & 0xff),
(byte)((x >> 16) & 0xff),
(byte)((x >> 8) & 0xff),
(byte)(x & 0xff)
};
}
}
```
使用示例:
```csharp
string input = "Hello, World!";
string crc32 = CRC32Example.GetCRC32(input);
Console.WriteLine("CRC32 校验值为:" + crc32);
```
输出结果:
```
CRC32 校验值为:6f2f7a8c
```
以上代码实现了一个简单的 CRC32 算法,可以用于计算任意字符串的校验值。
阅读全文