Skip to content

Commit 7a836ae

Browse files
committed
use extension members for Convert.To/FromHexString
1 parent 7bc2828 commit 7a836ae

File tree

5 files changed

+39
-42
lines changed

5 files changed

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

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

@@ -1592,7 +1589,7 @@ internal void OnNewKeysReceived(NewKeysMessage message)
15921589
{
15931590
System.IO.File.AppendAllText(
15941591
path,
1595-
$"{ToHex(ClientInitMessage.Cookie)} SHARED_SECRET {ToHex(kex.SharedKey)}{Environment.NewLine}");
1592+
$"{Convert.ToHexString(ClientInitMessage.Cookie)} SHARED_SECRET {Convert.ToHexString(kex.SharedKey)}{Environment.NewLine}");
15961593
}
15971594
#endif
15981595

@@ -1861,27 +1858,6 @@ private Message LoadMessage(byte[] data, int offset, int count)
18611858
return message;
18621859
}
18631860

1864-
private static string ToHex(byte[] bytes)
1865-
{
1866-
if (bytes is null)
1867-
{
1868-
return null;
1869-
}
1870-
1871-
#if NET
1872-
return Convert.ToHexString(bytes);
1873-
#else
1874-
var builder = new StringBuilder(bytes.Length * 2);
1875-
1876-
foreach (var b in bytes)
1877-
{
1878-
builder.Append(b.ToString("X2"));
1879-
}
1880-
1881-
return builder.ToString();
1882-
#endif
1883-
}
1884-
18851861
/// <summary>
18861862
/// Gets a value indicating whether the socket is connected.
18871863
/// </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)