Skip to content

Commit d1213cf

Browse files
authored
remove Reverse extension to avoid source-breaking change with .NET 10 (#1606)
* remove Reverse extension to avoid source-breaking change with .NET 10 * revert to ifdef in some places to avoid extra array copy on lower targets
1 parent 86dc811 commit d1213cf

10 files changed

+32
-120
lines changed

src/Renci.SshNet/Common/Extensions.cs

+16-16
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,26 @@ internal static ServiceName ToServiceName(this byte[] data)
4646
}
4747
}
4848

49+
internal static BigInteger ToBigInteger(this ReadOnlySpan<byte> data)
50+
{
51+
#if NETSTANDARD2_1 || NET
52+
return new BigInteger(data, isBigEndian: true);
53+
#else
54+
var reversed = data.ToArray();
55+
Array.Reverse(reversed);
56+
return new BigInteger(reversed);
57+
#endif
58+
}
59+
4960
internal static BigInteger ToBigInteger(this byte[] data)
5061
{
5162
#if NETSTANDARD2_1 || NET
5263
return new BigInteger(data, isBigEndian: true);
5364
#else
5465
var reversed = new byte[data.Length];
5566
Buffer.BlockCopy(data, 0, reversed, 0, data.Length);
56-
return new BigInteger(reversed.Reverse());
67+
Array.Reverse(reversed);
68+
return new BigInteger(reversed);
5769
#endif
5870
}
5971

@@ -69,7 +81,8 @@ public static BigInteger ToBigInteger2(this byte[] data)
6981
{
7082
var buf = new byte[data.Length + 1];
7183
Buffer.BlockCopy(data, 0, buf, 1, data.Length);
72-
return new BigInteger(buf.Reverse());
84+
Array.Reverse(buf);
85+
return new BigInteger(buf);
7386
}
7487

7588
return data.ToBigInteger();
@@ -88,7 +101,7 @@ public static byte[] ToByteArray(this BigInteger bigInt, bool isUnsigned = false
88101

89102
if (isBigEndian)
90103
{
91-
_ = data.Reverse();
104+
Array.Reverse(data);
92105
}
93106

94107
return data;
@@ -138,19 +151,6 @@ public static void SetIgnoringObjectDisposed(this EventWaitHandle waitHandle)
138151
}
139152
}
140153

141-
/// <summary>
142-
/// Reverses the sequence of the elements in the entire one-dimensional <see cref="Array"/>.
143-
/// </summary>
144-
/// <param name="array">The one-dimensional <see cref="Array"/> to reverse.</param>
145-
/// <returns>
146-
/// The <see cref="Array"/> with its elements reversed.
147-
/// </returns>
148-
internal static T[] Reverse<T>(this T[] array)
149-
{
150-
Array.Reverse(array);
151-
return array;
152-
}
153-
154154
/// <summary>
155155
/// Prints out the specified bytes.
156156
/// </summary>

src/Renci.SshNet/Common/SshDataStream.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ public BigInteger ReadBigInt()
210210
#if NETSTANDARD2_1 || NET
211211
return new BigInteger(data, isBigEndian: true);
212212
#else
213-
return new BigInteger(data.Reverse());
213+
Array.Reverse(data);
214+
return new BigInteger(data);
214215
#endif
215216
}
216217

src/Renci.SshNet/Security/Cryptography/EcdsaKey.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public partial class EcdsaKey : Key, IDisposable
2222
private const string ECDSA_P521_OID_VALUE = "1.3.132.0.35"; // Also called nistP521or secP521r1
2323
#pragma warning restore SA1310 // Field names should not contain underscore
2424

25-
private static readonly BigInteger Encoded256 = new BigInteger("nistp256"u8.ToArray().Reverse());
26-
private static readonly BigInteger Encoded384 = new BigInteger("nistp384"u8.ToArray().Reverse());
27-
private static readonly BigInteger Encoded521 = new BigInteger("nistp521"u8.ToArray().Reverse());
25+
private static readonly BigInteger Encoded256 = "nistp256"u8.ToBigInteger();
26+
private static readonly BigInteger Encoded384 = "nistp384"u8.ToBigInteger();
27+
private static readonly BigInteger Encoded521 = "nistp521"u8.ToBigInteger();
2828

