# Murmur哈希

MurmurHash 是一种非加密哈希函数，适用于一般的哈希检索操作。[1][2][3] 由Austin Appleby在2008年发明，[4][5] 并出现了多个变种，[6] 都已经发布到了公有领域(public domain)。与其它流行的哈希函数相比，对于规律性较强的key，MurmurHash的随机分布特征表现更良好。[7]

## 算法

```Murmur3_32(key, len, seed)
c1 $\gets$ 0xcc9e2d51
c2 $\gets$ 0x1b873593
r1 $\gets$ 15
r2 $\gets$ 13
m $\gets$ 5
n $\gets$ 0xe6546b64

hash $\gets$ seed

for each fourByteChunk of key
k $\gets$ fourByteChunk

k $\gets$ k * c1
k $\gets$ (k << r1) OR (k >> (32-r1))
k $\gets$ k * c2

hash $\gets$ hash XOR k
hash $\gets$ (hash << r2) OR (hash >> (32-r2))
hash $\gets$ hash * m + n

with any remainingBytesInKey
remainingBytes $\gets$ SwapEndianOrderOf(remainingBytesInKey)
remainingBytes $\gets$ remainingBytes * c1
remainingBytes $\gets$ (remainingBytes << r1) OR (remainingBytes >> (32 - r1))
remainingBytes $\gets$ remainingBytes * c2

hash $\gets$ hash XOR remainingBytes

hash $\gets$ hash XOR len

hash $\gets$ hash XOR (hash >> 16)
hash $\gets$ hash * 0x85ebca6b
hash $\gets$ hash XOR (hash >> 13)
hash $\gets$ hash * 0xc2b2ae35
hash $\gets$ hash XOR (hash >> 16)
```

## 参考

1. ^ 1.0 1.1 Hadoop in Java. Hbase.apache.org. 24 July 2011 [13 January 2012].
2. ^
3. ^ Couceiro et al. (PDF). [13 January 2012] <span style="font-family: sans-serif; cursor: default; color:#555; font-size: 0.8em; bottom: 0.1em; font-weight: bold;" title="连接到（葡萄牙文）网页">（（葡萄牙文）.
5. ^ Tanjent (tanjent) wrote,3 March 2008 13:31:00. MurmurHash first announcement. Tanjent.livejournal.com. [13 January 2012].
6. ^ MurmurHash2-160. Simonhf.wordpress.com. 25 September 2010 [13 January 2012].
7. ^ Which hashing algorithm is best for uniqueness and speed. stackexchange.com.
8. ^
9. ^ 9.0 9.1 Horvath, Adam. MurMurHash3, an ultra fast hash algorithm for C# / .NET. Aug 10, 2012.
10. ^
11. ^ pyfasthash in Python. Google. [13 January 2012].
12. ^
13. ^ Landman, Davy. Davy Landman in C#. Landman-code.blogspot.com. [13 January 2012].
14. ^ Toru Maesaka in Perl. Search.cpan.org. [13 January 2012].
15. ^ Bruce Williams <http://codefluency.com>, for Ruby Central <http://rubycentral.org>. Ruby. Rubyforge.org. 3 May 2009 [13 January 2012].
16. ^ Murmurhash3 PHP extension. Murmur.vaizard.org. [13 January 2012].