Crc32.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Cryptography;
  4. namespace Masuit.Tools.Security;
  5. /// <summary>
  6. /// 实现一个32位CRC哈希算法(兼容Zip)
  7. /// </summary>
  8. /// <remarks>
  9. /// Crc32仅应用于与旧文件格式和算法向后兼容。 对于新的应用程序,它不够安全。 如果需要多次调用同一数据,请使用HashAlgorithm接口
  10. /// </remarks>
  11. public sealed class Crc32 : HashAlgorithm
  12. {
  13. ~Crc32()
  14. {
  15. Dispose();
  16. }
  17. public const uint DefaultPolynomial = 0xedb88320u;
  18. public const uint DefaultSeed = 0xffffffffu;
  19. private static uint[] _defaultTable;
  20. private readonly uint _seed;
  21. private readonly uint[] _table;
  22. private uint _hash;
  23. public override int HashSize => 32;
  24. public Crc32() : this(DefaultPolynomial, DefaultSeed)
  25. {
  26. }
  27. public Crc32(uint polynomial, uint seed)
  28. {
  29. if (!BitConverter.IsLittleEndian)
  30. {
  31. throw new PlatformNotSupportedException("Big Endian 处理程序不支持");
  32. }
  33. _table = InitializeTable(polynomial);
  34. _seed = _hash = seed;
  35. }
  36. public override void Initialize()
  37. {
  38. _hash = _seed;
  39. }
  40. protected override void HashCore(byte[] array, int ibStart, int cbSize)
  41. {
  42. _hash = CalculateHash(_table, _hash, array, ibStart, cbSize);
  43. }
  44. protected override byte[] HashFinal()
  45. {
  46. var hashBuffer = UInt32ToBigEndianBytes(~_hash);
  47. HashValue = hashBuffer;
  48. return hashBuffer;
  49. }
  50. public static uint Compute(byte[] buffer)
  51. {
  52. return Compute(DefaultSeed, buffer);
  53. }
  54. public static uint Compute(uint seed, byte[] buffer)
  55. {
  56. return Compute(DefaultPolynomial, seed, buffer);
  57. }
  58. public static uint Compute(uint polynomial, uint seed, byte[] buffer)
  59. {
  60. return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
  61. }
  62. private static uint[] InitializeTable(uint polynomial)
  63. {
  64. if (polynomial == DefaultPolynomial && _defaultTable != null)
  65. {
  66. return _defaultTable;
  67. }
  68. var createTable = new uint[256];
  69. for (uint i = 0; i < 256; i++)
  70. {
  71. var entry = i;
  72. for (var j = 0; j < 8; j++)
  73. {
  74. if ((entry & 1) == 1)
  75. {
  76. entry = (entry >> 1) ^ polynomial;
  77. }
  78. else
  79. {
  80. entry >>= 1;
  81. }
  82. }
  83. createTable[i] = entry;
  84. }
  85. if (polynomial == DefaultPolynomial)
  86. {
  87. _defaultTable = createTable;
  88. }
  89. return createTable;
  90. }
  91. private static uint CalculateHash(uint[] table, uint seed, IList<byte> buffer, int start, int size)
  92. {
  93. var hash = seed;
  94. for (var i = start; i < start + size; i++)
  95. {
  96. hash = (hash >> 8) ^ table[buffer[i] ^ hash & 0xff];
  97. }
  98. return hash;
  99. }
  100. private static byte[] UInt32ToBigEndianBytes(uint uint32)
  101. {
  102. var result = BitConverter.GetBytes(uint32);
  103. if (BitConverter.IsLittleEndian)
  104. {
  105. Array.Reverse(result);
  106. }
  107. return result;
  108. }
  109. }