44using System . Globalization ;
55using System . Net ;
66using System . Net . Sockets ;
7+ using System . Numerics ;
78using System . Text ;
89
910using Renci . SshNet . Abstractions ;
@@ -14,7 +15,7 @@ namespace Renci.SshNet.Common
1415 /// <summary>
1516 /// Collection of different extension methods.
1617 /// </summary>
17- internal static partial class Extensions
18+ internal static class Extensions
1819 {
1920 internal static byte [ ] ToArray ( this ServiceName serviceName )
2021 {
@@ -45,26 +46,35 @@ internal static ServiceName ToServiceName(this byte[] data)
4546
4647 internal static BigInteger ToBigInteger ( this byte [ ] data )
4748 {
49+ #if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
50+ return new BigInteger ( data , isBigEndian : true ) ;
51+ #else
4852 var reversed = new byte [ data . Length ] ;
4953 Buffer . BlockCopy ( data , 0 , reversed , 0 , data . Length ) ;
5054 return new BigInteger ( reversed . Reverse ( ) ) ;
55+ #endif
5156 }
5257
5358 /// <summary>
5459 /// Initializes a new instance of the <see cref="BigInteger"/> structure using the SSH BigNum2 Format.
5560 /// </summary>
5661 public static BigInteger ToBigInteger2 ( this byte [ ] data )
5762 {
63+ #if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
64+ return new BigInteger ( data , isBigEndian : true , isUnsigned : true ) ;
65+ #else
5866 if ( ( data [ 0 ] & ( 1 << 7 ) ) != 0 )
5967 {
6068 var buf = new byte [ data . Length + 1 ] ;
6169 Buffer . BlockCopy ( data , 0 , buf , 1 , data . Length ) ;
62- data = buf ;
70+ return new BigInteger ( buf . Reverse ( ) ) ;
6371 }
6472
6573 return data . ToBigInteger ( ) ;
74+ #endif
6675 }
6776
77+ #if NETFRAMEWORK || NETSTANDARD2_0
6878 public static byte [ ] ToByteArray ( this BigInteger bigInt , bool isUnsigned = false , bool isBigEndian = false )
6979 {
7080 var data = bigInt . ToByteArray ( ) ;
@@ -81,6 +91,15 @@ public static byte[] ToByteArray(this BigInteger bigInt, bool isUnsigned = false
8191
8292 return data ;
8393 }
94+ #endif
95+
96+ #if ! NET6_0_OR_GREATER
97+ public static long GetBitLength ( this BigInteger bigint )
98+ {
99+ // Taken from https://github.com/dotnet/runtime/issues/31308
100+ return ( long ) Math . Ceiling ( BigInteger . Log ( bigint . Sign < 0 ? - bigint : bigint + 1 , 2 ) ) ;
101+ }
102+ #endif
84103
85104 // See https://github.com/dotnet/runtime/blob/9b57a265c7efd3732b035bade005561a04767128/src/libraries/Common/src/System/Security/Cryptography/KeyBlobHelpers.cs#L51
86105 public static byte [ ] ExportKeyParameter ( this BigInteger value , int length )
0 commit comments