Using ComputeHash with a string will yield an "incorrect" hash because the library is casting a string instance into an unsafe char* which can cause a different hash to be returned depending on the system it's running on.
|
public static unsafe ulong ComputeHash(string str, uint seed = 0) |
|
{ |
|
Debug.Assert(str != null); |
|
|
|
fixed (char* c = str) |
|
{ |
|
byte* ptr = (byte*) c; |
|
int length = str.Length * 2; |
|
|
|
return UnsafeComputeHash(ptr, length, seed); |
|
} |
|
} |
The official .NET documentation clearly specifies that the default encoding can very between systems and additionally that the string and char types use UTF-16 internally
The correct approach here would be to create a stack-allocated Span<byte> and then use Encoding.UTF8.GetBytes.
Additionally an optional encoding parameter could also be added, with the default being UTF8.
Example:

Using
ComputeHashwith a string will yield an "incorrect" hash because the library is casting astringinstance into an unsafechar*which can cause a different hash to be returned depending on the system it's running on.xxHash/src/Standart.Hash.xxHash/xxHash64.cs
Lines 243 to 254 in 6b20e7f
The official .NET documentation clearly specifies that the default encoding can very between systems and additionally that the
stringandchartypes use UTF-16 internallyThe correct approach here would be to create a stack-allocated
Span<byte>and then useEncoding.UTF8.GetBytes.Additionally an optional encoding parameter could also be added, with the default being UTF8.
Example:
