|
| 1 | +/* |
| 2 | + * Derived from MurmurHash2Simple, |
| 3 | + * http://landman-code.blogspot.com/2009/02/c-superfasthash-and-murmurhash2.html |
| 4 | + */ |
| 5 | + |
| 6 | +/***** BEGIN LICENSE BLOCK ***** |
| 7 | + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 8 | + * |
| 9 | + * The contents of this file are subject to the Mozilla Public License Version |
| 10 | + * 1.1 (the "License"); you may not use this file except in compliance with |
| 11 | + * the License. You may obtain a copy of the License at |
| 12 | + * http://www.mozilla.org/MPL/ |
| 13 | + * |
| 14 | + * Software distributed under the License is distributed on an "AS IS" basis, |
| 15 | + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 16 | + * for the specific language governing rights and limitations under the |
| 17 | + * License. |
| 18 | + * |
| 19 | + * The Original Code is HashTableHashing.MurmurHash2. |
| 20 | + * |
| 21 | + * The Initial Developer of the Original Code is |
| 22 | + * Davy Landman. |
| 23 | + * Portions created by the Initial Developer are Copyright (C) 2009 |
| 24 | + * the Initial Developer. All Rights Reserved. |
| 25 | + * |
| 26 | + * Contributor(s): |
| 27 | + * |
| 28 | + * |
| 29 | + * Alternatively, the contents of this file may be used under the terms of |
| 30 | + * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 31 | + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 32 | + * in which case the provisions of the GPL or the LGPL are applicable instead |
| 33 | + * of those above. If you wish to allow use of your version of this file only |
| 34 | + * under the terms of either the GPL or the LGPL, and not to allow others to |
| 35 | + * use your version of this file under the terms of the MPL, indicate your |
| 36 | + * decision by deleting the provisions above and replace them with the notice |
| 37 | + * and other provisions required by the GPL or the LGPL. If you do not delete |
| 38 | + * the provisions above, a recipient may use your version of this file under |
| 39 | + * the terms of any one of the MPL, the GPL or the LGPL. |
| 40 | + * |
| 41 | + * ***** END LICENSE BLOCK ***** */ |
| 42 | + |
| 43 | +using System; |
| 44 | +using System.Text; |
| 45 | + |
| 46 | +namespace NHibernate.Util |
| 47 | +{ |
| 48 | + /// <summary>A stable hasher using MurmurHash2 algorithm.</summary> |
| 49 | + internal static class Hasher |
| 50 | + { |
| 51 | + internal static string HashToString(string input) |
| 52 | + { |
| 53 | + var hash = Hash(input); |
| 54 | + return hash.ToString("X"); |
| 55 | + } |
| 56 | + |
| 57 | + internal static uint Hash(string input) |
| 58 | + { |
| 59 | + return Hash(Encoding.UTF8.GetBytes(input)); |
| 60 | + } |
| 61 | + |
| 62 | + internal static uint Hash(byte[] data) |
| 63 | + { |
| 64 | + return Hash(data, 0xc58f1a7b); |
| 65 | + } |
| 66 | + |
| 67 | + private const uint _m = 0x5bd1e995; |
| 68 | + private const int _r = 24; |
| 69 | + |
| 70 | + internal static uint Hash(byte[] data, uint seed) |
| 71 | + { |
| 72 | + var length = data.Length; |
| 73 | + if (length == 0) |
| 74 | + return 0; |
| 75 | + var h = seed ^ (uint) length; |
| 76 | + var currentIndex = 0; |
| 77 | + while (length >= 4) |
| 78 | + { |
| 79 | + var k = BitConverter.ToUInt32(data, currentIndex); |
| 80 | + k *= _m; |
| 81 | + k ^= k >> _r; |
| 82 | + k *= _m; |
| 83 | + |
| 84 | + h *= _m; |
| 85 | + h ^= k; |
| 86 | + currentIndex += 4; |
| 87 | + length -= 4; |
| 88 | + } |
| 89 | + |
| 90 | + switch (length) |
| 91 | + { |
| 92 | + case 3: |
| 93 | + h ^= BitConverter.ToUInt16(data, currentIndex); |
| 94 | + h ^= (uint) data[currentIndex + 2] << 16; |
| 95 | + h *= _m; |
| 96 | + break; |
| 97 | + case 2: |
| 98 | + h ^= BitConverter.ToUInt16(data, currentIndex); |
| 99 | + h *= _m; |
| 100 | + break; |
| 101 | + case 1: |
| 102 | + h ^= data[currentIndex]; |
| 103 | + h *= _m; |
| 104 | + break; |
| 105 | + } |
| 106 | + |
| 107 | + // Do a few final mixes of the hash to ensure the last few |
| 108 | + // bytes are well-incorporated. |
| 109 | + |
| 110 | + h ^= h >> 13; |
| 111 | + h *= _m; |
| 112 | + h ^= h >> 15; |
| 113 | + |
| 114 | + return h; |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments