// Copyright (c) Damien Guard. All rights reserved. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. // You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 using System; using System.Collections.Generic; using System.Security.Cryptography; namespace DamienG.Security.Cryptography { /// /// Implements a 32-bit CRC hash algorithm compatible with Zip etc. /// /// /// Crc32 should only be used for backward compatibility with older file formats /// and algorithms. It is not secure enough for new applications. /// If you need to call multiple times for the same data either use the HashAlgorithm /// interface or remember that the result of one Compute call needs to be ~ (XOR) before /// being passed in as the seed for the next Compute call. /// public sealed class Crc32 : HashAlgorithm { public const UInt32 DefaultPolynomial = 0xedb88320u; public const UInt32 DefaultSeed = 0xffffffffu; static UInt32[] defaultTable; readonly UInt32 seed; readonly UInt32[] table; UInt32 hash; /// /// Create a new with a and . /// public Crc32() : this(DefaultPolynomial, DefaultSeed) { } /// /// Create a new with a supplied polynomial and see. /// /// The polynomial to use in calculating. /// The initial seed to start from. public Crc32(UInt32 polynomial, UInt32 seed) { if (!BitConverter.IsLittleEndian) throw new PlatformNotSupportedException("Not supported on Big Endian processors"); 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 => 32; /// /// Calculate the for a given with the /// and . /// /// The buffer to calcuate a CRC32 for. /// The CRC32 for the buffer. public static UInt32 Compute(byte[] buffer) => Compute(DefaultSeed, buffer); /// /// Calculate the for a given with a /// specified and . /// /// The initial seed to start from. /// The buffer to calcuate a CRC32 for. /// The CRC32 for the buffer. public static UInt32 Compute(UInt32 seed, byte[] buffer) => Compute(DefaultPolynomial, seed, buffer); /// /// Calculate the for a given with a /// specified and . /// /// The initial seed to start from. /// The buffer to calcuate a CRC32 for. /// The CRC32 for the buffer. public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer) => ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length); /// /// Initialize a CRC32 calculation table for a given polynomial. /// /// The polynomial to calculate a table for. /// A table to be used in calculating a CRC32. 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; else entry >>= 1; createTable[i] = entry; } if (polynomial == DefaultPolynomial) defaultTable = createTable; return createTable; } /// /// Calculate an inverted CRC32 for a given using a polynomial-derived . /// /// The polynomial-derived table such as from . /// The initial seed to start from. /// The buffer to calculate the CRC32 from. /// What position within the to start calculating from. /// How many bytes within the to read in calculating the CRC32. /// The bit-inverted CRC32. /// This hash is bit-inverted. Use other methods in this class or the result from this method. static UInt32 CalculateHash(UInt32[] table, UInt32 seed, IList 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; } /// /// Convert a to a taking care /// to reverse the bytes on little endian processors. /// /// The to convert. /// The containing the converted bytes. static byte[] UInt32ToBigEndianBytes(UInt32 uint32) { var result = BitConverter.GetBytes(uint32); if (BitConverter.IsLittleEndian) Array.Reverse(result); return result; } } }