blob: c703aa810d8b00875289cf04ef1e7f2edf672f08 [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyCommonHelper.EncryptionHelper
{
public class MyHash
{
public static uint GetMurmurHash2(byte[] data, uint seed = 0x9747b28c)
{
const uint m = 0x5bd1e995;
const int r = 24;
uint len = (uint)data.Length;
uint h = seed ^ len;
int currentIndex = 0;
while (len >= 4)
{
uint k = (uint)(data[currentIndex++] | data[currentIndex++] << 8 | data[currentIndex++] << 16 | data[currentIndex++] << 24);
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
len -= 4;
}
switch (len)
{
case 3:
h ^= (ushort)(data[currentIndex++] | data[currentIndex++] << 8);
h ^= (uint)data[currentIndex] << 16;
h *= m;
break;
case 2:
h ^= (ushort)(data[currentIndex++] | data[currentIndex] << 8);
h *= m;
break;
case 1:
h ^= data[currentIndex];
h *= m;
break;
}
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
}
}