Crc64.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.IO;
  3. namespace COSXML.Utils
  4. {
  5. public class Crc64
  6. {
  7. private static ulong[] _table;
  8. private static object _lock = new object();
  9. private const int GF2_DIM = 64;
  10. private static ulong _poly;
  11. private static void GenStdCrcTable(ulong poly)
  12. {
  13. _poly = poly;
  14. _table = new ulong[256];
  15. for (uint n = 0; n < 256; n++)
  16. {
  17. ulong crc = n;
  18. for (int k = 0; k < 8; k++)
  19. {
  20. if ((crc & 1) == 1)
  21. {
  22. crc = (crc >> 1) ^ poly;
  23. }
  24. else
  25. {
  26. crc = (crc >> 1);
  27. }
  28. }
  29. _table[n] = crc;
  30. }
  31. }
  32. private static ulong TableValue(ulong[] table, byte b, ulong crc)
  33. {
  34. unchecked
  35. {
  36. return (crc >> 8) ^ table[(crc ^ b) & 0xffUL];
  37. }
  38. }
  39. public static void Init(ulong poly)
  40. {
  41. if (_table == null)
  42. {
  43. lock (_lock)
  44. {
  45. if (_table == null)
  46. {
  47. GenStdCrcTable(poly);
  48. }
  49. }
  50. }
  51. }
  52. public static void InitECMA()
  53. {
  54. Init(0xC96C5795D7870F42);
  55. }
  56. public static ulong Compute(byte[] bytes, int start, int size, ulong crc = 0)
  57. {
  58. crc = ~crc;
  59. for (var i = start; i < start + size; i++)
  60. {
  61. crc = TableValue(_table, bytes[i], crc);
  62. }
  63. crc = ~crc;
  64. return crc;
  65. }
  66. private static ulong Gf2MatrixTimes(ulong[] mat, ulong vec)
  67. {
  68. ulong sum = 0;
  69. int idx = 0;
  70. while (vec != 0)
  71. {
  72. if ((vec & 1) == 1)
  73. {
  74. sum ^= mat[idx];
  75. }
  76. vec >>= 1;
  77. idx++;
  78. }
  79. return sum;
  80. }
  81. private static void Gf2MatrixSquare(ulong[] square, ulong[] mat)
  82. {
  83. for (int n = 0; n < GF2_DIM; n++)
  84. {
  85. square[n] = Gf2MatrixTimes(mat, mat[n]);
  86. }
  87. }
  88. static public ulong Combine(ulong crc1, ulong crc2, long len2)
  89. {
  90. // degenerate case.
  91. if (len2 == 0)
  92. {
  93. return crc1;
  94. }
  95. int n;
  96. ulong row;
  97. ulong[] even = new ulong[GF2_DIM];
  98. ulong[] odd = new ulong[GF2_DIM];
  99. // put operator for one zero bit in odd
  100. odd[0] = _poly;
  101. row = 1;
  102. for (n = 1; n < GF2_DIM; n++)
  103. {
  104. odd[n] = row;
  105. row <<= 1;
  106. }
  107. // put operator for two zero bits in even
  108. Gf2MatrixSquare(even, odd);
  109. // put operator for four zero bits in odd
  110. Gf2MatrixSquare(odd, even);
  111. // apply len2 zeros to crc1 (first square will put the operator for one
  112. // zero byte, eight zero bits, in even)
  113. do
  114. {
  115. // apply zeros operator for this bit of len2
  116. Gf2MatrixSquare(even, odd);
  117. if ((len2 & 1) == 1)
  118. {
  119. crc1 = Gf2MatrixTimes(even, crc1);
  120. }
  121. len2 >>= 1;
  122. // if no more bits set, then done
  123. if (len2 == 0)
  124. {
  125. break;
  126. }
  127. // another iteration of the loop with odd and even swapped
  128. Gf2MatrixSquare(odd, even);
  129. if ((len2 & 1) == 1)
  130. {
  131. crc1 = Gf2MatrixTimes(odd, crc1);
  132. }
  133. len2 >>= 1;
  134. // if no more bits set, then done
  135. } while (len2 != 0);
  136. // return combined crc.
  137. crc1 ^= crc2;
  138. return crc1;
  139. }
  140. public static bool CompareCrc64(string localFile, string crc64ecma)
  141. {
  142. Crc64.InitECMA();
  143. String hash = String.Empty;
  144. using (FileStream fs = File.Open(localFile, FileMode.Open))
  145. {
  146. byte[] buffer = new byte[2048];
  147. int bytesRead;
  148. ulong crc = 0;
  149. while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
  150. {
  151. ulong partCrc = Crc64.Compute(buffer, 0, bytesRead);
  152. if (crc == 0)
  153. {
  154. crc = partCrc;
  155. }
  156. else
  157. {
  158. crc = Crc64.Combine(crc, partCrc, bytesRead);
  159. }
  160. }
  161. string localFileCrc64 = crc.ToString();
  162. return localFileCrc64 == crc64ecma;
  163. }
  164. }
  165. }
  166. }