1
0

Crc32.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Security.Cryptography;
  29. namespace OpenSim.Framework
  30. {
  31. // this is more generic than openmetaverse CRC32
  32. public class Crc32 : HashAlgorithm
  33. {
  34. public const UInt32 DefaultPolynomial = 0xedb88320;
  35. public const UInt32 DefaultSeed = 0xffffffff;
  36. private UInt32 hash;
  37. private UInt32 seed;
  38. private UInt32[] table;
  39. private static UInt32[] defaultTable;
  40. public Crc32()
  41. {
  42. table = InitializeTable(DefaultPolynomial);
  43. seed = DefaultSeed;
  44. Initialize();
  45. }
  46. public Crc32(UInt32 polynomial, UInt32 seed)
  47. {
  48. table = InitializeTable(polynomial);
  49. this.seed = seed;
  50. Initialize();
  51. }
  52. public override void Initialize()
  53. {
  54. hash = seed;
  55. }
  56. protected override void HashCore(byte[] buffer, int start, int length)
  57. {
  58. hash = CalculateHash(table, hash, buffer, start, length);
  59. }
  60. protected override byte[] HashFinal()
  61. {
  62. byte[] hashBuffer = UInt32ToBigEndianBytes(~hash);
  63. this.HashValue = hashBuffer;
  64. return hashBuffer;
  65. }
  66. public override int HashSize
  67. {
  68. get { return 32; }
  69. }
  70. public static UInt32 Compute(byte[] buffer)
  71. {
  72. return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length);
  73. }
  74. public static UInt32 Compute(UInt32 seed, byte[] buffer)
  75. {
  76. return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length);
  77. }
  78. public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
  79. {
  80. return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
  81. }
  82. private static UInt32[] InitializeTable(UInt32 polynomial)
  83. {
  84. if (polynomial == DefaultPolynomial && defaultTable != null)
  85. return defaultTable;
  86. UInt32[] createTable = new UInt32[256];
  87. for (int i = 0; i < 256; i++)
  88. {
  89. UInt32 entry = (UInt32)i;
  90. for (int j = 0; j < 8; j++)
  91. if ((entry & 1) == 1)
  92. entry = (entry >> 1) ^ polynomial;
  93. else
  94. entry = entry >> 1;
  95. createTable[i] = entry;
  96. }
  97. if (polynomial == DefaultPolynomial)
  98. defaultTable = createTable;
  99. return createTable;
  100. }
  101. private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size)
  102. {
  103. UInt32 crc = seed;
  104. for (int i = start; i < size; i++)
  105. unchecked
  106. {
  107. crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
  108. }
  109. return crc;
  110. }
  111. private byte[] UInt32ToBigEndianBytes(UInt32 x)
  112. {
  113. return new byte[] {
  114. (byte)((x >> 24) & 0xff),
  115. (byte)((x >> 16) & 0xff),
  116. (byte)((x >> 8) & 0xff),
  117. (byte)(x & 0xff) };
  118. }
  119. }
  120. }