Modifiche principali: - Aggiornamento di App.config per il framework v4.8.1 e nuove impostazioni utente. - Aggiornamento di BSHash.csproj con nuovi riferimenti a librerie NuGet. - Introduzione di logica per la gestione degli utenti e delle credenziali in Database.cs. - Aggiunta di funzionalità di logging avanzate in Logger.cs. - Implementazione di un gestore API in Network.cs per inviare e ricevere dati hash. - Nuovi metodi per la gestione di file in Storage.cs con logging dettagliato. - Introduzione di classi come ApiManager, UserSession, CredentialManager e FileScanner. - Miglioramenti all'interfaccia utente per la gestione delle impostazioni del database e del login. - Rimozione di codice obsoleto per semplificare e migliorare la manutenzione.
71 lines
2.2 KiB
C#
71 lines
2.2 KiB
C#
using System;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
namespace BSHash
|
|
{
|
|
public static class CredentialManager
|
|
{
|
|
public static void SaveCredentials(string username, string password)
|
|
{
|
|
try
|
|
{
|
|
var credentials = new UserCredentials { Username = username, Password = password };
|
|
var json = JsonSerializer.Serialize(credentials);
|
|
var encrypted = ProtectData(json);
|
|
|
|
Properties.Settings.Default.SavedCredentials = encrypted;
|
|
Properties.Settings.Default.Save();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception($"Errore nel salvataggio delle credenziali: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public static UserCredentials LoadCredentials()
|
|
{
|
|
try
|
|
{
|
|
var encrypted = Properties.Settings.Default.SavedCredentials;
|
|
if (string.IsNullOrEmpty(encrypted))
|
|
return null;
|
|
|
|
var decrypted = UnprotectData(encrypted);
|
|
return JsonSerializer.Deserialize<UserCredentials>(decrypted);
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static void ClearCredentials()
|
|
{
|
|
Properties.Settings.Default.SavedCredentials = string.Empty;
|
|
Properties.Settings.Default.Save();
|
|
}
|
|
|
|
private static string ProtectData(string data)
|
|
{
|
|
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
|
|
byte[] protectedBytes = ProtectedData.Protect(dataBytes, null, DataProtectionScope.CurrentUser);
|
|
return Convert.ToBase64String(protectedBytes);
|
|
}
|
|
|
|
private static string UnprotectData(string protectedData)
|
|
{
|
|
byte[] protectedBytes = Convert.FromBase64String(protectedData);
|
|
byte[] dataBytes = ProtectedData.Unprotect(protectedBytes, null, DataProtectionScope.CurrentUser);
|
|
return Encoding.UTF8.GetString(dataBytes);
|
|
}
|
|
}
|
|
|
|
public class UserCredentials
|
|
{
|
|
public string Username { get; set; }
|
|
public string Password { get; set; }
|
|
}
|
|
}
|