示例#1
0
        private static string Hash(string plain)
        {
            ulong hash = XXHash64.Hash(plain);

            byte[] buffer = BitConverter.GetBytes(hash);
            return(BitConverter.ToString(buffer).Replace("-", "").ToLower());
        }
示例#2
0
        private static string Hash(string key)
        {
            switch (FileNameStyle)
            {
            case KeyStyle.Hashed:
            {
                var         hash  = XXHash64.Hash(key);
                Span <byte> bytes = stackalloc byte[10];
                Utf8Formatter.TryFormat(hash, bytes, out var bytesWritten);
                // This requires netcoreapp3.1 or net5
                // return MemoryMarshal.AsRef<char>(bytes.Slice(0, bytesWritten)).ToString();
                return(DefaultEncoding.GetString(bytes.Slice(0, bytesWritten).ToArray()));
            }

            case KeyStyle.Base64:
            {
                var bytes = DefaultEncoding.GetBytes(key);
                return(UrlBase64.Encode(bytes));
            }

            case KeyStyle.PlainText:
            {
                if (key.ToCharArray().Any(c => Array.BinarySearch(IllegalCharacters, c) >= 0))
                {
                    throw new IllegalKeyException();
                }
                return(key);
            }
            }

            throw new Exception("Invalid key hashing style set!");
        }
示例#3
0
 public unsafe void SystemImpl()
 {
     for (int i = 0; i < N; i++)
     {
         fixed(byte *p = bytes)
         {
             var hash1 = XXHash64.Hash(bytes, 0, Size, 0);
         }
     }
 }
        public void CompHas()
        {
            using (var sw = File.CreateText(EXPORT_PATH))
            {
                foreach (var key in hashes)
                {
                    sw.WriteLine($"{key}\t\t = {XXHash64.Hash(0, key)}");
                }
            }

            Assert.True(File.Exists(EXPORT_PATH));
        }
示例#5
0
        public unsafe static void HashRandomBytesFor()
        {
            Random rd    = new Random(Guid.NewGuid().GetHashCode());
            ulong  hash1 = 0;

            for (int n = 1; n <= 32; n++)
            {
                byte[] bytes = new byte[n];
                for (int i = 0; i < bytes.Length; i++)
                {
                    bytes[i] = (byte)rd.Next(0, byte.MaxValue);
                }

                Stopwatch w = Stopwatch.StartNew();
                for (int i = 0; i < 100000000; i++)
                {
                    hash1 = FastHash.Hash(bytes);
                }
                w.Stop();
                long costA = w.ElapsedMilliseconds;

                w = Stopwatch.StartNew();
                for (int i = 0; i < 100000000; i++)
                {
                    hash1 = XXHash64.Hash(bytes);
                }
                w.Stop();
                long costB = w.ElapsedMilliseconds;
                Console.WriteLine($"Length:{n.ToString().PadRight(3,' ')}    " +
                                  $"FastHash Cost:{costA.ToString().PadRight(5, ' ')}    " +
                                  $"XXHash64 Cost:{costB.ToString().PadRight(5, ' ')}    " +
                                  $"Times:{((float)costB/costA).ToString("f3").PadRight(5,' ')}");
            }

            //rd = new Random(Guid.NewGuid().GetHashCode());
            //for (int n = 1; n <= 32; n++)
            //{
            //    byte[] bytes = new byte[n];
            //    for (int i = 0; i < bytes.Length; i++)
            //        bytes[i] = (byte)rd.Next(0, byte.MaxValue);

            //    Stopwatch w = Stopwatch.StartNew();
            //    for (int i = 0; i < 100000000; i++)
            //    {
            //        hash1 = XXHash64.Hash(bytes);
            //    }

            //    w.Stop();
            //    Console.WriteLine($"length:{n} XXHash64 cost:{w.ElapsedMilliseconds}");
            //}
        }
