Skip to content

Commit f1edeed

Browse files
authored
Fix XXHash for stripe size (#61881)
1 parent 8daae66 commit f1edeed

8 files changed

Lines changed: 44 additions & 2 deletions

File tree

src/libraries/System.IO.Hashing/src/System/IO/Hashing/XxHash32.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private static int StaticHash(ReadOnlySpan<byte> source, Span<byte> destination,
215215
int totalLength = source.Length;
216216
State state = new State((uint)seed);
217217

218-
while (source.Length > StripeSize)
218+
while (source.Length >= StripeSize)
219219
{
220220
state.ProcessStripe(source);
221221
source = source.Slice(StripeSize);

src/libraries/System.IO.Hashing/src/System/IO/Hashing/XxHash64.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private static int StaticHash(ReadOnlySpan<byte> source, Span<byte> destination,
215215
int totalLength = source.Length;
216216
State state = new State((ulong)seed);
217217

218-
while (source.Length > StripeSize)
218+
while (source.Length >= StripeSize)
219219
{
220220
state.ProcessStripe(source);
221221
source = source.Slice(StripeSize);

src/libraries/System.IO.Hashing/tests/XxHash32Tests.007.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public static IEnumerable<object[]> TestCases
3636
private const string DotNetHashesThis3 = DotNetHashesThis + DotNetHashesThis + DotNetHashesThis;
3737
private const string DotNetNCHashing = ".NET now has non-crypto hashing";
3838
private const string DotNetNCHashing3 = DotNetNCHashing + DotNetNCHashing + DotNetNCHashing;
39+
private const string SixteenBytes = ".NET Hashes This";
40+
private const string SixteenBytes3 = SixteenBytes + SixteenBytes + SixteenBytes;
3941

4042
protected static IEnumerable<TestCase> TestCaseDefinitions { get; } =
4143
new[]
@@ -77,6 +79,11 @@ public static IEnumerable<object[]> TestCases
7779
$"{DotNetNCHashing} (x3)",
7880
Encoding.ASCII.GetBytes(DotNetNCHashing3),
7981
"CABC8ABD"),
82+
// stripe size
83+
new TestCase(
84+
$"{SixteenBytes} (x3)",
85+
Encoding.ASCII.GetBytes(SixteenBytes3),
86+
"AD98EBD3")
8087
};
8188

8289
protected override NonCryptographicHashAlgorithm CreateInstance() => new XxHash32(Seed);

src/libraries/System.IO.Hashing/tests/XxHash32Tests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public static IEnumerable<object[]> TestCases
3434
private const string DotNetHashesThis3 = DotNetHashesThis + DotNetHashesThis + DotNetHashesThis;
3535
private const string DotNetNCHashing = ".NET now has non-crypto hashing";
3636
private const string DotNetNCHashing3 = DotNetNCHashing + DotNetNCHashing + DotNetNCHashing;
37+
private const string SixteenBytes = ".NET Hashes This";
38+
private const string SixteenBytes3 = SixteenBytes + SixteenBytes + SixteenBytes;
3739

3840
protected static IEnumerable<TestCase> TestCaseDefinitions { get; } =
3941
new[]
@@ -90,6 +92,11 @@ public static IEnumerable<object[]> TestCases
9092
$"{DotNetNCHashing} (x3)",
9193
Encoding.ASCII.GetBytes(DotNetNCHashing3),
9294
"65242024"),
95+
// stripe size
96+
new TestCase(
97+
$"{SixteenBytes} (x3)",
98+
Encoding.ASCII.GetBytes(SixteenBytes3),
99+
"29DA7472")
93100
};
94101

95102
protected override NonCryptographicHashAlgorithm CreateInstance() => new XxHash32();

src/libraries/System.IO.Hashing/tests/XxHash32Tests.f00d.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public static IEnumerable<object[]> TestCases
3636
private const string DotNetHashesThis3 = DotNetHashesThis + DotNetHashesThis + DotNetHashesThis;
3737
private const string DotNetNCHashing = ".NET now has non-crypto hashing";
3838
private const string DotNetNCHashing3 = DotNetNCHashing + DotNetNCHashing + DotNetNCHashing;
39+
private const string SixteenBytes = ".NET Hashes This";
40+
private const string SixteenBytes3 = SixteenBytes + SixteenBytes + SixteenBytes;
3941

4042
protected static IEnumerable<TestCase> TestCaseDefinitions { get; } =
4143
new[]
@@ -77,6 +79,11 @@ public static IEnumerable<object[]> TestCases
7779
$"{DotNetNCHashing} (x3)",
7880
Encoding.ASCII.GetBytes(DotNetNCHashing3),
7981
"5A513E6D"),
82+
// stripe size
83+
new TestCase(
84+
$"{SixteenBytes} (x3)",
85+
Encoding.ASCII.GetBytes(SixteenBytes3),
86+
"B38A9A45")
8087
};
8188

