備忘録

備忘録

C#でCRC32を計算する

Ⅰ. はじめに

C#でCRC32を計算するクラスを作成しました。
ホントはSystem.Security.Cryptography.HashAlgorithmを実装するのが筋です。
いつか実装して記事を修正します(そのうちやります…)

Ⅱ. 使い方

バイト配列のCRC32を計算する

byte[] bytes = new byte[] { 0x00, 0x01, 0x02 };
uint crc32 = new CRC32().Calc(bytes);

Console.WriteLine(Convert.ToString(crc32, 16));
// 854897f

文字列のCRC32を計算する

string str = "abc";
byte[] bytes = new UTF8Encoding.GetBytes(str);
uint crc32 = new CRC32().Calc(bytes);

Console.WriteLine(Convert.ToString(crc32, 16));
// 352441c2

ファイルのCRC32を計算する

byte[] bytes = File.ReadAllBytes("hoge.txt");
uint crc32 = new CRC32().Calc(bytes);

Console.WriteLine(Convert.ToString(crc32, 16));
// 86c082c1

gist.github.com