示例#6
0
        public unsafe static void HashRandomBytes()
        {
            Random rd = new Random(Guid.NewGuid().GetHashCode());

            byte[] bytes = new byte[1024];
            for (int i = 0; i < bytes.Length; i++)
            {
                bytes[i] = (byte)rd.Next(0, byte.MaxValue);
            }

            ulong hash1 = 0;
            ulong hash2 = 0;
            ulong hash3 = 0;
            ulong hash4 = 0;

            //Stopwatch w = Stopwatch.StartNew();
            //fixed (Byte* @in = &bytes[0])
            //{
            //    for (int i = 0; i < 10000000; i++)
            //    {
            //        //hash1 = XXHash64.Hash(@in, bytes.Length, 0);
            //        //XXHash64.Hash(@in, bytes.Length, ref hash1,ref hash2,0,0);
            //        //XXHash64.Hash(@in, bytes.Length, ref hash1, ref hash2,ref hash3, 0, 0,0);
            //        XXHash64.Hash(@in, bytes.Length, ref hash1, ref hash2, ref hash3,ref hash4, 0, 0, 0,0);
            //    }
            //}

            Stopwatch w = Stopwatch.StartNew();

            for (int i = 0; i < 10000000; i++)
            {
                hash1 = XXHash64.Hash(bytes);
                //hash2 = XXHash32.Hash(bytes);
            }

            w.Stop();
            Console.WriteLine($"cost:{w.ElapsedMilliseconds}");
            Console.WriteLine(hash1 == hash2);
        }
示例#7
0
        public unsafe static void HashRandomBytesFor()
        {
            var   rd    = new Random(Guid.NewGuid().GetHashCode());
            ulong hash1 = 0;

            for (int n = 1; n <= 8; n++)
            {
                byte[] bytes = new byte[n];
                for (int i = 0; i < bytes.Length; i++)
                {
                    bytes[i] = (byte)rd.Next(0, byte.MaxValue);
                }

                long costA = 0;
                var  w     = Stopwatch.StartNew();
                fixed(byte * @in = &bytes[0])
                {
                    if (n == 1)
                    {
                        for (int i = 0; i < 100000000; i++)
                        {
                            //hash1 = FastHash.Hash(bytes);
                            //hash1 = XXHash64.HashUnroll(@in,n,0);
                            hash1 = XXHash64.Hash1(@in, 0);
                        }
                        costA = w.ElapsedMilliseconds;
                    }
                    if (n == 2)
                    {
                        for (int i = 0; i < 100000000; i++)
                        {
                            hash1 = XXHash64.Hash2(@in, 0);
                        }
                        costA = w.ElapsedMilliseconds;
                    }
                    if (n == 3)
                    {
                        for (int i = 0; i < 100000000; i++)
                        {
                            hash1 = XXHash64.Hash3(@in, 0);
                        }
                        costA = w.ElapsedMilliseconds;
                    }
                    if (n == 4)
                    {
                        for (int i = 0; i < 100000000; i++)
                        {
                            hash1 = XXHash64.Hash4(@in, 0);
                        }
                        costA = w.ElapsedMilliseconds;
                    }
                    if (n == 5)
                    {
                        for (int i = 0; i < 100000000; i++)
                        {
                            hash1 = XXHash64.Hash5(@in, 0);
                        }
                        costA = w.ElapsedMilliseconds;
                    }
                    if (n == 6)
                    {
                        for (int i = 0; i < 100000000; i++)
                        {
                            hash1 = XXHash64.Hash6(@in, 0);
                        }
                        costA = w.ElapsedMilliseconds;
                    }
                    if (n == 7)
                    {
                        for (int i = 0; i < 100000000; i++)
                        {
                            hash1 = XXHash64.Hash7(@in, 0);
                        }
                        costA = w.ElapsedMilliseconds;
                    }
                    if (n == 8)
                    {
                        for (int i = 0; i < 100000000; i++)
                        {
                            hash1 = XXHash64.Hash8(@in, 0);
                        }
                        costA = w.ElapsedMilliseconds;
                    }

                    w = Stopwatch.StartNew();
                    for (int i = 0; i < 100000000; i++)
                    {
                        hash1 = XXHash64.Hash(@in, n, 0);
                    }
                }

                w.Stop();
                long costB = w.ElapsedMilliseconds;
                Console.WriteLine($"Length:{n.ToString().PadRight(3,' ')}    " +
                                  $"FastHash Cost:{costA.ToString().PadRight(5, ' ')}    " +
                                  $"XXHash64 Cost:{costB.ToString().PadRight(5, ' ')}    " +
                                  $"Times:{((float)costB/costA).ToString("f3").PadRight(5,' ')}");
            }

            //rd = new Random(Guid.NewGuid().GetHashCode());
            //for (int n = 1; n <= 32; n++)
            //{
            //    byte[] bytes = new byte[n];
            //    for (int i = 0; i < bytes.Length; i++)
            //        bytes[i] = (byte)rd.Next(0, byte.MaxValue);

            //    Stopwatch w = Stopwatch.StartNew();
            //    for (int i = 0; i < 100000000; i++)
            //    {
            //        hash1 = XXHash64.Hash(bytes);
            //    }

            //    w.Stop();
            //    Console.WriteLine($"length:{n} XXHash64 cost:{w.ElapsedMilliseconds}");
            //}
        }