PoshSSH/PoshSSH/TrustedKeyMng.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System.Collections.Generic;
using Microsoft.Win32;
using System;

namespace SSH
{
    // Class for managing the keys 
    public class TrustedKeyMng
    {
        public Dictionary<string, string> GetKeys()
        {
            var hostkeys = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
            var poshSoftKey = Registry.CurrentUser.OpenSubKey(@"Software\PoshSSH", true);
            if (poshSoftKey != null)
            {
                var hosts = poshSoftKey.GetValueNames();
                foreach (var host in hosts)
                {
                    var hostkey = poshSoftKey.GetValue(host).ToString();
                    hostkeys.Add(host, hostkey);
                }
            }
            else
            {
                using (var softKey = Registry.CurrentUser.OpenSubKey(@"Software", true))
                {
                    if (softKey != null) softKey.CreateSubKey("PoshSSH");
                }
            }
            return hostkeys;
        }

        public bool SetKey(string host, string fingerprint)
        {
            var poshSoftKey = Registry.CurrentUser.OpenSubKey(@"Software\PoshSSH", true);
            if (poshSoftKey != null)
            {
                poshSoftKey.SetValue(host, fingerprint);
                return true;
            }
            var softKey = Registry.CurrentUser.OpenSubKey(@"Software", true);
            if (softKey == null) return true;
            softKey.CreateSubKey("PoshSSH");
            softKey.SetValue(host, fingerprint);
            return true;
        }
    }
}