本文整理汇总了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;
}
}
示例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);
}
示例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));
}
示例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;
}
示例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("-", "");
}
示例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);
}