Files
MoShiro/backend/shared/MoShiro.Protocol/Raw/Crc32c.cs
T
justme 83e5120377 refactor!: rename the project from Monitor to MoShiro
Namespaces, assemblies, projects and directories, the solution, binaries,
images, systemd units, config paths, environment variables, protocol method
names, the media type and the raw chunk magic all move to MoShiro/moshiro.
Existing data and deployments are not compatible and must be recreated.
Also fix the architecture tests after the solution layout change and make
the update tests independent of their order.
2026-10-07 01:49:04 +08:00

36 lines
791 B
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace MoShiro.Protocol;
/// <summary>CRC-32C(Castagnoli),RawChunk 的完整性校验。</summary>
public static class Crc32c
{
private static readonly uint[] s_table = BuildTable();
public static uint Compute(ReadOnlySpan<byte> data)
{
uint crc = uint.MaxValue;
foreach (byte b in data)
{
crc = s_table[(crc ^ b) & 0xFF] ^ (crc >> 8);
}
return ~crc;
}
private static uint[] BuildTable()
{
var table = new uint[256];
for (uint i = 0; i < 256; i++)
{
uint c = i;
for (int k = 0; k < 8; k++)
{
c = (c & 1) != 0 ? (c >> 1) ^ 0x82F63B78u : c >> 1;
}
table[i] = c;
}
return table;
}
}