网站建设和维护的教程,网站网络优化服务器,南京做网站的额,中国最好的网络营销公司咨询区 Nick Berardi请问我如何通过 C# 计算 string 的 crc32 校验和#xff1f;回答区 Pete我一直震撼的一件事就是 .NET 底层框架中居然没有提供对 CRC32 的直接支持#xff0c;CRC32 一直都是 ZIP,RAR 压缩中做校验和的绝佳方案#xff0c;在 .NET 中你可以这么使用。var… 咨询区 Nick Berardi请问我如何通过 C# 计算 string 的 crc32 校验和回答区 Pete我一直震撼的一件事就是 .NET 底层框架中居然没有提供对 CRC32 的直接支持CRC32 一直都是 ZIP,RAR 压缩中做校验和的绝佳方案在 .NET 中你可以这么使用。var crc32 new Crc32();
var hash String.Empty;using (var 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);更详细的资料可参考https://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net 和 https://github.com/damieng/DamienGKit/blob/master/CSharp/DamienG.Library/Security/Cryptography/Crc32.csSharpC上面朋友的回答是基于 File 的方式其实替换成 string 会更加直接方便我的项目中定义了一个 Crc32 帮助类代码如下/// summary
/// Performs 32-bit reversed cyclic redundancy checks.
/// /summary
public class Crc32
{#region Constants/// summary/// Generator polynomial (modulo 2) for the reversed CRC32 algorithm. /// /summaryprivate const UInt32 s_generator 0xEDB88320;#endregion#region Constructors/// summary/// Creates a new instance of the Crc32 class./// /summarypublic Crc32(){// Constructs the checksum lookup table. Used to optimize the checksum.m_checksumTable Enumerable.Range(0, 256).Select(i {var tableEntry (uint)i;for (var j 0; j 8; j){tableEntry ((tableEntry 1) ! 0)? (s_generator ^ (tableEntry 1)) : (tableEntry 1);}return tableEntry;}).ToArray();}#endregion#region Methods/// summary/// Calculates the checksum of the byte stream./// /summary/// param namebyteStreamThe byte stream to calculate the checksum for./param/// returnsA 32-bit reversed checksum./returnspublic UInt32 GetT(IEnumerableT byteStream){try{// Initialize checksumRegister to 0xFFFFFFFF and calculate the checksum.return ~byteStream.Aggregate(0xFFFFFFFF, (checksumRegister, currentByte) (m_checksumTable[(checksumRegister 0xFF) ^ Convert.ToByte(currentByte)] ^ (checksumRegister 8)));}catch (FormatException e){throw new CrcException(Could not read the stream out as bytes., e);}catch (InvalidCastException e){throw new CrcException(Could not read the stream out as bytes., e);}catch (OverflowException e){throw new CrcException(Could not read the stream out as bytes., e);}}#endregion#region Fields/// summary/// Contains a cache of calculated checksum chunks./// /summaryprivate readonly UInt32[] m_checksumTable;#endregion
}帮助类定义好后接下来就可以这么使用了。var arrayOfBytes Encoding.ASCII.GetBytes(The quick brown fox jumps over the lazy dog);var crc32 new Crc32();
Console.WriteLine(crc32.Get(arrayOfBytes).ToString(X));对了你可以用 https://crccalc.com/ 以在线的方式来测试你的 crc32 的输入输出。点评区 两位大佬提供的方式不错学习了最后的 Online https://crccalc.com/ 太方便了有兴趣可以尝试一下。