Skip to content

remove Reverse extension to avoid source-breaking change with .NET 10 #1606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/Renci.SshNet/Common/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,26 @@ internal static ServiceName ToServiceName(this byte[] data)
}
}

internal static BigInteger ToBigInteger(this ReadOnlySpan<byte> data)
{
#if NETSTANDARD2_1 || NET
return new BigInteger(data, isBigEndian: true);
#else
var reversed = data.ToArray();
Array.Reverse(reversed);
return new BigInteger(reversed);
#endif
}

internal static BigInteger ToBigInteger(this byte[] data)
{
#if NETSTANDARD2_1 || NET
return new BigInteger(data, isBigEndian: true);
#else
var reversed = new byte[data.Length];
Buffer.BlockCopy(data, 0, reversed, 0, data.Length);
return new BigInteger(reversed.Reverse());
Array.Reverse(reversed);
return new BigInteger(reversed);
#endif
}

Expand All @@ -69,7 +81,8 @@ public static BigInteger ToBigInteger2(this byte[] data)
{
var buf = new byte[data.Length + 1];
Buffer.BlockCopy(data, 0, buf, 1, data.Length);
return new BigInteger(buf.Reverse());
Array.Reverse(buf);
return new BigInteger(buf);
}

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

if (isBigEndian)
{
_ = data.Reverse();
Array.Reverse(data);
}

return data;
Expand Down Expand Up @@ -138,19 +151,6 @@ public static void SetIgnoringObjectDisposed(this EventWaitHandle waitHandle)
}
}

/// <summary>
/// Reverses the sequence of the elements in the entire one-dimensional <see cref="Array"/>.
/// </summary>
/// <param name="array">The one-dimensional <see cref="Array"/> to reverse.</param>
/// <returns>
/// The <see cref="Array"/> with its elements reversed.
/// </returns>
internal static T[] Reverse<T>(this T[] array)
{
Array.Reverse(array);
return array;
}

/// <summary>
/// Prints out the specified bytes.
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Renci.SshNet/Common/SshDataStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ public BigInteger ReadBigInt()
#if NETSTANDARD2_1 || NET
return new BigInteger(data, isBigEndian: true);
#else
return new BigInteger(data.Reverse());
Array.Reverse(data);
return new BigInteger(data);
#endif
}

Expand Down
9 changes: 5 additions & 4 deletions src/Renci.SshNet/Security/Cryptography/EcdsaKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public partial class EcdsaKey : Key, IDisposable
private const string ECDSA_P521_OID_VALUE = "1.3.132.0.35"; // Also called nistP521or secP521r1
#pragma warning restore SA1310 // Field names should not contain underscore

private static readonly BigInteger Encoded256 = new BigInteger("nistp256"u8.ToArray().Reverse());
private static readonly BigInteger Encoded384 = new BigInteger("nistp384"u8.ToArray().Reverse());
private static readonly BigInteger Encoded521 = new BigInteger("nistp521"u8.ToArray().Reverse());
private static readonly BigInteger Encoded256 = "nistp256"u8.ToBigInteger();
private static readonly BigInteger Encoded384 = "nistp384"u8.ToBigInteger();
private static readonly BigInteger Encoded521 = "nistp521"u8.ToBigInteger();

private EcdsaDigitalSignature? _digitalSignature;

Expand Down Expand Up @@ -150,7 +150,8 @@ public override BigInteger[] Public
#if NETSTANDARD2_1 || NET
return new[] { curve, new BigInteger(q, isBigEndian: true) };
#else
return new[] { curve, new BigInteger(q.Reverse()) };
Array.Reverse(q);
return new[] { curve, new BigInteger(q) };
#endif
}
}
Expand Down
27 changes: 0 additions & 27 deletions test/Renci.SshNet.Benchmarks/Common/ExtensionsBenchmarks.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void ShouldNotAppendZero()
{
byte[] value = { 0x0a, 0x0d };

var actual = value.ToBigInteger2().ToByteArray().Reverse();
var actual = value.ToBigInteger2().ToByteArray(isBigEndian: true);

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

var actual = value.ToBigInteger2().ToByteArray().Reverse();
var actual = value.ToBigInteger2().ToByteArray(isBigEndian: true);

Assert.IsNotNull(actual);
Assert.AreEqual(4, actual.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected override void OnInit()
[TestMethod]
public void GroupPrimeShouldBeSecondOakleyGroup()
{
var bytes = _group14.GroupPrime.ToByteArray().Reverse();
var bytes = _group14.GroupPrime.ToByteArray(isBigEndian: true);
Assert.IsTrue(SecondOkleyGroup.IsEqualTo(bytes));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected override void OnInit()
[TestMethod]
public void GroupPrimeShouldBeSecondOakleyGroup()
{
var bytes = _group14.GroupPrime.ToByteArray().Reverse();
var bytes = _group14.GroupPrime.ToByteArray(isBigEndian: true);
Assert.IsTrue(SecondOkleyGroup.IsEqualTo(bytes));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected override void OnInit()
[TestMethod]
public void GroupPrimeShouldBeMoreModularExponentialGroup16()
{
var bytes = _group16.GroupPrime.ToByteArray().Reverse();
var bytes = _group16.GroupPrime.ToByteArray(isBigEndian: true);
Assert.IsTrue(MoreModularExponentialGroup16.IsEqualTo(bytes));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Renci.SshNet.Common;
using Renci.SshNet.Security;
Expand Down Expand Up @@ -37,7 +39,7 @@ protected override void OnInit()
[TestMethod]
public void GroupPrimeShouldBeSecondOakleyGroup()
{
var bytes = _group1.GroupPrime.ToByteArray().Reverse();
var bytes = _group1.GroupPrime.ToByteArray(isBigEndian: true);
Assert.IsTrue(SecondOkleyGroup.IsEqualTo(bytes));

SecondOkleyGroup.Reverse().DebugPrint();
Expand Down