244 lines
7.9 KiB
C#
244 lines
7.9 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace EgwCoreLib.Utils
|
|
{
|
|
/// <summary>
|
|
/// Classe validazione valori
|
|
/// </summary>
|
|
public class MachineDataValidator
|
|
{
|
|
#region Public Fields
|
|
|
|
public static Dictionary<string, string> biosInfo = new Dictionary<string, string>();
|
|
|
|
public static Dictionary<string, string> cpuInfo = new Dictionary<string, string>();
|
|
|
|
public static Dictionary<string, string> netInfo = new Dictionary<string, string>();
|
|
|
|
public static Dictionary<string, string> osInfo = new Dictionary<string, string>();
|
|
|
|
public static Dictionary<string, string> ramInfo = new Dictionary<string, string>();
|
|
|
|
public static Dictionary<string, string> userInfo = new Dictionary<string, string>();
|
|
|
|
public static Dictionary<string, string> volInfo = new Dictionary<string, string>();
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Constructors
|
|
|
|
/// <summary>
|
|
/// Init classe validatore
|
|
/// </summary>
|
|
/// <param name="currMachineData"></param>
|
|
public MachineDataValidator()
|
|
{
|
|
// inizializzo info di base
|
|
if (machTestData == null || machTestData.Count == 0)
|
|
{
|
|
machTestData = setupData();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Init classe validatore
|
|
/// </summary>
|
|
/// <param name="currMachineData"></param>
|
|
public MachineDataValidator(string rawData)
|
|
{
|
|
// inizializzo info di base
|
|
Dictionary<string, string> currMachineData = new Dictionary<string, string>();
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
try
|
|
{
|
|
currMachineData = JsonConvert.DeserializeObject<Dictionary<string, string>>(rawData);
|
|
}
|
|
catch (Exception exc)
|
|
{ }
|
|
}
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Properties
|
|
|
|
public bool hasValidData
|
|
{
|
|
get => machTestData != null && machTestData.Count > 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restituisce HASH del codice impiego = payload utente
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string macHash
|
|
{
|
|
get => calcMachineHash();
|
|
}
|
|
|
|
#endregion Public Properties
|
|
|
|
#if false
|
|
/// <summary>
|
|
/// Verifica validità info correnti
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<bool> testCurrInfo()
|
|
{
|
|
return await testInfo();
|
|
}
|
|
#endif
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Verifica validità info correnti
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static async Task<bool> testCurrInfo()
|
|
{
|
|
var cDV = new MachineDataValidator();
|
|
return await cDV.testInfo();
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
/// <summary>
|
|
/// Dati rispetto cui fare i calcoli di validazione
|
|
/// </summary>
|
|
private static Dictionary<string, string> machTestData = new Dictionary<string, string>();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Calcola HASH del codice impiego = payload utente
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private string calcMachineHash()
|
|
{
|
|
string hash = "";
|
|
string buffData = currBaseBuffer();
|
|
if (!string.IsNullOrEmpty(buffData))
|
|
{
|
|
// hashing!
|
|
|
|
using (var hAlgo = SHA512.Create())
|
|
//using (var hAlgo = MD5.Create())
|
|
{
|
|
byte[] InputBytes = Encoding.UTF8.GetBytes(buffData);
|
|
var byteHash = hAlgo.ComputeHash(InputBytes);
|
|
hash = BitConverter.ToString(byteHash).Replace("-", "").Replace("/", "").Replace("\\", "").ToLowerInvariant();
|
|
}
|
|
}
|
|
return hash;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calcola hash key da info PC
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private string currBaseBuffer()
|
|
{
|
|
string answ = "????????????????????????????????????????????";
|
|
StringBuilder sb = new StringBuilder();
|
|
try
|
|
{
|
|
// calcolo hashKey da alcuni valori solamente
|
|
sb.Append($"{machTestData["SystemName"]}|");
|
|
sb.Append($"{machTestData["ProcessorId"]}|");
|
|
sb.Append($"{machTestData["PartNumber"]}|");
|
|
sb.Append($"{machTestData["MACAddress"]}|");
|
|
sb.Append($"{machTestData["SerialNumber"]}|");
|
|
sb.Append($"{machTestData["OSArchitecture"]}|");
|
|
sb.Append($"{machTestData["SoftwareElementID"]}|");
|
|
sb.Append($"{machTestData["SystemBiosMajorVersion"]}|");
|
|
sb.Append($"{machTestData["SystemBiosMinorVersion"]}|");
|
|
sb.Append($"{machTestData["CpuVersion"]}|");
|
|
sb.Append($"{machTestData["BiosVersion"]}|");
|
|
if (sb.Length > 0)
|
|
{
|
|
answ = sb.ToString();
|
|
// lo uso 2 volte dritto e reverse...
|
|
answ += ReverseString(sb.ToString());
|
|
}
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Legge la hash key dal file licenza salvato
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
private async Task<string> getSavedHash()
|
|
{
|
|
string answ = "############################################################";
|
|
string filePath = "Conf/lic.file";
|
|
if (File.Exists(filePath))
|
|
{
|
|
answ = await File.ReadAllTextAsync(filePath);
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Esegue reverse delal stringa
|
|
/// </summary>
|
|
/// <param name="myStr"></param>
|
|
/// <returns></returns>
|
|
private string ReverseString(string myStr)
|
|
{
|
|
char[] myArr = myStr.ToCharArray();
|
|
Array.Reverse(myArr);
|
|
return new string(myArr);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Predispone elenco dati di test
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private Dictionary<string, string> setupData()
|
|
{
|
|
// creo un unico array di info
|
|
Dictionary<string, string> testData = new Dictionary<string, string>();
|
|
biosInfo = MachineInfo.GetInfo("BIOS");
|
|
cpuInfo = MachineInfo.GetInfo("CPU");
|
|
netInfo = MachineInfo.GetInfo("NET");
|
|
osInfo = MachineInfo.GetInfo("OS");
|
|
ramInfo = MachineInfo.GetInfo("RAM");
|
|
userInfo = MachineInfo.GetInfo("USER");
|
|
volInfo = MachineInfo.GetInfo("VOL");
|
|
testData = biosInfo.Concat(cpuInfo).Concat(netInfo).Concat(osInfo).Concat(ramInfo).GroupBy(ele => ele.Key).ToDictionary(ele => ele.Key, ele => ele.First().Value);
|
|
return testData;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua verifica putuale tra hash Key fornita ed hash key calcolata da info PC
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private async Task<bool> testInfo()
|
|
{
|
|
// recupero hash key da conf applicativo (licenza)
|
|
string fileHash = await getSavedHash();
|
|
/// recupero hash calcolata
|
|
string calcHash = calcMachineHash();
|
|
// confronto con hashKey calcolata da info PC + dati...
|
|
return fileHash.Equals(calcHash);
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |