예제 #1
0
        // Create new connection profile
        public void AddProfile(string name, string host, string user, string password)
        {
            if (ProfileExists(name))
            {
                throw new KanpachiProfileException($"Profile {name} already exists.");
            }
            KanpachiProfile profile = new KanpachiProfile(name, host, user);

            try{
                if (GetActiveProfile(true).Name == name)
                {
                    System.Console.WriteLine($"Profile {name} is already active.");
                    return;
                }
            } catch (KanpachiProfileException) {
                profile.IsActive = true; // no active profile, so set this new one
            }

            if (password.Length > 0)
            {
                profile.PasswordDecrypted = password;
                profile.Password          = SecUtils.EncryptProfile(profile);
            }
            WriteProfile(profile);
            Console.WriteLine($"Added profile {name}.");
        }
예제 #2
0
 // Write profile to json file
 public void WriteProfile(KanpachiProfile profile)
 {
     if (!Directory.Exists(ProfilesPath))
     {
         Directory.CreateDirectory(ProfilesPath);
     }
     using (StreamWriter f = File.CreateText(GetProfilePath(profile.Name))){
         if (profile.PasswordDecrypted != null && profile.PasswordDecrypted.Length > 0)
         {
             profile.Password = SecUtils.EncryptProfile(profile);
         }
         f.Write(JsonConvert.SerializeObject(profile, Formatting.Indented, new JsonSerializerSettings {
             ContractResolver = new CamelCasePropertyNamesContractResolver()
         }));
     }
 }
예제 #3
0
        // Get active profile from local cache
        public KanpachiProfile GetActiveProfile(bool skipDecryption = false)
        {
            foreach (var filename in Directory.GetFiles(ProfilesPath, "*.json"))
            {
                using (StreamReader f = File.OpenText(filename)){
                    JsonSerializer  serializer = new JsonSerializer();
                    KanpachiProfile profile    = (KanpachiProfile)serializer.Deserialize(f, typeof(KanpachiProfile));

                    if (profile.IsActive)
                    {
                        if (!skipDecryption)
                        {
                            profile.PasswordDecrypted = SecUtils.DecryptProfile(profile);
                        }
                        return(profile);
                    }
                }
            }
            throw new KanpachiProfileException("Could not find an active profile.");
        }
예제 #4
0
        // validate hash matches expected hash
        public bool Validate(string key, string storedHash)
        {
            var compare = Pbkdf2Bytes(Encoding.UTF8.GetBytes(key), SecUtils.GenerateSalt(), DefaultIterations);

            return(SequenceEqual(compare, Convert.FromBase64String(storedHash)));
        }
예제 #5
0
 public string Hash(string key)
 {
     return(Hash(key, SecUtils.GenerateSalt()));
 }