< Summary

Information
Class: Renci.SshNet.Common.HostKeyEventArgs
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Common\HostKeyEventArgs.cs
Line coverage
95%
Covered lines: 43
Uncovered lines: 2
Coverable lines: 45
Total lines: 129
Line coverage: 95.5%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_CanTrust()100%1100%
get_HostKey()100%1100%
get_HostKeyName()100%1100%
get_FingerPrint()100%1100%
get_FingerPrintSHA256()100%1100%
get_FingerPrintMD5()100%1100%
get_KeyLength()100%1100%
.ctor(...)50%289.47%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Common\HostKeyEventArgs.cs

#LineLine coverage
 1using System;
 2
 3using Renci.SshNet.Abstractions;
 4using Renci.SshNet.Security;
 5
 6namespace Renci.SshNet.Common
 7{
 8    /// <summary>
 9    /// Provides data for the HostKeyReceived event.
 10    /// </summary>
 11    public class HostKeyEventArgs : EventArgs
 12    {
 13        private readonly Lazy<byte[]> _lazyFingerPrint;
 14        private readonly Lazy<string> _lazyFingerPrintSHA256;
 15        private readonly Lazy<string> _lazyFingerPrintMD5;
 16
 17        /// <summary>
 18        /// Gets or sets a value indicating whether host key can be trusted.
 19        /// </summary>
 20        /// <value>
 21        ///   <see langword="true"/> if host key can be trusted; otherwise, <see langword="false"/>.
 22        /// </value>
 243223        public bool CanTrust { get; set; }
 24
 25        /// <summary>
 26        /// Gets the host key.
 27        /// </summary>
 123728        public byte[] HostKey { get; private set; }
 29
 30        /// <summary>
 31        /// Gets the host key name.
 32        /// </summary>
 122833        public string HostKeyName { get; private set; }
 34
 35        /// <summary>
 36        /// Gets the MD5 fingerprint.
 37        /// </summary>
 38        /// <value>
 39        /// MD5 fingerprint as byte array.
 40        /// </value>
 41        public byte[] FingerPrint
 42        {
 43            get
 2944            {
 2945                return _lazyFingerPrint.Value;
 2946            }
 47        }
 48
 49        /// <summary>
 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.
 52        /// </summary>
 53        /// <example><c>ohD8VZEXGWo6Ez8GSEJQ9WpafgLFsOfLOtGGQCQo6Og</c>.</example>
 54        /// <value>
 55        /// Base64 encoded SHA256 fingerprint with padding (equals sign) removed.
 56        /// </value>
 57        public string FingerPrintSHA256
 58        {
 59            get
 460            {
 461                return _lazyFingerPrintSHA256.Value;
 462            }
 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
 473            {
 474                return _lazyFingerPrintMD5.Value;
 475            }
 76        }
 77
 78        /// <summary>
 79        /// Gets the length of the key in bits.
 80        /// </summary>
 81        /// <value>
 82        /// The length of the key in bits.
 83        /// </value>
 122884        public int KeyLength { get; private set; }
 85
 86        /// <summary>
 87        /// Initializes a new instance of the <see cref="HostKeyEventArgs"/> class.
 88        /// </summary>
 89        /// <param name="host">The host.</param>
 90        /// <exception cref="ArgumentNullException"><paramref name="host"/> is <see langword="null"/>.</exception>
 122091        public HostKeyEventArgs(KeyHostAlgorithm host)
 122092        {
 122093            if (host is null)
 094            {
 095                throw new ArgumentNullException(nameof(host));
 96            }
 97
 122098            CanTrust = true;
 122099            HostKey = host.Data;
 1220100            HostKeyName = host.Name;
 1220101            KeyLength = host.Key.KeyLength;
 102
 1220103            _lazyFingerPrint = new Lazy<byte[]>(() =>
 10104                {
 10105                    using var md5 = CryptoAbstraction.CreateMD5();
 10106                    return md5.ComputeHash(HostKey);
 1230107                });
 108
 1220109            _lazyFingerPrintSHA256 = new Lazy<string>(() =>
 4110                {
 4111                    using var sha256 = CryptoAbstraction.CreateSHA256();
 1220112
 4113                    return Convert.ToBase64String(sha256.ComputeHash(HostKey))
 4114#if NET || NETSTANDARD2_1_OR_GREATER
 4115                                  .Replace("=", string.Empty, StringComparison.Ordinal);
 1214116#else
 1214117                                  .Replace("=", string.Empty);
 1220118#endif // NET || NETSTANDARD2_1_OR_GREATER
 1224119                });
 120
 1220121            _lazyFingerPrintMD5 = new Lazy<string>(() =>
 4122                {
 1220123#pragma warning disable CA1308 // Normalize strings to uppercase
 4124                    return BitConverter.ToString(FingerPrint).Replace('-', ':').ToLowerInvariant();
 1220125#pragma warning restore CA1308 // Normalize strings to uppercase
 1224126                });
 1220127        }
 128    }
 129}