Skip to content

Commit 95cd658

Browse files
authored
Ensure Endianness in xxHash
Signed-off-by: Xen <lordofxen@deskasoft.com>
1 parent ce22c0a commit 95cd658

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

HashifyNet/Algorithms/XxHash/XxHash_Implementation.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -152,19 +152,19 @@ protected override void TransformByteGroupsInternal(ArraySegment<byte> data)
152152

153153
for (var currentIndex = dataOffset; currentIndex < endOffset; currentIndex += 16)
154154
{
155-
tempA += BitConverter.ToUInt32(dataArray, currentIndex) * tempPrime1;
155+
tempA += Endianness.ToUInt32LittleEndian(dataArray, currentIndex) * tempPrime1;
156156
tempA = RotateLeft(tempA, 13);
157157
tempA *= tempPrime0;
158158

159-
tempB += BitConverter.ToUInt32(dataArray, currentIndex + 4) * tempPrime1;
159+
tempB += Endianness.ToUInt32LittleEndian(dataArray, currentIndex + 4) * tempPrime1;
160160
tempB = RotateLeft(tempB, 13);
161161
tempB *= tempPrime0;
162162

163-
tempC += BitConverter.ToUInt32(dataArray, currentIndex + 8) * tempPrime1;
163+
tempC += Endianness.ToUInt32LittleEndian(dataArray, currentIndex + 8) * tempPrime1;
164164
tempC = RotateLeft(tempC, 13);
165165
tempC *= tempPrime0;
166166

167-
tempD += BitConverter.ToUInt32(dataArray, currentIndex + 12) * tempPrime1;
167+
tempD += Endianness.ToUInt32LittleEndian(dataArray, currentIndex + 12) * tempPrime1;
168168
tempD = RotateLeft(tempD, 13);
169169
tempD *= tempPrime0;
170170
}
@@ -204,7 +204,7 @@ protected override IHashValue FinalizeHashValueInternal(CancellationToken cancel
204204

205205
for (var currentOffset = 0; currentOffset < endOffset; currentOffset += 4)
206206
{
207-
hashValue += BitConverter.ToUInt32(remainder, currentOffset) * _primes32[2];
207+
hashValue += Endianness.ToUInt32LittleEndian(remainder, currentOffset) * _primes32[2];
208208
hashValue = RotateLeft(hashValue, 17) * _primes32[3];
209209
}
210210
}
@@ -229,7 +229,7 @@ protected override IHashValue FinalizeHashValueInternal(CancellationToken cancel
229229
hashValue ^= hashValue >> 16;
230230

231231
return new HashValue(
232-
BitConverter.GetBytes(hashValue),
232+
Endianness.GetBytesLittleEndian(hashValue),
233233
32);
234234
}
235235

@@ -312,19 +312,19 @@ protected override void TransformByteGroupsInternal(ArraySegment<byte> data)
312312

313313
for (var currentIndex = dataOffset; currentIndex < endOffset; currentIndex += 32)
314314
{
315-
tempA += BitConverter.ToUInt64(dataArray, currentIndex) * tempPrime1;
315+
tempA += Endianness.ToUInt64LittleEndian(dataArray, currentIndex) * tempPrime1;
316316
tempA = RotateLeft(tempA, 31);
317317
tempA *= tempPrime0;
318318

319-
tempB += BitConverter.ToUInt64(dataArray, currentIndex + 8) * tempPrime1;
319+
tempB += Endianness.ToUInt64LittleEndian(dataArray, currentIndex + 8) * tempPrime1;
320320
tempB = RotateLeft(tempB, 31);
321321
tempB *= tempPrime0;
322322

323-
tempC += BitConverter.ToUInt64(dataArray, currentIndex + 16) * tempPrime1;
323+
tempC += Endianness.ToUInt64LittleEndian(dataArray, currentIndex + 16) * tempPrime1;
324324
tempC = RotateLeft(tempC, 31);
325325
tempC *= tempPrime0;
326326

327-
tempD += BitConverter.ToUInt64(dataArray, currentIndex + 24) * tempPrime1;
327+
tempD += Endianness.ToUInt64LittleEndian(dataArray, currentIndex + 24) * tempPrime1;
328328
tempD = RotateLeft(tempD, 31);
329329
tempD *= tempPrime0;
330330
}
@@ -398,7 +398,7 @@ protected override IHashValue FinalizeHashValueInternal(CancellationToken cancel
398398
// In 8-byte chunks, process all full chunks
399399
for (int x = 0; x < remainder.Length / 8; ++x)
400400
{
401-
hashValue ^= RotateLeft(BitConverter.ToUInt64(remainder, x * 8) * _primes64[1], 31) * _primes64[0];
401+
hashValue ^= RotateLeft(Endianness.ToUInt64LittleEndian(remainder, x * 8) * _primes64[1], 31) * _primes64[0];
402402
hashValue = (RotateLeft(hashValue, 27) * _primes64[0]) + _primes64[3];
403403
}
404404

@@ -407,7 +407,7 @@ protected override IHashValue FinalizeHashValueInternal(CancellationToken cancel
407407
{
408408
var startOffset = remainderLength - (remainderLength % 8);
409409

410-
hashValue ^= BitConverter.ToUInt32(remainder, startOffset) * _primes64[0];
410+
hashValue ^= Endianness.ToUInt32LittleEndian(remainder, startOffset) * _primes64[0];
411411
hashValue = (RotateLeft(hashValue, 23) * _primes64[1]) + _primes64[2];
412412
}
413413

@@ -431,7 +431,7 @@ protected override IHashValue FinalizeHashValueInternal(CancellationToken cancel
431431
hashValue ^= hashValue >> 32;
432432

433433
return new HashValue(
434-
BitConverter.GetBytes(hashValue),
434+
Endianness.GetBytesLittleEndian(hashValue),
435435
64);
436436
}
437437

0 commit comments

Comments
 (0)