Рефакторинг + DI

This commit is contained in:
Danil Kislov
2019-03-15 17:49:58 +03:00
parent fd02201b42
commit d56094febb
19 changed files with 288 additions and 105 deletions
@@ -0,0 +1,42 @@
using System;
using System.Security.Cryptography;
using System.Text;
namespace JiraTimeBotForm.Configuration
{
public class PasswordEncryptionClass
{
private byte[] GetEntropy(string EntropyString)
{
MD5 md5 = MD5.Create();
return md5.ComputeHash(Encoding.UTF8.GetBytes(EntropyString));
}
public string Encrypt(string username, string password, string address)
{
byte[] entropy = GetEntropy(address + username);
byte[] pass = Encoding.UTF8.GetBytes(password);
byte[] crypted = ProtectedData.Protect(pass, entropy, DataProtectionScope.LocalMachine);
var cryptedBase64 = Convert.ToBase64String(crypted);
return cryptedBase64;
}
public string Decrypt(string username, string password, string address)
{
try
{
var pass = Convert.FromBase64String(password);
byte[] entropy = GetEntropy(address + username);
pass = ProtectedData.Unprotect(pass, entropy, DataProtectionScope.LocalMachine);
return Encoding.UTF8.GetString(pass);
}
catch (Exception ex)
{
return "";
}
}
}
}
+32
View File
@@ -1,4 +1,7 @@
using System;
using System.IO;
using System.Windows.Forms;
using Newtonsoft.Json;
namespace JiraTimeBotForm.Configuration
{
@@ -14,5 +17,34 @@ namespace JiraTimeBotForm.Configuration
public TimeSpan ActivationTime { get; set; }
public bool DummyMode { get; set; }
public bool AddCommentsToWorklog { get; set; }
public static Settings Load()
{
var settingsPath = Path.Combine(Application.UserAppDataPath, _settingsFileName);
if (!File.Exists(settingsPath))
{
return null;
}
var settingsText = File.ReadAllText(settingsPath);
var settings = JsonConvert.DeserializeObject<Settings>(settingsText);
var password = new PasswordEncryptionClass().Decrypt(settings.JiraUserName, settings.JiraPassword, settings.JiraUrl);
settings.JiraPassword = password;
return settings;
}
public void Save()
{
var password = new PasswordEncryptionClass().Encrypt(this.JiraUserName, this.JiraPassword, this.JiraUrl);
this.JiraPassword = password;
var settingsPath = Path.Combine(Application.UserAppDataPath, _settingsFileName);
var settingsString = JsonConvert.SerializeObject(this);
File.WriteAllText(settingsPath, settingsString);
}
private static string _settingsFileName = "settings.json";
}
}