Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/DeviceId/Components/MacosProcessorIdDeviceIdComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using DeviceId.CommandExecutors;
using DeviceId.Internal;

namespace DeviceId.Components
{
/// <summary>
/// An implementation of <see cref="IDeviceIdComponent"/> that uses the root drive's serial number.
/// </summary>
public class MacosProcessorIdDeviceIdComponent
: IDeviceIdComponent
{
/// <summary>
/// Gets the name of the component.
/// </summary>
public string Name { get; } = "ProcessorId";

/// <summary>
/// Command executor.
/// </summary>
private readonly ICommandExecutor _commandExecutor;

/// <summary>
/// Should the contents of the file be hashed? (Relevant for sources such as /proc/cpuinfo)
/// </summary>
private readonly bool _shouldHashContents;

/// <summary>
/// Initializes a new instance of the <see cref="MacosProcessorIdDeviceIdComponent"/> class.
/// </summary>
public MacosProcessorIdDeviceIdComponent(bool shouldHashContents = false)
: this(CommandExecutor.Bash, shouldHashContents) { }

/// <summary>
/// Initializes a new instance of the <see cref="MacosProcessorIdDeviceIdComponent"/> class.
/// </summary>
/// <param name="commandExecutor">The command executor to use.</param>
/// <param name="shouldHashContents">Whether the result contents should be hashed</param>
internal MacosProcessorIdDeviceIdComponent(ICommandExecutor commandExecutor, bool shouldHashContents = false)
{
_commandExecutor = commandExecutor;
_shouldHashContents = shouldHashContents;
}

/// <summary>
/// Gets the component value.
/// </summary>
/// <returns>The component value.</returns>
public string GetValue()
{
var contents = _commandExecutor.Execute("sysctl -a | grep machdep.cpu");

if (!string.IsNullOrEmpty(contents))
{
if (!_shouldHashContents)
{
return contents;
}

using var hasher = MD5.Create();
var hash = hasher.ComputeHash(Encoding.ASCII.GetBytes(contents));
return BitConverter.ToString(hash).Replace("-", "").ToUpper();
}

return null;
}

}
}
18 changes: 18 additions & 0 deletions src/DeviceId/DeviceIdBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public static DeviceIdBuilder AddProcessorId(this DeviceIdBuilder builder)
{
return builder.AddComponent(new FileDeviceIdComponent("ProcessorId", "/proc/cpuinfo", true));
}
else if (OS.IsOSX)
{
return builder.AddComponent(new MacosProcessorIdDeviceIdComponent(true));
}
else
{
return builder.AddComponent(new UnsupportedDeviceIdComponent("ProcessorId"));
Expand All @@ -111,6 +115,13 @@ public static DeviceIdBuilder AddMotherboardSerialNumber(this DeviceIdBuilder bu
{
return builder.AddComponent(new FileDeviceIdComponent("MotherboardSerialNumber", "/sys/class/dmi/id/board_serial"));
}
else if (OS.IsOSX)
{
return builder.AddComponent(new CommandComponent(
name: "MotherboardSerialNumber",
command: "ioreg -d2 -c IOPlatformExpertDevice | awk -F\" '/board-id/{print $(NF-1)}'",
commandExecutor: CommandExecutor.Bash));
}
else
{
return builder.AddComponent(new UnsupportedDeviceIdComponent("MotherboardSerialNumber"));
Expand Down Expand Up @@ -160,6 +171,13 @@ public static DeviceIdBuilder AddSystemUUID(this DeviceIdBuilder builder)
{
return builder.AddComponent(new FileDeviceIdComponent("SystemUUID", "/sys/class/dmi/id/product_uuid"));
}
else if (OS.IsOSX)
{
return builder.AddComponent(new CommandComponent(
name: "SystemUUID",
command: "ioreg -d2 -c IOPlatformExpertDevice | awk -F\" '/IOPlatformUUID/{print $(NF-1)}'",
commandExecutor: CommandExecutor.Bash));
}
else
{
return builder.AddComponent(new UnsupportedDeviceIdComponent("SystemUUID"));
Expand Down