我正在尝试将一个C#实现MurmurHash3移植到VB.Net。
它运行..。但是有人能为我提供一些已知的测试向量来验证正确性吗?
提前谢谢。
编辑: --我把实现限制在32位MurmurHash3上,但是如果你也能为64位实现提供向量,也会很好。
发布于 2015-08-10 21:31:00
我终于开始创建一个MurMur3实现,并成功地翻译了SMHasher测试代码。我的实现给出了与SMHasher测试相同的结果。这意味着我最终可以给出一些有用的,并且假设是正确的测试向量。
这只适用于Murmur3_x86_32
| Input | Seed | Expected |
|--------------|------------|------------|
| (no bytes) | 0 | 0 | with zero data and zero seed, everything becomes zero
| (no bytes) | 1 | 0x514E28B7 | ignores nearly all the math
| (no bytes) | 0xffffffff | 0x81F16F39 | make sure your seed uses unsigned 32-bit math
| FF FF FF FF | 0 | 0x76293B50 | make sure 4-byte chunks use unsigned math
| 21 43 65 87 | 0 | 0xF55B516B | Endian order. UInt32 should end up as 0x87654321
| 21 43 65 87 | 0x5082EDEE | 0x2362F9DE | Special seed value eliminates initial key with xor
| 21 43 65 | 0 | 0x7E4A8634 | Only three bytes. Should end up as 0x654321
| 21 43 | 0 | 0xA0F7B07A | Only two bytes. Should end up as 0x4321
| 21 | 0 | 0x72661CF4 | Only one byte. Should end up as 0x21
| 00 00 00 00 | 0 | 0x2362F9DE | Make sure compiler doesn't see zero and convert to null
| 00 00 00 | 0 | 0x85F0B427 |
| 00 00 | 0 | 0x30F4C306 |
| 00 | 0 | 0x514E28B7 |对于那些将要移植到没有实际数组的语言的人来说,我也有一些基于字符串的测试。对于这些测试:
我会把这些放在代码表格中:
TestString("", 0, 0); //empty string with zero seed should give zero
TestString("", 1, 0x514E28B7);
TestString("", 0xffffffff, 0x81F16F39); //make sure seed value is handled unsigned
TestString("\0\0\0\0", 0, 0x2362F9DE); //make sure we handle embedded nulls
TestString("aaaa", 0x9747b28c, 0x5A97808A); //one full chunk
TestString("aaa", 0x9747b28c, 0x283E0130); //three characters
TestString("aa", 0x9747b28c, 0x5D211726); //two characters
TestString("a", 0x9747b28c, 0x7FA09EA6); //one character
//Endian order within the chunks
TestString("abcd", 0x9747b28c, 0xF0478627); //one full chunk
TestString("abc", 0x9747b28c, 0xC84A62DD);
TestString("ab", 0x9747b28c, 0x74875592);
TestString("a", 0x9747b28c, 0x7FA09EA6);
TestString("Hello, world!", 0x9747b28c, 0x24884CBA);
//Make sure you handle UTF-8 high characters. A bcrypt implementation messed this up
TestString("ππππππππ", 0x9747b28c, 0xD58063C1); //U+03C0: Greek Small Letter Pi
//String of 256 characters.
//Make sure you don't store string lengths in a char, and overflow at 255 bytes (as OpenBSD's canonical BCrypt implementation did)
TestString(StringOfChar("a", 256), 0x9747b28c, 0x37405BDC);我将只发布我转换为Murmur3的11个SHA-2测试向量中的两个。
TestString("abc", 0, 0xB3DD93FA);
TestString("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 0, 0xEE925B90);最后,最重要的是:
"The quick brown fox jumps over the lazy dog"如果其他人可以从它们的实现中确认任何/所有这些向量。
而且,同样,这些测试向量来自通过SMHasher 256迭代循环测试的KeySetTest.cpp - VerificationTest(...)实现。
这些测试来自于我在Delphi中的实现。我还在Lua中创建了一个实现(在支持数组方面不是很大)。
注意事项:发布到公共域中的任何代码。不需要归属。
发布于 2013-05-28 15:39:56
SMHasher使用一个检查散列是否正常工作的小例程,基本上计算以下值的散列数,对每个值使用一个递减的种子值(从256个开始):
' The comment in the SMHasher code is a little wrong -
' it's missing the first case.
{}, {0}, {0, 1}, {0, 1, 2} ... {0, 1, 2, ... 254}并将其追加到HASHLENGTH * 256长度数组中,换句话说:
' Where & is a byte array concatenation.
HashOf({}, 256) &
HashOf({0}, 255) &
HashOf({0, 1}, 254) &
...
HashOf({0, 1, ... 254), 1)然后,它接受那个大数组的散列。最后哈希的前4个字节被解释为一个无符号的32位整数,并根据验证代码进行检查:
MurmurHash3 x86 32 0xB0F57EE3MurmurHash3 x86 128 0xB3ECE62AMurmurHash3 x64 128 0x6384BA69不幸的是这是我唯一能找到的公开测试。我想另一个选择是编写一个快速的C应用程序并散列一些值。
这是我的验证器的C#实现。
static void VerificationTest(uint expected)
{
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);
if (verification == expected)
Console.WriteLine("Verification passed.");
else
Console.WriteLine("Verification failed, got {0:x8}, expected {1:x8}", verification, expected);
}
}发布于 2016-03-18 23:04:46
我改进了乔纳森的救生代码。您的Murmur3必须实现ICryptoTransform才能使此方法工作。您可以在github上找到实现此接口的一个。
public static void VerificationTest(uint expected)
{
using (var hash = new Murmur32ManagedX86())
{
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 mur = new Murmur32ManagedX86((uint)(256 - i)))
{
var computed = mur.ComputeHash(key, 0,i);
cs.Write(computed, 0, 4);
}
}
cs.FlushFinalBlock();
var testBoy = hash.Seed;
var final = hash.Hash;
var verification = ((uint)final[0]) | ((uint)final[1] << 8) | ((uint)final[2] << 16) | ((uint)final[3] << 24);
if (verification == expected)
Console.WriteLine("Verification passed.");
else
Console.WriteLine("Verification failed, got {0:x8}, expected {1:x8}", verification, expected);
}
}
}如果您使用的实现没有ICryptoTransform接口,但只处理字节并返回int (也可以轻松修改以使用byte[] )。下面是该函数的测试函数:
public static void VerificationTest(uint expected)
{
using (var stream = new MemoryStream())
{
var key = new byte[256];
for (var i = 0; i < 256; i++)
{
key[i] = (byte)i;
var hasher = new MurMurHash3((uint)(256 - i));
int computed = hasher.ComputeBytesFast(key.Take(i).ToArray());
stream.Write(BitConverter.GetBytes(computed), 0, 4);
}
var finalHasher = new MurMurHash3(0); //initial seed = 0
int result = finalHasher.ComputeBytesFast2(stream.GetBuffer());
if (result == (int)expected)
Console.WriteLine("Verification passed.");
else
Console.WriteLine("Verification failed, got {0:x8}, expected {1:x8}", verification, expected);
}
}https://stackoverflow.com/questions/14747343
复制相似问题