Crc32.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (c) Damien Guard. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  3. // You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Security.Cryptography;
  7. namespace InABox.Core
  8. {
  9. /// <summary>
  10. /// Implements a 32-bit CRC hash algorithm compatible with Zip etc.
  11. /// </summary>
  12. /// <remarks>
  13. /// Crc32 should only be used for backward compatibility with older file formats
  14. /// and algorithms. It is not secure enough for new applications.
  15. /// If you need to call multiple times for the same data either use the HashAlgorithm
  16. /// interface or remember that the result of one Compute call needs to be ~ (XOR) before
  17. /// being passed in as the seed for the next Compute call.
  18. /// </remarks>
  19. public sealed class Crc32 : HashAlgorithm
  20. {
  21. public const uint DefaultPolynomial = 0xedb88320u;
  22. public const uint DefaultSeed = 0xffffffffu;
  23. private static uint[] defaultTable;
  24. private readonly uint seed;
  25. private readonly uint[] table;
  26. private uint hash;
  27. public Crc32()
  28. : this(DefaultPolynomial, DefaultSeed)
  29. {
  30. }
  31. public Crc32(uint polynomial, uint seed)
  32. {
  33. if (!BitConverter.IsLittleEndian)
  34. throw new PlatformNotSupportedException("Not supported on Big Endian processors");
  35. table = InitializeTable(polynomial);
  36. this.seed = hash = seed;
  37. }
  38. public override int HashSize => 32;
  39. public override void Initialize()
  40. {
  41. hash = seed;
  42. }
  43. protected override void HashCore(byte[] array, int ibStart, int cbSize)
  44. {
  45. hash = CalculateHash(table, hash, array, ibStart, cbSize);
  46. }
  47. protected override byte[] HashFinal()
  48. {
  49. var hashBuffer = UInt32ToBigEndianBytes(~hash);
  50. HashValue = hashBuffer;
  51. return hashBuffer;
  52. }
  53. public static uint Compute(byte[] buffer)
  54. {
  55. return Compute(DefaultSeed, buffer);
  56. }
  57. public static uint Compute(uint seed, byte[] buffer)
  58. {
  59. return Compute(DefaultPolynomial, seed, buffer);
  60. }
  61. public static uint Compute(uint polynomial, uint seed, byte[] buffer)
  62. {
  63. return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
  64. }
  65. private static uint[] InitializeTable(uint polynomial)
  66. {
  67. if (polynomial == DefaultPolynomial && defaultTable != null)
  68. return defaultTable;
  69. var createTable = new uint[256];
  70. for (var i = 0; i < 256; i++)
  71. {
  72. var entry = (uint)i;
  73. for (var j = 0; j < 8; j++)
  74. if ((entry & 1) == 1)
  75. entry = (entry >> 1) ^ polynomial;
  76. else
  77. entry = entry >> 1;
  78. createTable[i] = entry;
  79. }
  80. if (polynomial == DefaultPolynomial)
  81. defaultTable = createTable;
  82. return createTable;
  83. }
  84. private static uint CalculateHash(uint[] table, uint seed, IList<byte> buffer, int start, int size)
  85. {
  86. var hash = seed;
  87. for (var i = start; i < start + size; i++)
  88. hash = (hash >> 8) ^ table[buffer[i] ^ (hash & 0xff)];
  89. return hash;
  90. }
  91. private static byte[] UInt32ToBigEndianBytes(uint uint32)
  92. {
  93. var result = BitConverter.GetBytes(uint32);
  94. if (BitConverter.IsLittleEndian)
  95. Array.Reverse(result);
  96. return result;
  97. }
  98. }
  99. }