11using System ;
2+
23using Renci . SshNet . Abstractions ;
34using Renci . SshNet . Security ;
45
@@ -9,6 +10,10 @@ namespace Renci.SshNet.Common
910 /// </summary>
1011 public class HostKeyEventArgs : EventArgs
1112 {
13+ private readonly Lazy < byte [ ] > _lazyFingerPrint ;
14+ private readonly Lazy < string > _lazyFingerPrintSHA256 ;
15+ private readonly Lazy < string > _lazyFingerPrintMD5 ;
16+
1217 /// <summary>
1318 /// Gets or sets a value indicating whether host key can be trusted.
1419 /// </summary>
@@ -33,15 +38,42 @@ public class HostKeyEventArgs : EventArgs
3338 /// <value>
3439 /// MD5 fingerprint as byte array.
3540 /// </value>
36- public byte [ ] FingerPrint { get ; private set ; }
41+ public byte [ ] FingerPrint
42+ {
43+ get
44+ {
45+ return _lazyFingerPrint . Value ;
46+ }
47+ }
3748
3849 /// <summary>
39- /// Gets the SHA256 fingerprint.
50+ /// Gets the SHA256 fingerprint of the host key in the same format as the ssh command,
51+ /// i.e. non-padded base64, but without the <c>SHA256:</c> prefix.
4052 /// </summary>
53+ /// <example><c>ohD8VZEXGWo6Ez8GSEJQ9WpafgLFsOfLOtGGQCQo6Og</c></example>
4154 /// <value>
4255 /// Base64 encoded SHA256 fingerprint with padding (equals sign) removed.
4356 /// </value>
44- public string FingerPrintSHA256 { get ; private set ; }
57+ public string FingerPrintSHA256
58+ {
59+ get
60+ {
61+ return _lazyFingerPrintSHA256 . Value ;
62+ }
63+ }
64+
65+ /// <summary>
66+ /// Gets the MD5 fingerprint of the host key in the same format as the ssh command,
67+ /// i.e. hexadecimal bytes separated by colons, but without the <c>MD5:</c> prefix.
68+ /// </summary>
69+ /// <example><c>97:70:33:82:fd:29:3a:73:39:af:6a:07:ad:f8:80:49</c></example>
70+ public string FingerPrintMD5
71+ {
72+ get
73+ {
74+ return _lazyFingerPrintMD5 . Value ;
75+ }
76+ }
4577
4678 /// <summary>
4779 /// Gets the length of the key in bits.
@@ -61,16 +93,21 @@ public HostKeyEventArgs(KeyHostAlgorithm host)
6193 HostKey = host . Data ;
6294 HostKeyName = host . Name ;
6395 KeyLength = host . Key . KeyLength ;
64-
65- using ( var md5 = CryptoAbstraction . CreateMD5 ( ) )
96+
97+ _lazyFingerPrint = new Lazy < byte [ ] > ( ( ) =>
6698 {
67- FingerPrint = md5 . ComputeHash ( host . Data ) ;
68- }
99+ using var md5 = CryptoAbstraction . CreateMD5 ( ) ;
100+ return md5 . ComputeHash ( HostKey ) ;
101+ } ) ;
69102
70- using ( var sha256 = CryptoAbstraction . CreateSHA256 ( ) )
103+ _lazyFingerPrintSHA256 = new Lazy < string > ( ( ) =>
71104 {
72- FingerPrintSHA256 = Convert . ToBase64String ( sha256 . ComputeHash ( host . Data ) ) . Replace ( "=" , "" ) ;
73- }
105+ using var sha256 = CryptoAbstraction . CreateSHA256 ( ) ;
106+ return Convert . ToBase64String ( sha256 . ComputeHash ( HostKey ) ) . Replace ( "=" , "" ) ;
107+ } ) ;
108+
109+ _lazyFingerPrintMD5 = new Lazy < string > ( ( ) =>
110+ BitConverter . ToString ( FingerPrint ) . Replace ( "-" , ":" ) . ToLowerInvariant ( ) ) ;
74111 }
75112 }
76113}
0 commit comments