ironlicensing-dotnet/MachineIdentifier.cs

39 lines
1.1 KiB
C#

using System.Security.Cryptography;
using System.Text;
namespace IronLicensing.Client;
public partial class MachineIdentifier : IMachineIdentifier
{
public string GetMachineId()
{
var components = GetMachineComponents();
var combined = string.Join("|", components.Where(c => !string.IsNullOrEmpty(c)));
if (string.IsNullOrEmpty(combined))
{
// Fallback to a device-specific identifier
combined = DeviceInfo.Current.Idiom.ToString() + "|" +
DeviceInfo.Current.Manufacturer + "|" +
DeviceInfo.Current.Model;
}
using var sha256 = SHA256.Create();
var hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(combined));
return Convert.ToBase64String(hash)[..32];
}
public string GetMachineName()
{
return DeviceInfo.Current.Name ?? Environment.MachineName ?? "Unknown";
}
public string GetPlatform()
{
return DeviceInfo.Current.Platform.ToString();
}
// Platform-specific implementation
private partial List<string> GetMachineComponents();
}