Unique Hardware Id in MAC Os X

Unique hardware ID in Mac OS X

Try this Terminal command:

ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/ { split($0, line, "\""); printf("%s\n", line[4]); }'

From here

Here is that command wrapped in Cocoa (which could probably be made a bit cleaner):

NSArray * args = [NSArray arrayWithObjects:@"-rd1", @"-c", @"IOPlatformExpertDevice", @"|", @"grep", @"model", nil];
NSTask * task = [NSTask new];
[task setLaunchPath:@"/usr/sbin/ioreg"];
[task setArguments:args];

NSPipe * pipe = [NSPipe new];
[task setStandardOutput:pipe];
[task launch];

NSArray * args2 = [NSArray arrayWithObjects:@"/IOPlatformUUID/ { split($0, line, \"\\\"\"); printf(\"%s\\n\", line[4]); }", nil];
NSTask * task2 = [NSTask new];
[task2 setLaunchPath:@"/usr/bin/awk"];
[task2 setArguments:args2];

NSPipe * pipe2 = [NSPipe new];
[task2 setStandardInput:pipe];
[task2 setStandardOutput:pipe2];
NSFileHandle * fileHandle2 = [pipe2 fileHandleForReading];
[task2 launch];

NSData * data = [fileHandle2 readDataToEndOfFile];
NSString * uuid = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

Unique Identifier of a Mac?

Apple has a technote on uniquely identifying a mac. Here's a loosely modified version of the code Apple has posted in that technote... don't forget to link your project against IOKit.framework in order to build this:

#import <IOKit/IOKitLib.h>

- (NSString *)serialNumber
{
io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault,

IOServiceMatching("IOPlatformExpertDevice"));
CFStringRef serialNumberAsCFString = NULL;

if (platformExpert) {
serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert,
CFSTR(kIOPlatformSerialNumberKey),
kCFAllocatorDefault, 0);
IOObjectRelease(platformExpert);
}

NSString *serialNumberAsNSString = nil;
if (serialNumberAsCFString) {
serialNumberAsNSString = [NSString stringWithString:(NSString *)serialNumberAsCFString];
CFRelease(serialNumberAsCFString);
}

return serialNumberAsNSString;
}

Getting Unique System ID on Mac using C#(Mono)

First, stay away from using a MAC address as those can change (spoofed or hardware change) and is not a very Mac'ie way since 10.3/4?... Since then the "I/O Kit registry" is the preferred method to get a system id. Drop to a cmd line and man ioreg for same details.

So, not sure if you are using Xamarin.Mac or not, so if you are you could do a binding of the IOKit IORegistryEntryFromPath/IORegistryEntryCreateCFProperty APIs as I do not believe those are currently in C# wrappers, but really quick way that I use is:

using System;
using System.Diagnostics;
using System.Text;

namespace uuidmac
{
class MainClass
{
static string MacUUID ()
{
var startInfo = new ProcessStartInfo () {
FileName = "sh",
Arguments = "-c \"ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/'\"",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UserName = Environment.UserName
};
var builder = new StringBuilder ();
using (Process process = Process.Start (startInfo)) {
process.WaitForExit ();
builder.Append (process.StandardOutput.ReadToEnd ());
}
return builder.ToString ();
}

public static int Main (string[] args)
{
Console.WriteLine (MacUUID ());
return 0;
}
}
}

Example output:

 "IOPlatformUUID" = "9E134619-9999-9999-9999-90DC147C61A9"

You could also parse out "IOPlatformSerialNumber" instead of the "IOPlatformUUID" if you want something human readable and that someone can find on the main dialog of "About This Mac" for tech support calls...

Now if you are looking at storing and protecting info as in the MS DPAPI, the KeyChain is the place to do that (OSX/iOS). Mono does have some of the DPAPI crypto apis that are x-plat based.

How do I get device id in the Apple Cocoa application?

You can get the Hardware UUID with IOKit

func hardwareUUID() -> String?
{
let matchingDict = IOServiceMatching("IOPlatformExpertDevice")
let platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, matchingDict)
defer{ IOObjectRelease(platformExpert) }

guard platformExpert != 0 else { return nil }
return IORegistryEntryCreateCFProperty(platformExpert, kIOPlatformUUIDKey as CFString, kCFAllocatorDefault, 0).takeRetainedValue() as? String
}


Related Topics



Leave a reply



Submit