KeyUtils.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using FxSsh.Algorithms;
  2. using System;
  3. using System.Diagnostics.Contracts;
  4. using System.Security.Cryptography;
  5. namespace FxSsh
  6. {
  7. public static class KeyUtils
  8. {
  9. public static string GetFingerprint(string sshkey)
  10. {
  11. Contract.Requires(sshkey != null);
  12. using (var md5 = new MD5CryptoServiceProvider())
  13. {
  14. var bytes = Convert.FromBase64String(sshkey);
  15. bytes = md5.ComputeHash(bytes);
  16. return BitConverter.ToString(bytes).Replace('-', ':');
  17. }
  18. }
  19. private static PublicKeyAlgorithm GetKeyAlgorithm(string type)
  20. {
  21. Contract.Requires(type != null);
  22. switch (type)
  23. {
  24. case "ssh-rsa":
  25. return new RsaKey();
  26. case "ssh-dss":
  27. return new DssKey();
  28. default:
  29. throw new ArgumentOutOfRangeException("type");
  30. }
  31. }
  32. public static string GeneratePrivateKey(string type)
  33. {
  34. Contract.Requires(type != null);
  35. var alg = GetKeyAlgorithm(type);
  36. var bytes = alg.ExportKey();
  37. return Convert.ToBase64String(bytes);
  38. }
  39. public static string[] SupportedAlgorithms
  40. {
  41. get { return new string[] { "ssh-rsa", "ssh-dss" }; }
  42. }
  43. }
  44. }