Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 1 addition & 6 deletions src/Renci.SshNet/Common/SshDataStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,7 @@ public void WriteBinary(byte[] buffer, int offset, int count)
public BigInteger ReadBigInt()
{
var data = ReadBinary();

#if NETSTANDARD2_1 || NET
return new BigInteger(data, isBigEndian: true);
#else
return new BigInteger(data.Reverse());
#endif
return data.ToBigInteger();
Comment thread
mus65 marked this conversation as resolved.
Outdated
}

/// <summary>
Expand Down
12 changes: 4 additions & 8 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 @@ -147,11 +147,7 @@ public override BigInteger[] Public
Buffer.BlockCopy(qy, 0, q, qx.Length + 1, qy.Length);

// returns Curve-Name and x/y as ECPoint
#if NETSTANDARD2_1 || NET
return new[] { curve, new BigInteger(q, isBigEndian: true) };
#else
return new[] { curve, new BigInteger(q.Reverse()) };
#endif
return new[] { curve, q.ToBigInteger() };
Comment thread
mus65 marked this conversation as resolved.
Outdated
}
}

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