CRC32.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Security.Cryptography;
  3. namespace Utilities
  4. {
  5. // Author: Damien Guard (damieng@gmail.com)
  6. // http://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net
  7. public class CRC32 : HashAlgorithm
  8. {
  9. public const UInt32 DefaultPolynomial = 0xedb88320;
  10. public const UInt32 DefaultSeed = 0xffffffff;
  11. private UInt32 hash;
  12. private UInt32 seed;
  13. private UInt32[] table;
  14. private static UInt32[] defaultTable;
  15. public CRC32()
  16. {
  17. table = InitializeTable(DefaultPolynomial);
  18. seed = DefaultSeed;
  19. Initialize();
  20. }
  21. public CRC32(UInt32 polynomial, UInt32 seed)
  22. {
  23. table = InitializeTable(polynomial);
  24. this.seed = seed;
  25. Initialize();
  26. }
  27. public override void Initialize()
  28. {
  29. hash = seed;
  30. }
  31. protected override void HashCore(byte[] buffer, int start, int length)
  32. {
  33. hash = CalculateHash(table, hash, buffer, start, length);
  34. }
  35. protected override byte[] HashFinal()
  36. {
  37. byte[] hashBuffer = UInt32ToBigEndianBytes(~hash);
  38. this.HashValue = hashBuffer;
  39. return hashBuffer;
  40. }
  41. public override int HashSize
  42. {
  43. get { return 32; }
  44. }
  45. public static UInt32 Compute(byte[] buffer)
  46. {
  47. return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length);
  48. }
  49. public static UInt32 Compute(UInt32 seed, byte[] buffer)
  50. {
  51. return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length);
  52. }
  53. public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
  54. {
  55. return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
  56. }
  57. private static UInt32[] InitializeTable(UInt32 polynomial)
  58. {
  59. if (polynomial == DefaultPolynomial && defaultTable != null)
  60. return defaultTable;
  61. UInt32[] createTable = new UInt32[256];
  62. for (int i = 0; i < 256; i++)
  63. {
  64. UInt32 entry = (UInt32)i;
  65. for (int j = 0; j < 8; j++)
  66. if ((entry & 1) == 1)
  67. entry = (entry >> 1) ^ polynomial;
  68. else
  69. entry = entry >> 1;
  70. createTable[i] = entry;
  71. }
  72. if (polynomial == DefaultPolynomial)
  73. defaultTable = createTable;
  74. return createTable;
  75. }
  76. private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size)
  77. {
  78. UInt32 crc = seed;
  79. for (int i = start; i < size; i++)
  80. unchecked
  81. {
  82. crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
  83. }
  84. return crc;
  85. }
  86. private byte[] UInt32ToBigEndianBytes(UInt32 x)
  87. {
  88. return new byte[] {
  89. (byte)((x >> 24) & 0xff),
  90. (byte)((x >> 16) & 0xff),
  91. (byte)((x >> 8) & 0xff),
  92. (byte)(x & 0xff)
  93. };
  94. }
  95. // Added by Tal Aloni 2013.07.11
  96. public static uint UPDC32(byte octet, uint crc)
  97. {
  98. uint[] table = InitializeTable(DefaultPolynomial);
  99. return (table[((crc) ^ ((byte)octet)) & 0xff] ^ ((crc) >> 8));
  100. }
  101. }
  102. }