本文介绍了如何在n次迭代中生成CRC 32哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Hi 我想通过读取2 MB的文件来生成CRC 32哈希值,并且应该读取512个字节并计算512字节的哈希并将生成的哈希值传递给下一个迭代并计算下一个迭代字节数组的散列以及前一个散列,依此类推,直到完成2 MB文件(最多4096次迭代)。 通过以下方式代码只能生成一个迭代哈希值,不能通过传递现有哈希值来生成多次迭代。 以下是我的代码。 HiI would like to generate CRC 32 hash value by reading a file 2 MB and should read 512 bytes and calculate hash for 512 bytes and pass that generated hash value to next iteration and calculate hash of next iteration byte array along with previous hash and so on until complete 2 MB file (that is up-to 4096 iterations).By the following code able to generate only one iteration hash not able to generate for multiple iteration by passing existing hash value.Following is my code.static void Main(string[] args){var path = @"C:\Users\Desktop\RandomKeys.bin";uint crc = 0;var filedata = File.ReadAllBytes(path);var readBuff = new uint[128];Crc32Single crc32 = new Crc32Single();for (int i = 0; i < 4096; i++){Buffer.BlockCopy(filedata, (i * 512), readBuff, 0, 512);byte[] aa = crc32.ComputeHash(readBuff.SelectMany(BitConverter.GetBytes).ToArray(), 0, 512);Array.Reverse(aa);crc = BitConverter.ToUInt32(aa, 0);Console.WriteLine("\n********** : " + crc.ToString("X"));}Console.ReadLine();}public sealed class Crc32 : HashAlgorithm{public const UInt32 DefaultPolynomial = 0xedb88320u;public const UInt32 DefaultSeed = 0xffffffffu;static UInt32[] defaultTable;public UInt32 seed;readonly UInt32[] table;UInt32 hash;public Crc32(): this(DefaultPolynomial, DefaultSeed){}public Crc32(UInt32 polynomial, UInt32 seed){table = InitializeTable(polynomial);this.seed = hash = seed;}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(){var hashBuffer = UInt32ToBigEndianBytes(~hash);HashValue = hashBuffer;return hashBuffer;}public override int HashSize { get { return 32; } }public static UInt32 Compute(byte[] buffer){return Compute(DefaultSeed, buffer);}public static UInt32 Compute(UInt32 seed, byte[] buffer){return Compute(DefaultPolynomial, seed, buffer);}public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer){return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);}static UInt32[] InitializeTable(UInt32 polynomial){if (polynomial == DefaultPolynomial && defaultTable != null)return defaultTable;var createTable = new UInt32[256];for (var i = 0; i < 256; i++){var entry = (UInt32)i;for (var j = 0; j < 8; j++)if ((entry & 1) == 1)entry = (entry >> 1) ^ polynomial;elseentry = entry >> 1;createTable[i] = entry;}if (polynomial == DefaultPolynomial)defaultTable = createTable;return createTable;}static UInt32 CalculateHash(UInt32[] table, UInt32 seed, IList<byte> buffer, int start, int size){var hash = seed;for (var i = start; i < start + size; i++)hash = (hash >> 8) ^ table[buffer[i] ^ hash & 0xff];return hash;}static byte[] UInt32ToBigEndianBytes(UInt32 uint32){var result = BitConverter.GetBytes(uint32);if (BitConverter.IsLittleEndian)Array.Reverse(result);return result;}} 谢谢,任何帮助都可以升值。 我尝试了什么: 以下链接我试过但没有得到Exact哈希值 使用HashAlgorithm的Crc32 C#.NET中的Crc32.cs源代码 CRC安全C# CRC C# 推荐答案 尝试移动Crc32Single crc32 = new Crc32Single();在你的循环中...... Inside your loop... 这篇关于如何在n次迭代中生成CRC 32哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-20 15:36