Advertisement
Guest User

Untitled

a guest
Jan 14th, 2019
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace MurMur3Tests
  9. {
  10.     public static class MurMurHash3
  11.     {
  12.         //Change to suit your needs
  13.         const uint seed = 0xFFFFFFFF;
  14.  
  15.         public static uint Hash(Stream stream)
  16.         {
  17.             const uint c1 = 0xcc9e2d51;
  18.             const uint c2 = 0x1b873593;
  19.  
  20.             uint h1 = seed;
  21.             uint k1 = 0;
  22.             uint streamLength = 0;
  23.  
  24.             using (BinaryReader reader = new BinaryReader(stream))
  25.             {
  26.                 byte[] chunk = reader.ReadBytes(4);
  27.                 while (chunk.Length > 0)
  28.                 {
  29.                     streamLength += (uint)chunk.Length;
  30.                     switch (chunk.Length)
  31.                     {
  32.                         case 4:
  33.                             /* Get four bytes from the input into an uint */
  34.                             k1 = (uint)
  35.                                (chunk[0]
  36.                               | chunk[1] << 8
  37.                               | chunk[2] << 16
  38.                               | chunk[3] << 24);
  39.  
  40.                             /* bitmagic hash */
  41.                             k1 *= c1;
  42.                             k1 = rotl32(k1, 15);
  43.                             k1 *= c2;
  44.  
  45.                             h1 ^= k1;
  46.                             h1 = rotl32(h1, 13);
  47.                             h1 = h1 * 5 + 0xe6546b64;
  48.                             break;
  49.                         case 3:
  50.                             k1 = (uint)
  51.                                (chunk[0]
  52.                               | chunk[1] << 8
  53.                               | chunk[2] << 16);
  54.                             k1 *= c1;
  55.                             k1 = rotl32(k1, 15);
  56.                             k1 *= c2;
  57.                             h1 ^= k1;
  58.                             break;
  59.                         case 2:
  60.                             k1 = (uint)
  61.                                (chunk[0]
  62.                               | chunk[1] << 8);
  63.                             k1 *= c1;
  64.                             k1 = rotl32(k1, 15);
  65.                             k1 *= c2;
  66.                             h1 ^= k1;
  67.                             break;
  68.                         case 1:
  69.                             k1 = (uint)(chunk[0]);
  70.                             k1 *= c1;
  71.                             k1 = rotl32(k1, 15);
  72.                             k1 *= c2;
  73.                             h1 ^= k1;
  74.                             break;
  75.  
  76.                     }
  77.                     chunk = reader.ReadBytes(4);
  78.                 }
  79.             }
  80.  
  81.             // finalization, magic chants to wrap it all up
  82.             h1 ^= streamLength;
  83.             h1 = fmix(h1);
  84.  
  85.             return h1;
  86.         }
  87.  
  88.         private static uint rotl32(uint x, byte r)
  89.         {
  90.             return (x << r) | (x >> (32 - r));
  91.         }
  92.  
  93.         private static uint fmix(uint h)
  94.         {
  95.             h ^= h >> 16;
  96.             h *= 0x85ebca6b;
  97.             h ^= h >> 13;
  98.             h *= 0xc2b2ae35;
  99.             h ^= h >> 16;
  100.             return h;
  101.         }
  102.     }
  103.  
  104.     class Program
  105.     {
  106.  
  107.         static void Main(string[] args)
  108.         {
  109.             using (var stream = new MemoryStream(Encoding.Unicode.GetBytes("natives/x64/systems/rendering/nullwhite.tex.10".ToLower())))
  110.             {
  111.                 Console.WriteLine(MurMurHash3.Hash(stream));
  112.             }
  113.             Console.ReadLine();
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement