Skip to content

Commit 70a663f

Browse files
committed
Add SHA256 fingerprint support
1 parent 4bb544b commit 70a663f

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

README.md

+2-19
Original file line numberDiff line numberDiff line change
@@ -149,30 +149,13 @@ using (var client = new SftpClient(connectionInfo))
149149
Establish a SSH connection using user name and password, and reject the connection if the fingerprint of the server does not match the expected fingerprint:
150150

151151
```cs
152-
byte[] expectedFingerPrint = new byte[] {
153-
0x66, 0x31, 0xaf, 0x00, 0x54, 0xb9, 0x87, 0x31,
154-
0xff, 0x58, 0x1c, 0x31, 0xb1, 0xa2, 0x4c, 0x6b
155-
};
152+
string expectedFingerPrint = "LKOy5LvmtEe17S4lyxVXqvs7uPMy+yF79MQpHeCs/Qo";
156153

157154
using (var client = new SshClient("sftp.foo.com", "guest", "pwd"))
158155
{
159156
client.HostKeyReceived += (sender, e) =>
160157
{
161-
if (expectedFingerPrint.Length == e.FingerPrint.Length)
162-
{
163-
for (var i = 0; i < expectedFingerPrint.Length; i++)
164-
{
165-
if (expectedFingerPrint[i] != e.FingerPrint[i])
166-
{
167-
e.CanTrust = false;
168-
break;
169-
}
170-
}
171-
}
172-
else
173-
{
174-
e.CanTrust = false;
175-
}
158+
e.CanTrust = expectedFingerPrint.Equals(e.FingerPrintSHA256);
176159
};
177160
client.Connect();
178161
}

src/Renci.SshNet.Tests/Classes/Common/HostKeyEventArgsTest.cs

+10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ public void HostKeyEventArgsConstructorTest_VerifyMD5()
5858
}.SequenceEqual(target.FingerPrint));
5959
}
6060

61+
/// <summary>
62+
///A test for SHA256 calculation in HostKeyEventArgs Constructor
63+
///</summary>
64+
[TestMethod]
65+
public void HostKeyEventArgsConstructorTest_VerifySHA256()
66+
{
67+
HostKeyEventArgs target = new HostKeyEventArgs(GetKeyHostAlgorithm());
68+
Assert.AreEqual("93LkmoWksp9ytNVZIPXi9KJU1uvlC9clZ/CkUHf6uEE", target.FingerPrintSHA256);
69+
}
70+
6171
/// <summary>
6272
///A test for CanTrust
6373
///</summary>

src/Renci.SshNet/Common/HostKeyEventArgs.cs

+17-1
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,21 @@ public class HostKeyEventArgs : EventArgs
2828
public string HostKeyName{ get; private set; }
2929

3030
/// <summary>
31-
/// Gets the finger print.
31+
/// Gets the MD5 fingerprint.
3232
/// </summary>
33+
/// <value>
34+
/// MD5 fingerprint as byte array.
35+
/// </value>
3336
public byte[] FingerPrint { get; private set; }
3437

38+
/// <summary>
39+
/// Gets the SHA256 fingerprint.
40+
/// </summary>
41+
/// <value>
42+
/// Base64 encoded SHA256 fingerprint with padding (equals sign) removed.
43+
/// </value>
44+
public string FingerPrintSHA256 { get; private set; }
45+
3546
/// <summary>
3647
/// Gets the length of the key in bits.
3748
/// </summary>
@@ -58,6 +69,11 @@ public HostKeyEventArgs(KeyHostAlgorithm host)
5869
{
5970
FingerPrint = md5.ComputeHash(host.Data);
6071
}
72+
73+
using (var sha256 = CryptoAbstraction.CreateSHA256())
74+
{
75+
FingerPrintSHA256 = Convert.ToBase64String(sha256.ComputeHash(host.Data)).Replace("=", "");
76+
}
6177
}
6278
}
6379
}

0 commit comments

Comments
 (0)