HostKeyEventArgs.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using Renci.SshNet.Abstractions;
  3. using Renci.SshNet.Security;
  4. namespace Renci.SshNet.Common
  5. {
  6. /// <summary>
  7. /// Provides data for the HostKeyReceived event.
  8. /// </summary>
  9. public class HostKeyEventArgs : EventArgs
  10. {
  11. /// <summary>
  12. /// Gets or sets a value indicating whether host key can be trusted.
  13. /// </summary>
  14. /// <value>
  15. /// <c>true</c> if host key can be trusted; otherwise, <c>false</c>.
  16. /// </value>
  17. public bool CanTrust { get; set; }
  18. /// <summary>
  19. /// Gets the host key.
  20. /// </summary>
  21. public byte[] HostKey { get; private set; }
  22. /// <summary>
  23. /// Gets the host key name.
  24. /// </summary>
  25. public string HostKeyName{ get; private set; }
  26. /// <summary>
  27. /// Gets the MD5 fingerprint.
  28. /// </summary>
  29. /// <value>
  30. /// MD5 fingerprint as byte array.
  31. /// </value>
  32. public byte[] FingerPrint { get; private set; }
  33. /// <summary>
  34. /// Gets the SHA256 fingerprint.
  35. /// </summary>
  36. /// <value>
  37. /// Base64 encoded SHA256 fingerprint with padding (equals sign) removed.
  38. /// </value>
  39. public string FingerPrintSHA256 { get; private set; }
  40. /// <summary>
  41. /// Gets the length of the key in bits.
  42. /// </summary>
  43. /// <value>
  44. /// The length of the key in bits.
  45. /// </value>
  46. public int KeyLength { get; private set; }
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="HostKeyEventArgs"/> class.
  49. /// </summary>
  50. /// <param name="host">The host.</param>
  51. public HostKeyEventArgs(KeyHostAlgorithm host)
  52. {
  53. CanTrust = true;
  54. HostKey = host.Data;
  55. HostKeyName = host.Name;
  56. KeyLength = host.Key.KeyLength;
  57. using (var md5 = CryptoAbstraction.CreateMD5())
  58. {
  59. FingerPrint = md5.ComputeHash(host.Data);
  60. }
  61. using (var sha256 = CryptoAbstraction.CreateSHA256())
  62. {
  63. FingerPrintSHA256 = Convert.ToBase64String(sha256.ComputeHash(host.Data)).Replace("=", "");
  64. }
  65. }
  66. }
  67. }