Files
DE70/.svn/pristine/b2/b271214ec62b3c6b5a262ad8fec08ae69da51a10.svn-base
2024-09-02 10:03:23 +07:00

69 lines
2.0 KiB
Plaintext

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")));
}
}
}
}