我正在尝试将MurmurHash3 的 C# 实现移植到 VB.Net。
它运行...但是有人可以为我提供一些已知的测试向量来验证正确性吗?
- 已知字符串文本
- 种子价值
- MurmurHash3 的结果
提前致谢。
编辑:我将实现限制为仅 32 位 MurmurHash3,但如果您还可以为 64 位实现提供向量,那也很好。
我正在尝试将MurmurHash3 的 C# 实现移植到 VB.Net。
它运行...但是有人可以为我提供一些已知的测试向量来验证正确性吗?
提前致谢。
编辑:我将实现限制为仅 32 位 MurmurHash3,但如果您还可以为 64 位实现提供向量,那也很好。
我终于开始创建一个 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 中创建了一个实现(在支持数组方面并不大)。
注意:任何发布到公共领域的代码。无需归属。
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
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);
}
}
我改进了乔纳森的救生代码。您的 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);
}
}