Skip to content

Commit 92917ee

Browse files
committed
Clean up some of the has sum computing.
1 parent 2c3ef7c commit 92917ee

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

ProbabilisticDataStructures/CuckooBloomFilter.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ private bool Insert(uint i1, uint i2, byte[] f)
363363
var tempF = f;
364364
f = this.Buckets[bucketIdx][entryIdx];
365365
this.Buckets[bucketIdx][entryIdx] = tempF;
366-
i = i ^ ProbabilisticDataStructures.ToBigEndianUInt32(this.ComputeHash(f));
366+
i = i ^ ComputeHashSum32(f);
367367
var b = this.Buckets[i % this.M];
368368

369369
idx = GetEmptyEntry(b);
@@ -389,8 +389,8 @@ private Components GetComponents(byte[] data)
389389
{
390390
var hash = this.ComputeHash(data);
391391
var f = hash.Take((int)this.F).ToArray();
392-
var i1 = ProbabilisticDataStructures.ToBigEndianUInt32(hash);
393-
var i2 = ProbabilisticDataStructures.ToBigEndianUInt32(this.ComputeHash(f));
392+
var i1 = this.ComputeHashSum32(hash);
393+
var i2 = this.ComputeHashSum32(f);
394394

395395
return Components.Create(f, i1, i2);
396396
}
@@ -408,6 +408,19 @@ private byte[] ComputeHash(byte[] data)
408408
return sum;
409409
}
410410

411+
/// <summary>
412+
/// Returns the sum of the hash.
413+
/// </summary>
414+
/// <param name="data">Data</param>
415+
/// <returns>32-bit hash value</returns>
416+
private uint ComputeHashSum32(byte[] data)
417+
{
418+
var hash = new Hash(this.Hash);
419+
hash.ComputeHash(data);
420+
var sum = hash.Sum();
421+
return ProbabilisticDataStructures.ToBigEndianUInt32(sum);
422+
}
423+
411424
/// <summary>
412425
/// Returns the optimal fingerprint length in bytes for the given bucket size and
413426
/// false-positive rate epsilon.

ProbabilisticDataStructures/ProbabilisticDataStructures.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ public static HashKernelReturnValue HashKernel(byte[] data, HashAlgorithm algori
5555
);
5656
}
5757

58+
/// <summary>
59+
/// Returns the default hashing algorithm for the library.
60+
/// </summary>
61+
/// <returns>The default hashing algorithm for the library</returns>
62+
internal static HashAlgorithm GetDefaultHashAlgorithm()
63+
{
64+
return HashAlgorithm.Create("MD5");
65+
}
66+
5867
public static uint ToBigEndianUInt32(byte[] bytes)
5968
{
6069
if (BitConverter.IsLittleEndian)

0 commit comments

Comments
 (0)