MurmurHash3测试向量

pep*_*uan 5 vb.net murmurhash

我正在尝试将MurmurHash3C#实现移植到VB.Net.

它运行...但有人可以提供一些已知的测试向量来验证正确性吗?

  • 已知的字符串文字
  • 种子价值
  • MurmurHash3的结果

提前致谢.

编辑:我将实现限制为只有32位MurmurHash3,但如果你也可以提供64位实现的向量,也会很好.

Ian*_*oyd 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 |
Run Code Online (Sandbox Code Playgroud)

对于那些将移植到没有实际数组的语言的人,我也有一些基于字符串的测试.对于这些测试:

  • 假设所有字符串都是UTF-8编码的
  • 并且不包括任何null终止符

我会以代码形式留下这些:

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);
Run Code Online (Sandbox Code Playgroud)

我将发布11个转换为Murmur3的SHA-2测试向量中的两个.

TestString("abc", 0, 0xB3DD93FA);
TestString("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 0, 0xEE925B90);
Run Code Online (Sandbox Code Playgroud)

最后,最重要的一个:

  • 键: "The quick brown fox jumps over the lazy dog"
  • 种子: 0x9747b28c
  • 哈希:0x2FA826CD

如果其他人可以从他们的实现中确认任何/所有这些向量.

而且,这些测试向量来自于通过SMHasher 256迭代循环测试的实现KeySetTest.cpp - VerificationTest(...).

这些测试来自我在Delphi中的实现.我还在Lua中创建了一个实现(在支持数组方面并不大).

注意:任何代码都会发布到公共领域.无需归属.

  • “如果其他人可以从其实现中确认任何/所有这些向量。” -- 已确认,https://github.com/airbreather/AirBreather.Common/blob/582e0453168f270f2fc88fdb4ccce559a9cc2089/Source/AirBreather.Common/AirBreather.Common.Tests/UTF8StringTests.cs#L47-L81 全部传递到我的 C# 实现中。谢谢! (3认同)
  • [这是JSON格式的测试用例](https://pastebin.com/9BNXbA6z) (2认同)

Jon*_*son 5

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}
Run Code Online (Sandbox Code Playgroud)

并将其附加到HASHLENGTH * 256长度数组,换句话说:

' Where & is a byte array concatenation.
HashOf({}, 256) &
HashOf({0}, 255) &
HashOf({0, 1}, 254) &
...
HashOf({0, 1, ... 254), 1)
Run Code Online (Sandbox Code Playgroud)

然后它接受那个大数组的哈希值.最终散列的前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);
    }
}
Run Code Online (Sandbox Code Playgroud)