Skip to content

Commit 6179df6

Browse files
authored
Change BitConverter to Endianness
Signed-off-by: Xen <lordofxen@deskasoft.com>
1 parent c3af947 commit 6179df6

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

HashifyNet/Algorithms/SpookyHash/SpookyHashV2_Implementation.cs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -222,24 +222,23 @@ protected override IHashValue FinalizeHashValueInternal(CancellationToken cancel
222222
{
223223
case 32:
224224
return new HashValue(
225-
BitConverter.GetBytes((uint)finalHashValue[0]),
225+
Endianness.GetBytesLittleEndian((uint)finalHashValue[0]),
226226
32);
227227

228228
case 64:
229229
return new HashValue(
230-
BitConverter.GetBytes(finalHashValue[0]),
230+
Endianness.GetBytesLittleEndian(finalHashValue[0]),
231231
64);
232232

233233
case 128:
234234
var hashValueResult = new byte[16];
235235

236-
BitConverter.GetBytes(finalHashValue[0])
236+
Endianness.GetBytesLittleEndian(finalHashValue[0])
237237
.CopyTo(hashValueResult, 0);
238238

239-
BitConverter.GetBytes(finalHashValue[1])
239+
Endianness.GetBytesLittleEndian(finalHashValue[1])
240240
.CopyTo(hashValueResult, 8);
241241

242-
243242
return new HashValue(hashValueResult, 128);
244243

245244
default:
@@ -283,7 +282,6 @@ private IHashValue ComputeShortHashInternal(CancellationToken cancellationToken)
283282
if (dataArray != null)
284283
{
285284
var currentOffset = 0;
286-
287285
var remainderCount = dataCount % 32;
288286

289287
// Process 32-byte groups
@@ -292,13 +290,13 @@ private IHashValue ComputeShortHashInternal(CancellationToken cancellationToken)
292290

293291
while (currentOffset < endOffset)
294292
{
295-
tempHashValue[2] += BitConverter.ToUInt64(dataArray, currentOffset);
296-
tempHashValue[3] += BitConverter.ToUInt64(dataArray, currentOffset + 8);
293+
tempHashValue[2] += Endianness.ToUInt64LittleEndian(dataArray, currentOffset);
294+
tempHashValue[3] += Endianness.ToUInt64LittleEndian(dataArray, currentOffset + 8);
297295

298296
ShortMix(tempHashValue);
299297

300-
tempHashValue[0] += BitConverter.ToUInt64(dataArray, currentOffset + 16);
301-
tempHashValue[1] += BitConverter.ToUInt64(dataArray, currentOffset + 24);
298+
tempHashValue[0] += Endianness.ToUInt64LittleEndian(dataArray, currentOffset + 16);
299+
tempHashValue[1] += Endianness.ToUInt64LittleEndian(dataArray, currentOffset + 24);
302300

303301
currentOffset += 32;
304302
}
@@ -307,8 +305,8 @@ private IHashValue ComputeShortHashInternal(CancellationToken cancellationToken)
307305
// Process 16-byte group (if available)
308306
if (remainderCount >= 16)
309307
{
310-
tempHashValue[2] += BitConverter.ToUInt64(dataArray, currentOffset);
311-
tempHashValue[3] += BitConverter.ToUInt64(dataArray, currentOffset + 8);
308+
tempHashValue[2] += Endianness.ToUInt64LittleEndian(dataArray, currentOffset);
309+
tempHashValue[3] += Endianness.ToUInt64LittleEndian(dataArray, currentOffset + 8);
312310

313311
ShortMix(tempHashValue);
314312

@@ -323,16 +321,14 @@ private IHashValue ComputeShortHashInternal(CancellationToken cancellationToken)
323321
var finalRemainderBuffer = new byte[16];
324322
Array.Copy(dataArray, currentOffset, finalRemainderBuffer, 0, remainderCount);
325323

326-
tempHashValue[3] += BitConverter.ToUInt64(finalRemainderBuffer, 8);
327-
tempHashValue[2] += BitConverter.ToUInt64(finalRemainderBuffer, 0);
328-
324+
tempHashValue[3] += Endianness.ToUInt64LittleEndian(finalRemainderBuffer, 8);
325+
tempHashValue[2] += Endianness.ToUInt64LittleEndian(finalRemainderBuffer, 0);
329326
}
330327
else
331328
{
332329
tempHashValue[3] += 0XDEADBEEFDEADBEEF;
333330
tempHashValue[2] += 0XDEADBEEFDEADBEEF;
334331
}
335-
336332
}
337333
else
338334
{
@@ -346,21 +342,21 @@ private IHashValue ComputeShortHashInternal(CancellationToken cancellationToken)
346342
{
347343
case 32:
348344
return new HashValue(
349-
BitConverter.GetBytes((uint)tempHashValue[0]),
345+
Endianness.GetBytesLittleEndian((uint)tempHashValue[0]),
350346
32);
351347

352348
case 64:
353349
return new HashValue(
354-
BitConverter.GetBytes(tempHashValue[0]),
350+
Endianness.GetBytesLittleEndian(tempHashValue[0]),
355351
64);
356352

357353
case 128:
358354
var finalHashValue = new byte[16];
359355

360-
BitConverter.GetBytes(tempHashValue[0])
356+
Endianness.GetBytesLittleEndian(tempHashValue[0])
361357
.CopyTo(finalHashValue, 0);
362358

363-
BitConverter.GetBytes(tempHashValue[1])
359+
Endianness.GetBytesLittleEndian(tempHashValue[1])
364360
.CopyTo(finalHashValue, 8);
365361

366362
return new HashValue(
@@ -384,7 +380,7 @@ private static void Mix(ulong[] hashValue, ArraySegment<byte> data)
384380
{
385381
for (var i = 0; i < 12; ++i)
386382
{
387-
hashValue[i] += BitConverter.ToUInt64(dataArray, currentOffset + (i * 8));
383+
hashValue[i] += Endianness.ToUInt64LittleEndian(dataArray, currentOffset + (i * 8));
388384
hashValue[(i + 2) % 12] ^= hashValue[(i + 10) % 12];
389385
hashValue[(i + 11) % 12] ^= hashValue[i];
390386
hashValue[i] = RotateLeft(hashValue[i], _MixRotationParameters[i]);
@@ -397,7 +393,7 @@ private static void End(ulong[] hashValue, byte[] dataArray)
397393
{
398394
for (int i = 0; i < 12; ++i)
399395
{
400-
hashValue[i] += BitConverter.ToUInt64(dataArray, i * 8);
396+
hashValue[i] += Endianness.ToUInt64LittleEndian(dataArray, i * 8);
401397
}
402398

403399
EndPartial(hashValue);
@@ -446,4 +442,4 @@ private static ulong RotateLeft(ulong operand, int shiftCount)
446442
}
447443
}
448444
}
449-
}
445+
}

0 commit comments

Comments
 (0)