8289
protected override NonCryptographicHashAlgorithm CreateInstance() => new XxHash32(Seed);

src/libraries/System.IO.Hashing/tests/XxHash64Tests.007.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public static IEnumerable<object[]> TestCases
3838
private const string DotNetNCHashing = ".NET now has non-crypto hashing";
3939
private const string SixtyThreeBytes = "A sixty-three byte test input requires substantial forethought!";
4040
private const string SixtyThreeBytes3 = SixtyThreeBytes + SixtyThreeBytes + SixtyThreeBytes;
41+
private const string ThirtyTwoBytes = "This string has 32 ASCII bytes..";
42+
private const string ThirtyTwoBytes3 = ThirtyTwoBytes + ThirtyTwoBytes + ThirtyTwoBytes;
4143

4244
protected static IEnumerable<TestCase> TestCaseDefinitions { get; } =
4345
new[]
@@ -87,6 +89,11 @@ public static IEnumerable<object[]> TestCases
8789
$"{SixtyThreeBytes} (x3)",
8890
Encoding.ASCII.GetBytes(SixtyThreeBytes3),
8991
"D6095B93EB10BEDA"),
92+
// stripe size
93+
new TestCase(
94+
$"{ThirtyTwoBytes} (x3)",
95+
Encoding.ASCII.GetBytes(ThirtyTwoBytes3),
96+
"45116421CF932B1F")
9097
};
9198

9299
protected override NonCryptographicHashAlgorithm CreateInstance() => new XxHash64(Seed);

src/libraries/System.IO.Hashing/tests/XxHash64Tests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public static IEnumerable<object[]> TestCases
3636
private const string DotNetNCHashing = ".NET now has non-crypto hashing";
3737
private const string SixtyThreeBytes = "A sixty-three byte test input requires substantial forethought!";
3838
private const string SixtyThreeBytes3 = SixtyThreeBytes + SixtyThreeBytes + SixtyThreeBytes;
39+
private const string ThirtyTwoBytes = "This string has 32 ASCII bytes..";
40+
private const string ThirtyTwoBytes3 = ThirtyTwoBytes + ThirtyTwoBytes + ThirtyTwoBytes;
3941

4042
protected static IEnumerable<TestCase> TestCaseDefinitions { get; } =
4143
new[]
@@ -103,6 +105,11 @@ public static IEnumerable<object[]> TestCases
103105
$"{SixtyThreeBytes} (x3)",
104106
Encoding.ASCII.GetBytes(SixtyThreeBytes3),
105107
"239C7B3A85BD22B3"),
108+
// stripe size
109+
new TestCase(
110+
$"{ThirtyTwoBytes} (x3)",
111+
Encoding.ASCII.GetBytes(ThirtyTwoBytes3),
112+
"975E3E6FE7E67FBC")
106113
};
107114

108115
protected override NonCryptographicHashAlgorithm CreateInstance() => new XxHash64();

src/libraries/System.IO.Hashing/tests/XxHash64Tests.f00d.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public static IEnumerable<object[]> TestCases
3838
private const string DotNetNCHashing = ".NET now has non-crypto hashing";
3939
private const string SixtyThreeBytes = "A sixty-three byte test input requires substantial forethought!";
4040
private const string SixtyThreeBytes3 = SixtyThreeBytes + SixtyThreeBytes + SixtyThreeBytes;
41+
private const string ThirtyTwoBytes = "This string has 32 ASCII bytes..";
42+
private const string ThirtyTwoBytes3 = ThirtyTwoBytes + ThirtyTwoBytes + ThirtyTwoBytes;
4143

4244
protected static IEnumerable<TestCase> TestCaseDefinitions { get; } =
4345
new[]
@@ -87,6 +89,11 @@ public static IEnumerable<object[]> TestCases
8789
$"{SixtyThreeBytes} (x3)",
8890
Encoding.ASCII.GetBytes(SixtyThreeBytes3),
8991
"6F1C62EB48EA2FEC"),
92+
// stripe size
93+
new TestCase(
94+
$"{ThirtyTwoBytes} (x3)",
95+
Encoding.ASCII.GetBytes(ThirtyTwoBytes3),
96+
"B358EB96B8E3E7AD")
9097
};
9198

9299
protected override NonCryptographicHashAlgorithm CreateInstance() => new XxHash64(Seed);

0 commit comments

Comments
 (0)