Skip to content

Commit 4a408de

Browse files
committed
Add xxHash32 hasher class (nw)
1 parent 3469d42 commit 4a408de

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

SabreTools.Hashing/XxHash/XxHash32.cs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
3+
namespace SabreTools.Hashing.XxHash
4+
{
5+
internal class XxHash32
6+
{
7+
/// <summary>
8+
/// The 32-bit seed to alter the hash result predictably.
9+
/// </summary>
10+
private uint _seed;
11+
12+
/// <summary>
13+
/// Internal xxHash32 state
14+
/// </summary>
15+
private readonly XXH32State _state;
16+
17+
public XxHash32(uint seed = 0)
18+
{
19+
_seed = seed;
20+
_state = new XXH32State();
21+
_state.Reset(seed);
22+
}
23+
24+
/// <summary>
25+
/// Reset the internal hashing state
26+
/// </summary>
27+
public void Reset()
28+
{
29+
_state.Reset(_seed);
30+
}
31+
32+
/// <summary>
33+
/// Hash a block of data and append it to the existing hash
34+
/// </summary>
35+
/// <param name="data">Byte array representing the data</param>
36+
/// <param name="offset">Offset in the byte array to include</param>
37+
/// <param name="length">Length of the data to hash</param>
38+
public void TransformBlock(byte[] data, int offset, int length)
39+
=> _state.Update(data, offset, length);
40+
41+
/// <summary>
42+
/// Finalize the hash and return as a byte array
43+
/// </summary>
44+
public byte[] Finalize()
45+
{
46+
uint hash = _state.Digest();
47+
return BitConverter.GetBytes(hash);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)