Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 109 lines (89 sloc) 2.741 kB
ff351fa @enyim VBucket support
authored
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Security.Cryptography;
6
7 namespace Enyim
8 {
9 /// <summary>
10 /// This is Jenkin's "One at A time Hash".
11 /// http://en.wikipedia.org/wiki/Jenkins_hash_function
12 ///
13 /// Coming from libhashkit.
14 /// </summary>
15 /// <remarks>Does not support block based hashing.</remarks>
16 internal class HashkitOneAtATime : HashAlgorithm, IUIntHashAlgorithm
17 {
18 public HashkitOneAtATime()
19 {
20 this.HashSizeValue = 32;
21 }
22
23 public override bool CanTransformMultipleBlocks
24 {
25 get { return false; }
26 }
27
28 public override void Initialize() { }
29
30 protected override void HashCore(byte[] array, int ibStart, int cbSize)
31 {
32 if (array == null) throw new ArgumentNullException("array");
33 if (ibStart < 0 || ibStart > array.Length) throw new ArgumentOutOfRangeException("ibStart");
34 if (ibStart + cbSize > array.Length) throw new ArgumentOutOfRangeException("cbSize");
35
36 HashkitOneAtATime.UnsafeHashCore(array, ibStart, cbSize);
37 }
38
39 protected override byte[] HashFinal()
40 {
41 return BitConverter.GetBytes(this.CurrentHash);
42 }
43
44 public uint CurrentHash { get; private set; }
45
46 #region [ UnsafeHashCore ]
47
48 // see the murmur hash about stream support
49 private static unsafe uint UnsafeHashCore(byte[] data, int offset, int count)
50 {
51 uint hash = 0;
52
53 fixed (byte* start = &(data[offset]))
54 {
55 var ptr = start;
56
57 while (count > 0)
58 {
59 hash += *ptr;
60 hash += (hash << 10);
61 hash ^= (hash >> 6);
62
63 count--;
64 ptr++;
65 }
66 }
67
68 hash += (hash << 3);
69 hash ^= (hash >> 11);
70 hash += (hash << 15);
71
72 return hash;
73 }
74 #endregion
75 #region [ IUIntHash ]
76
77 uint IUIntHashAlgorithm.ComputeHash(byte[] data)
78 {
79 this.Initialize();
80
81 this.HashCore(data, 0, data.Length);
82
83 return this.CurrentHash;
84 }
85
86 #endregion
87 }
88 }
89
90 #region [ License information ]
91 /* ************************************************************
92 *
93 * Copyright (c) 2010 Attila Kiskó, enyim.com
94 *
95 * Licensed under the Apache License, Version 2.0 (the "License");
96 * you may not use this file except in compliance with the License.
97 * You may obtain a copy of the License at
98 *
99 * http://www.apache.org/licenses/LICENSE-2.0
100 *
101 * Unless required by applicable law or agreed to in writing, software
102 * distributed under the License is distributed on an "AS IS" BASIS,
103 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
104 * See the License for the specific language governing permissions and
105 * limitations under the License.
106 *
107 * ************************************************************/
108 #endregion
Something went wrong with that request. Please try again.