Crc32.cs 3.6 KB

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