我正在尝试将C# implementation of MurmurHash3移植到VB.Net。
它运行...但有人可以为我提供一些已知的测试向量来验证正确性吗?
提前致谢。
编辑:我将实现限制为只有32位的MurmurHash3,但是如果你也可以为64位实现提供向量,那也很好。
答案 0 :(得分:9)
我终于开始创建一个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);
我将发布11个转换为Murmur3的SHA-2测试向量中的两个。
TestString("abc", 0, 0xB3DD93FA);
TestString("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 0, 0xEE925B90);
最后,最重要的一个:
"The quick brown fox jumps over the lazy dog"
如果其他人可以从他们的实现中确认任何/所有这些向量。
而且,这些测试向量来自于从KeySetTest.cpp - VerificationTest(...)
传递SMHasher 256迭代循环测试的实现。
这些测试来自我在Delphi中的实现。我还在Lua中创建了一个实现(在支持数组方面并不大)。
注意:任何已发布到公共领域的代码。无需归属。
答案 1 :(得分:5)
SMHasher使用little routine to check that the hashes are working,基本上它使用递减的种子值(从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
0xB0F57EE3
MurmurHash3 x86 128
0xB3ECE62A
MurmurHash3 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);
}
}
答案 2 :(得分:0)
我改进了Jonathan的救命代码。你的Murmur3必须实现.md
才能使用此方法。你可以在github找到一个
实现此接口。
ICryptoTransform
如果您使用的实现没有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[]