当前位置: 首页>>代码示例>>C#>>正文


C# IHasher.Hash方法代码示例

本文整理汇总了C#中IHasher.Hash方法的典型用法代码示例。如果您正苦于以下问题:C# IHasher.Hash方法的具体用法?C# IHasher.Hash怎么用?C# IHasher.Hash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IHasher的用法示例。


在下文中一共展示了IHasher.Hash方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: VerificationTest

        //-----------------------------------------------------------------------------
        // This should hopefully be a thorough and unambiguous test of whether a hash
        // is correctly implemented on a given platform
        public static bool VerificationTest(IHasher hasher, uint expected)
        {
            const int hashBytes = 4;

            var key = new byte[256];
            var hashes = new byte[hashBytes * 256];

            // Hash keys of the form {0}, {0,1}, {0,1,2}... up to N=255,using 256-N as
            // the seed
            for (int i = 0; i < 256; i++)
            {
                key[i] = (byte)i;

                var hash = hasher.Hash(key, 0, (uint)i, (uint)(256 - i));
                Buffer.BlockCopy(BitConverter.GetBytes(hash), 0, hashes, i*hashBytes, hashBytes);
            }

            // Then hash the result array

            var verification = hasher.Hash(hashes, 0, hashBytes * 256, 0);

            // The first four bytes of that hash, interpreted as a little-endian integer, is our
            // verification value

            //uint verification = (final[0] << 0) | (final[1] << 8) | (final[2] << 16) | (final[3] << 24);


            //----------

            if (expected != verification)
            {
                Console.WriteLine("Verification value 0x{0:X8} : Failed! (Expected 0x{1:X8})", verification, expected);
                return false;
            }
            else
            {
                Console.WriteLine("Verification value 0x{0:X8} : Passed!", verification);
                return true;
            }
        }
开发者ID:Kristinn-Stefansson,项目名称:EventStore,代码行数:43,代码来源:SMHasher.cs

示例2: TestFixtureSetUp

        public override void TestFixtureSetUp()
        {
            hasher = new Murmur3AUnsafe();
            base.TestFixtureSetUp();

            for (int i = 0; i < 4; i++)
            {
                _files.Add(GetTempFilePath());

                var table = new HashListMemTable(_ptableVersion, maxSize: 20);
                for (int j=0;j<10;j++)
                {
                    table.Add((ulong)(0x010100000000 << (j + 1)), i + 1, i*j);
                }
                _tables.Add(PTable.FromMemtable(table, _files[i]));
            }
            _files.Add(GetTempFilePath());
            _newtable = PTable.MergeTo(_tables, _files[4], (streamId, hash) => hash << 32 | hasher.Hash(streamId), _ => true, _ => new System.Tuple<string, bool>("", true), _ptableVersion);
        }
开发者ID:SzymonPobiega,项目名称:EventStore,代码行数:19,代码来源:when_merging_four_ptables.cs

示例3: CompositeFileModel

 public CompositeFileModel(IHasher hasher, IUrlManager urlManager, IActionContextAccessor accessor)
     : base("file", urlManager, accessor)
 {
     //Creates a single hash of the full url (which can include many files)
     _fileSetKey = hasher.Hash(string.Join(".", ParsedPath.Names));
 }
开发者ID:eByte23,项目名称:Smidge,代码行数:6,代码来源:CompositeFileModel.cs

