Advertisement
Guest User

MHR Hash

a guest
Sep 26th, 2022
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. public static class MHRHash
  2. {
  3. private const uint m_seed = 0xffffffff;
  4. private const uint m_c1 = 0xcc9e2d51;
  5. private const uint m_c2 = 0x1b873593;
  6. private const uint m_c3 = 0xfaddaf14;
  7. private const int m_r1 = 15;
  8. private const int m_r2 = 13;
  9. public static int Hash(byte[] data)
  10. {
  11. if (data.Length % 4 != 0) throw new InvalidDataException($"{nameof(data)} is not 4 byte aligned.");
  12.  
  13. int hashPosition = data.Length - 4;
  14.  
  15. uint hash = m_seed;
  16. for (int i = 0; i < ((data.Length - 4) / 4); i++)
  17. {
  18. uint intData = BitConverter.ToUInt32(data, i * 4);
  19. uint eax = intData * m_c1;
  20. uint ecx;
  21.  
  22. eax = rol32(eax, m_r1);
  23. ecx = eax * m_c2;
  24.  
  25. hash ^= ecx;
  26. hash = rol32(hash, m_r2);
  27. hash += m_c3;
  28.  
  29. long temp = hash + hash * 4;
  30. hash = (uint)temp;
  31. }
  32.  
  33. byte twoBits = data[hashPosition];
  34. uint extraHash = 0;
  35.  
  36. if (twoBits == 3)
  37. {
  38. extraHash = (uint)data[hashPosition + 2] << 0x10;
  39. }
  40.  
  41. if (twoBits == 2)
  42. {
  43. extraHash = (uint)data[hashPosition + 1] << 8;
  44. }
  45.  
  46. if (twoBits == 1)
  47. {
  48. extraHash = data[hashPosition] ^ extraHash;
  49. extraHash *= m_c1;
  50. extraHash *= m_c2;
  51. hash ^= extraHash;
  52. }
  53.  
  54. hash ^= (uint)hashPosition;
  55. hash ^= hash >> 16;
  56. hash *= 0x85ebca6b;
  57. hash ^= hash >> 13;
  58. hash *= 0xc2b2ae35;
  59. hash ^= hash >> 16;
  60.  
  61. return (int)hash;
  62. }
  63.  
  64. private static uint rol32(uint value, int amount)
  65. {
  66. return (value << amount) | (value >> (32 - amount));
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement