Skip to content

Commit 6233b98

Browse files
committed
use extension members for Convert.To/FromHexString
1 parent 1d037d0 commit 6233b98

File tree

5 files changed

+38
-42
lines changed

5 files changed

+38
-42
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#if !NET
2+
using System.Text;
3+
#endif
4+
5+
namespace System
6+
{
7+
internal static class ConvertExtensions
8+
{
9+
extension(Convert)
10+
{
11+
#if !NET
12+
public static byte[] FromHexString(string s)
13+
{
14+
return Org.BouncyCastle.Utilities.Encoders.Hex.Decode(s);
15+
}
16+
17+
public static string ToHexString(byte[] inArray)
18+
{
19+
ArgumentNullException.ThrowIfNull(inArray);
20+
21+
var builder = new StringBuilder(inArray.Length * 2);
22+
23+
foreach (var b in inArray)
24+
{
25+
builder.Append(b.ToString("X2"));
26+
}
27+
28+
return builder.ToString();
29+
}
30+
#endif
31+
}
32+
}
33+
}

src/Renci.SshNet/PrivateKeyFile.PKCS1.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,8 @@ public Key Parse()
4242
{
4343
throw new SshPassPhraseNullOrEmptyException("Private key is encrypted but passphrase is empty.");
4444
}
45-
#if NET
45+
4646
var binarySalt = Convert.FromHexString(_salt);
47-
#else
48-
var binarySalt = Org.BouncyCastle.Utilities.Encoders.Hex.Decode(_salt);
49-
#endif
5047
CipherInfo cipher;
5148
switch (_cipherName)
5249
{

src/Renci.SshNet/PrivateKeyFile.PuTTY.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ public Key Parse()
8282
Convert.ToInt32(_argon2Iterations),
8383
Convert.ToInt32(_argon2Memory),
8484
Convert.ToInt32(_argon2Parallelism),
85-
#if NET
8685
Convert.FromHexString(_argon2Salt),
87-
#else
88-
Org.BouncyCastle.Utilities.Encoders.Hex.Decode(_argon2Salt),
89-
#endif
9086
_passPhrase);
9187

9288
cipherKey = keyData.Take(32);
@@ -152,11 +148,9 @@ public Key Parse()
152148
{
153149
macValue = hmac.ComputeHash(macData);
154150
}
155-
#if NET
151+
156152
var reference = Convert.FromHexString(_mac);
157-
#else
158-
var reference = Org.BouncyCastle.Utilities.Encoders.Hex.Decode(_mac);
159-
#endif
153+
160154
if (!macValue.SequenceEqual(reference))
161155
{
162156
throw new SshException("MAC verification failed for PuTTY key file");

src/Renci.SshNet/Session.cs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
using System.Linq;
66
using System.Net.Sockets;
77
using System.Security.Cryptography;
8-
#if !NET
9-
using System.Text;
10-
#endif
118
using System.Threading;
129
using System.Threading.Tasks;
1310

@@ -310,7 +307,7 @@ public byte[] SessionId
310307
private set
311308
{
312309
_sessionId = value;
313-
SessionIdHex = ToHex(value);
310+
SessionIdHex = value == null ? null : Convert.ToHexString(value);
314311
}
315312
}
316313

@@ -1588,7 +1585,7 @@ internal void OnNewKeysReceived(NewKeysMessage message)
15881585
{
15891586
System.IO.File.AppendAllText(
15901587
path,
1591-
$"{ToHex(ClientInitMessage.Cookie)} SHARED_SECRET {ToHex(kex.SharedKey)}{Environment.NewLine}");
1588+
$"{Convert.ToHexString(ClientInitMessage.Cookie)} SHARED_SECRET {Convert.ToHexString(kex.SharedKey)}{Environment.NewLine}");
15921589
}
15931590
#endif
15941591

@@ -1857,27 +1854,6 @@ private Message LoadMessage(byte[] data, int offset, int count)
18571854
return message;
18581855
}
18591856

1860-
private static string ToHex(byte[] bytes)
1861-
{
1862-
if (bytes is null)
1863-
{
1864-
return null;
1865-
}
1866-
1867-
#if NET
1868-
return Convert.ToHexString(bytes);
1869-
#else
1870-
var builder = new StringBuilder(bytes.Length * 2);
1871-
1872-
foreach (var b in bytes)
1873-
{
1874-
builder.Append(b.ToString("X2"));
1875-
}
1876-
1877-
return builder.ToString();
1878-
#endif
1879-
}
1880-
18811857
/// <summary>
18821858
/// Gets a value indicating whether the socket is connected.
18831859
/// </summary>

test/Renci.SshNet.Tests/Classes/Security/Cryptography/RsaKeyTest.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ private static RsaKey GetRsaKey(string fileName, string passPhrase = null)
2424
// This is just to line up any differences in the assertion message.
2525
private static void AssertEqual(byte[] actualBytes, string expectedHex)
2626
{
27-
#if NET
2827
string actualHex = Convert.ToHexString(actualBytes);
29-
#else
30-
string actualHex = BitConverter.ToString(actualBytes).Replace("-", "");
31-
#endif
3228

3329
Assert.AreEqual(expectedHex, actualHex,
3430
$"{Environment.NewLine}Expected: {expectedHex}{Environment.NewLine} Actual: {actualHex}");

0 commit comments

Comments
 (0)