示例4: SanityTest

        //----------------------------------------------------------------------------
        // Basic sanity checks:
        //     - A hash function should not be reading outside the bounds of the key.
        //     - Flipping a bit of a key should, with overwhelmingly high probability, result in a different hash.
        //     - Hashing the same key twice should always produce the same result.
        //     - The memory alignment of the key should not affect the hash result.
        public static bool SanityTest(IHasher hasher)
        {
            var rnd = new Random(883741);

            bool result = true;

            const int reps = 10;
            const int keymax = 256;
            const int pad = 16;
            const int buflen = keymax + pad*3;
  
            byte[] buffer1 = new byte[buflen];
            byte[] buffer2 = new byte[buflen];
            //----------
            for (int irep = 0; irep < reps; irep++)
            {
                if (irep % (reps/10) == 0) Console.Write(".");

                for (int len = 4; len <= keymax; len++)
                {
                    for(int offset = pad; offset < pad*2; offset++)
                    {
//                        byte* key1 = &buffer1[pad];
//                        byte* key2 = &buffer2[pad+offset];

                        rnd.NextBytes(buffer1);
                        rnd.NextBytes(buffer2);

                        //memcpy(key2, key1, len);
                        Buffer.BlockCopy(buffer2, pad + offset, buffer1, pad, len);

                        // hash1 = hash(key1, len, 0)
                        var hash1 = hasher.Hash(buffer1, pad, (uint)len, 0);

                        for(int bit = 0; bit < (len * 8); bit++)
                        {
                            // Flip a bit, hash the key -> we should get a different result.
                            //Flipbit(key2,len,bit);
                            Flipbit(buffer2, pad + offset, len, bit);
                            var hash2 = hasher.Hash(buffer2, pad + offset, (uint)len, 0);

                            if (hash1 == hash2)
                                result = false;

                            // Flip it back, hash again -> we should get the original result.
                            //flipbit(key2,len,bit);
                            Flipbit(buffer2, pad + offset, len, bit);
                            //hash(key2, len, 0, hash2);
                            hash2 = hasher.Hash(buffer2, pad + offset, (uint)len, 0);

                            if (hash1 != hash2)
                                result = false;
                        }
                    }
                }
            }
            return result;
        }
开发者ID:Kristinn-Stefansson,项目名称:EventStore,代码行数:64,代码来源:SMHasher.cs

示例5: Hash

        /// <summary>
        /// Hashes the specified input using the specified <see cref="IHasher"/>.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="hasher"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string Hash(string input, IHasher hasher, Encoding encoding = null)
        {
            var hash = hasher.Hash((encoding ?? Encoding.UTF8).GetBytes(input));

            return BitConverter.ToString(hash).Replace("-", "");
        }
开发者ID:ryanwentzel,项目名称:DesignPatterns,代码行数:13,代码来源:HashUtil.cs

示例6: TestFixtureSetUp

 public override void TestFixtureSetUp()
 {
     hasher = new Murmur3AUnsafe();
     base.TestFixtureSetUp();
     _files.Add(GetTempFilePath());
     var table = new HashListMemTable(PTableVersions.Index32Bit, maxSize: 20);
     table.Add(0x010100000000, 0, 1);
     table.Add(0x010200000000, 0, 2);
     table.Add(0x010300000000, 0, 3);
     table.Add(0x010300000000, 1, 4);
     _tables.Add(PTable.FromMemtable(table, GetTempFilePath()));
     table = new HashListMemTable(PTableVersions.Index64Bit, maxSize: 20);
     table.Add(0x010100000000, 2, 5);
     table.Add(0x010200000000, 1, 6);
     table.Add(0x010200000000, 2, 7);
     table.Add(0x010400000000, 0, 8);
     table.Add(0x010400000000, 1, 9);
     _tables.Add(PTable.FromMemtable(table, GetTempFilePath()));
     table = new HashListMemTable(PTableVersions.Index32Bit, maxSize: 20);
     table.Add(0x010100000000, 1, 10);
     table.Add(0x010100000000, 2, 11);
     table.Add(0x010500000000, 1, 12);
     table.Add(0x010500000000, 2, 13);
     table.Add(0x010500000000, 3, 14);
     _tables.Add(PTable.FromMemtable(table, GetTempFilePath()));
     _newtable = PTable.MergeTo(_tables, GetTempFilePath(), (streamId, hash) => hash << 32 | hasher.Hash(streamId), x => x.Position % 2 == 0, x => new Tuple<string, bool>(x.Stream.ToString(), x.Position % 2 == 0), PTableVersions.Index64Bit);
 }
开发者ID:EventStore,项目名称:EventStore,代码行数:27,代码来源:when_merging_ptables.cs


注:本文中的IHasher.Hash方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。