4
4
using System . Globalization ;
5
5
using System . Net ;
6
6
using System . Net . Sockets ;
7
+ using System . Numerics ;
7
8
using System . Text ;
8
9
9
10
using Renci . SshNet . Abstractions ;
@@ -14,7 +15,7 @@ namespace Renci.SshNet.Common
14
15
/// <summary>
15
16
/// Collection of different extension methods.
16
17
/// </summary>
17
- internal static partial class Extensions
18
+ internal static class Extensions
18
19
{
19
20
internal static byte [ ] ToArray ( this ServiceName serviceName )
20
21
{
@@ -45,26 +46,35 @@ internal static ServiceName ToServiceName(this byte[] data)
45
46
46
47
internal static BigInteger ToBigInteger ( this byte [ ] data )
47
48
{
49
+ #if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
50
+ return new BigInteger ( data , isBigEndian : true ) ;
51
+ #else
48
52
var reversed = new byte [ data . Length ] ;
49
53
Buffer . BlockCopy ( data , 0 , reversed , 0 , data . Length ) ;
50
54
return new BigInteger ( reversed . Reverse ( ) ) ;
55
+ #endif
51
56
}
52
57
53
58
/// <summary>
54
59
/// Initializes a new instance of the <see cref="BigInteger"/> structure using the SSH BigNum2 Format.
55
60
/// </summary>
56
61
public static BigInteger ToBigInteger2 ( this byte [ ] data )
57
62
{
63
+ #if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
64
+ return new BigInteger ( data , isBigEndian : true , isUnsigned : true ) ;
65
+ #else
58
66
if ( ( data [ 0 ] & ( 1 << 7 ) ) != 0 )
59
67
{
60
68
var buf = new byte [ data . Length + 1 ] ;
61
69
Buffer . BlockCopy ( data , 0 , buf , 1 , data . Length ) ;
62
- data = buf ;
70
+ return new BigInteger ( buf . Reverse ( ) ) ;
63
71
}
64
72
65
73
return data . ToBigInteger ( ) ;
74
+ #endif
66
75
}
67
76
77
+ #if NETFRAMEWORK || NETSTANDARD2_0
68
78
public static byte [ ] ToByteArray ( this BigInteger bigInt , bool isUnsigned = false , bool isBigEndian = false )
69
79
{
70
80
var data = bigInt . ToByteArray ( ) ;
@@ -81,6 +91,15 @@ public static byte[] ToByteArray(this BigInteger bigInt, bool isUnsigned = false
81
91
82
92
return data ;
83
93
}
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
84
103
85
104
// See https://github.com/dotnet/runtime/blob/9b57a265c7efd3732b035bade005561a04767128/src/libraries/Common/src/System/Security/Cryptography/KeyBlobHelpers.cs#L51
86
105
public static byte [ ] ExportKeyParameter ( this BigInteger value , int length )
0 commit comments