.NETで文字列のCRC32(巡回冗長検査)を計算するにはどうすればよいですか?
質問する
97300 次
3 に答える
43
この男はあなたの答えを持っているようです。
https://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net
また、ブログがなくなったりURLが壊れたりした場合に備えて、githubのリンクは次のとおりです。
ブログ投稿のCrc32クラスの使用法:
Crc32 crc32 = new Crc32();
String hash = String.Empty;
using (FileStream fs = File.Open("c:\\myfile.txt", FileMode.Open))
foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToLower();
Console.WriteLine("CRC-32 is {0}", hash);
于 2008-08-11T19:43:31.003 に答える
2
前の回答のロジックを使用すると、これが私の見解でした:
public class CRC32
{
private readonly uint[] ChecksumTable;
private readonly uint Polynomial = 0xEDB88320;
public CRC32()
{
ChecksumTable = new uint[0x100];
for (uint index = 0; index < 0x100; ++index)
{
uint item = index;
for (int bit = 0; bit < 8; ++bit)
item = ((item & 1) != 0) ? (Polynomial ^ (item >> 1)) : (item >> 1);
ChecksumTable[index] = item;
}
}
public byte[] ComputeHash(Stream stream)
{
uint result = 0xFFFFFFFF;
int current;
while ((current = stream.ReadByte()) != -1)
result = ChecksumTable[(result & 0xFF) ^ (byte)current] ^ (result >> 8);
byte[] hash = BitConverter.GetBytes(~result);
Array.Reverse(hash);
return hash;
}
public byte[] ComputeHash(byte[] data)
{
using (MemoryStream stream = new MemoryStream(data))
return ComputeHash(stream);
}
}
于 2020-04-29T06:46:09.137 に答える