Exemplo n.º 1
0
 public static uint Murmur32(this IEnumerable <byte> value, uint seed)
 {
     using (Murmur3 murmur = new Murmur3(seed))
     {
         return(murmur.ComputeHash(value.ToArray()).ToUInt32(0));
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Computes the custom checksum of the data.
 /// </summary>
 /// <param name="data">the data to compute the checksum for.</param>
 /// <param name="checksum1">the 64 bit component of this checksum</param>
 /// <param name="checksum2">the 32 bit component of this checksum</param>
 /// <param name="length">the number of bytes to have in the checksum,Must </param>
 /// <remarks>This checksum is similiar to Adler</remarks>
 public static void ComputeChecksum(IntPtr data, out long checksum1, out int checksum2, int length)
 {
     Stats.ChecksumCount++;
     Murmur3.ComputeHash((byte *)data, length, out ulong a, out ulong b);
     checksum1 = (long)a;
     checksum2 = (int)b ^ (int)(b >> 32);
 }
Exemplo n.º 3
0
        public unsafe void TestIsSame()
        {
            byte[] data = new byte[4096];
            Random r    = new Random();

            for (int x = 0; x < 100; x++)
            {
                r.NextBytes(data);

                Murmur3Orig mm3       = new Murmur3Orig();
                byte[]      checksum  = mm3.ComputeHash(data);
                byte[]      checksum2 = new byte[16];

                fixed(byte *lp = data)
                {
                    ulong value1;
                    ulong value2;

                    Murmur3.ComputeHash(lp, data.Length, out value1, out value2);

                    Array.Copy(BitConverter.GetBytes(value1), 0, checksum2, 0, 8);
                    Array.Copy(BitConverter.GetBytes(value2), 0, checksum2, 8, 8);
                }

                Assert.IsTrue(checksum2.SequenceEqual(checksum));
            }
        }
Exemplo n.º 4
0
        public void When_performing_the_standard_test()
        {
            // Adapted from: https://code.google.com/p/smhasher/source/browse/trunk/KeysetTest.cpp#13
            const uint Murmur3_x64_128 = 0x6384BA69u;

            using (var hash = new Murmur3())
            // Also test that Merkle incremental hashing works.
            using (var cs = new CryptoStream(Stream.Null, hash, CryptoStreamMode.Write))
            {
                var key = new byte[256];

                for (var i = 0; i < 256; i++)
                {
                    key[i] = (byte)i;
                    using (var m = new Murmur3(256 - i))
                    {
                        var computed = m.ComputeHash(key, 0, i);
                        // Also check that your implementation deals with incomplete
                        // blocks.
                        cs.Write(computed, 0, 5);
                        cs.Write(computed, 5, computed.Length - 5);
                    }
                }

                cs.FlushFinalBlock();
                var final = hash.Hash;
                var verification = ((uint)final[0]) | ((uint)final[1] << 8) | ((uint)final[2] << 16) | ((uint)final[3] << 24);
                Assert.Equal(Murmur3_x64_128, verification);
            }
        }
Exemplo n.º 5
0
        private uint TestHash(string testString, uint seed)
        {
            var hashAlgoritm = new Murmur3(seed);
            var testBytes    = Encoding.UTF8.GetBytes(testString);
            var hash         = hashAlgoritm.ComputeHash(testBytes);

            return(hash.ToUInt32());
        }
Exemplo n.º 6
0
        public unsafe void Benchmark()
        {
            byte[] data = new byte[4096];
            Random r    = new Random(1);

            r.NextBytes(data);

            //Prime the run
            Murmur3Orig mm3 = new Murmur3Orig();

            for (int x = 0; x < 1000; x++)
            {
                mm3.ComputeHash(data);
                fixed(byte *lp = data)
                {
                    for (int x = 0; x < 1000; x++)
                    {
                        Murmur3.ComputeHash(lp, data.Length, out ulong value1, out ulong value2);
                    }

                    for (int x = 0; x < 1000; x++)
                    {
                        Footer.ComputeChecksum((IntPtr)lp, out long value3, out int value4, data.Length);
                    }
                }

                Stopwatch sw1 = new Stopwatch();
                Stopwatch sw2 = new Stopwatch();
                Stopwatch sw3 = new Stopwatch();

                sw1.Start();
                for (int x = 0; x < 10000; x++)
                    mm3.ComputeHash(data); }
                sw1.Stop();

                fixed(byte *lp = data)
                {
                    sw2.Start();
                    for (int x = 0; x < 10000; x++)
                    {
                        Murmur3.ComputeHash(lp, data.Length, out ulong value1, out ulong value2);
                    }
                    sw2.Stop();

                    sw3.Start();
                    for (int x = 0; x < 10000; x++)
                    {
                        Footer.ComputeChecksum((IntPtr)lp, out long value3, out int value4, data.Length);
                    }
                    sw3.Stop();
                }

                System.Console.WriteLine("orig: " + (4096 * 10000 / sw1.Elapsed.TotalSeconds / 1024 / 1024).ToString("0 MB/S"));
                System.Console.WriteLine("mine: " + (4096 * 10000 / sw2.Elapsed.TotalSeconds / 1024 / 1024).ToString("0 MB/S"));
                System.Console.WriteLine("old: " + (4096 * 10000 / sw3.Elapsed.TotalSeconds / 1024 / 1024).ToString("0 MB/S"));
        }
Exemplo n.º 7
0
        protected async Task <string> ComputeHashValueAsync(string source)
        {
            if (!File.Exists(source))
            {
                throw new FileNotFoundException($"Unable to locate file {source}");
            }

            return(await Task.Factory.StartNew(() =>
            {
                using (var murmur3 = new Murmur3())
                {
                    using (var fs = File.Open(source, FileMode.Open, FileAccess.Read))
                    {
                        using (var hashProgressStream = new ProgressStream(fs))
                        {
                            hashProgressStream.ProgressChanged += HashProgressStream_ProgressChanged;
                            var hash = murmur3.ComputeHash(hashProgressStream);
                            return hash.ToHexString();
                        }
                    }
                }
            }));
        }
Exemplo n.º 8
0
 public byte[] Has(byte[] b)
 {
     byte[] bx = null;
     bx = m.ComputeHash(b);
     return(bx);
 }
Exemplo n.º 9
0
 public static uint Murmur32(this byte[] message, uint seed)
 {
     using var murmur = new Murmur3(seed);
     return(BitConverter.ToUInt32(murmur.ComputeHash(message), 0));
 }
Exemplo n.º 10
0
 public static byte[] Murmur3(this byte[] message, uint seed)
 {
     using var murmur = new Murmur3(seed);
     return(murmur.ComputeHash(message));
 }