2929
private EcdsaDigitalSignature? _digitalSignature;
3030

@@ -150,7 +150,8 @@ public override BigInteger[] Public
150150
#if NETSTANDARD2_1 || NET
151151
return new[] { curve, new BigInteger(q, isBigEndian: true) };
152152
#else
153-
return new[] { curve, new BigInteger(q.Reverse()) };
153+
Array.Reverse(q);
154+
return new[] { curve, new BigInteger(q) };
154155
#endif
155156
}
156157
}

test/Renci.SshNet.Benchmarks/Common/ExtensionsBenchmarks.cs

-27
This file was deleted.

test/Renci.SshNet.Tests/Classes/Common/ExtensionsTest_Reverse.cs

-65
This file was deleted.

test/Renci.SshNet.Tests/Classes/Common/ExtensionsTest_ToBigInteger2.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public void ShouldNotAppendZero()
1212
{
1313
byte[] value = { 0x0a, 0x0d };
1414

15-
var actual = value.ToBigInteger2().ToByteArray().Reverse();
15+
var actual = value.ToBigInteger2().ToByteArray(isBigEndian: true);
1616

1717
Assert.IsNotNull(actual);
1818
Assert.AreEqual(2, actual.Length);
@@ -25,7 +25,7 @@ public void ShouldAppendZero()
2525
{
2626
byte[] value = { 0xff, 0x0a, 0x0d };
2727

28-
var actual = value.ToBigInteger2().ToByteArray().Reverse();
28+
var actual = value.ToBigInteger2().ToByteArray(isBigEndian: true);
2929

3030
Assert.IsNotNull(actual);
3131
Assert.AreEqual(4, actual.Length);

test/Renci.SshNet.Tests/Classes/Security/KeyExchangeDiffieHellmanGroup14Sha1Test.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected override void OnInit()
4848
[TestMethod]
4949
public void GroupPrimeShouldBeSecondOakleyGroup()
5050
{
51-
var bytes = _group14.GroupPrime.ToByteArray().Reverse();
51+
var bytes = _group14.GroupPrime.ToByteArray(isBigEndian: true);
5252
Assert.IsTrue(SecondOkleyGroup.IsEqualTo(bytes));
5353
}
5454

test/Renci.SshNet.Tests/Classes/Security/KeyExchangeDiffieHellmanGroup14Sha256Test.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected override void OnInit()
4848
[TestMethod]
4949
public void GroupPrimeShouldBeSecondOakleyGroup()
5050
{
51-
var bytes = _group14.GroupPrime.ToByteArray().Reverse();
51+
var bytes = _group14.GroupPrime.ToByteArray(isBigEndian: true);
5252
Assert.IsTrue(SecondOkleyGroup.IsEqualTo(bytes));
5353
}
5454

test/Renci.SshNet.Tests/Classes/Security/KeyExchangeDiffieHellmanGroup16Sha512Test.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected override void OnInit()
5858
[TestMethod]
5959
public void GroupPrimeShouldBeMoreModularExponentialGroup16()
6060
{
61-
var bytes = _group16.GroupPrime.ToByteArray().Reverse();
61+
var bytes = _group16.GroupPrime.ToByteArray(isBigEndian: true);
6262
Assert.IsTrue(MoreModularExponentialGroup16.IsEqualTo(bytes));
6363
}
6464

test/Renci.SshNet.Tests/Classes/Security/KeyExchangeDiffieHellmanGroup1Sha1Test.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Microsoft.VisualStudio.TestTools.UnitTesting;
1+
using System.Linq;
2+
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
24

35
using Renci.SshNet.Common;
46
using Renci.SshNet.Security;
@@ -37,7 +39,7 @@ protected override void OnInit()
3739
[TestMethod]
3840
public void GroupPrimeShouldBeSecondOakleyGroup()
3941
{
40-
var bytes = _group1.GroupPrime.ToByteArray().Reverse();
42+
var bytes = _group1.GroupPrime.ToByteArray(isBigEndian: true);
4143
Assert.IsTrue(SecondOkleyGroup.IsEqualTo(bytes));
4244

4345
SecondOkleyGroup.Reverse().DebugPrint();

0 commit comments

Comments
 (0)