69 lines
2.0 KiB
C#
69 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Management;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SafeBagDE70.Services
|
|
{
|
|
public static class HardwareInforManager
|
|
{
|
|
public static string GetHardwareId()
|
|
{
|
|
string cpuId = GetCpuId();
|
|
string biosId = GetBiosId();
|
|
string motherboardId = GetMotherboardId();
|
|
|
|
// Combine the strings and create a hash
|
|
string combined = cpuId + biosId + motherboardId;
|
|
string hardwareId = CreateMD5Hash(combined);
|
|
|
|
return hardwareId;
|
|
}
|
|
|
|
public static string GetCpuId()
|
|
{
|
|
return GetHardwareInfo("Win32_Processor", "ProcessorId");
|
|
}
|
|
|
|
public static string GetBiosId()
|
|
{
|
|
return GetHardwareInfo("Win32_BIOS", "SerialNumber");
|
|
}
|
|
|
|
public static string GetMotherboardId()
|
|
{
|
|
return GetHardwareInfo("Win32_BaseBoard", "SerialNumber");
|
|
}
|
|
|
|
private static string GetHardwareInfo(string wmiClass, string wmiProperty)
|
|
{
|
|
try
|
|
{
|
|
ManagementObjectSearcher searcher = new ManagementObjectSearcher($"select {wmiProperty} from {wmiClass}");
|
|
foreach (ManagementObject obj in searcher.Get())
|
|
{
|
|
return obj[wmiProperty]?.ToString() ?? string.Empty;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception($"Failed to retrieve {wmiClass} {wmiProperty}: " + ex.Message);
|
|
}
|
|
return string.Empty;
|
|
}
|
|
|
|
private static string CreateMD5Hash(string input)
|
|
{
|
|
using (MD5 md5 = MD5.Create())
|
|
{
|
|
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
|
|
byte[] hashBytes = md5.ComputeHash(inputBytes);
|
|
return string.Join("", hashBytes.Select(b => b.ToString("X2")));
|
|
}
|
|
}
|
|
}
|
|
}
|