Last active
November 3, 2022 14:19
-
-
Save alexstyl/19e95745d5e37d94f32b576426a0d53c to your computer and use it in GitHub Desktop.
Get MacOS Machine ID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Java version of node-machine-id. Used to identify a Mac, for software installation or licenses purposes. | |
// See the node project at https://github.com/automation-stack/node-machine-id | |
suspend fun machineId(): String { | |
val platformUUID = getPlatformUUID() | |
return DigestUtils.sha256Hex(platformUUID) | |
} | |
private fun expose(result: String): String { | |
val startIndex = result.substringAfter("IOPlatformUUID").substringBefore("\n") | |
val regex = "=|\\s+|\"".toRegex() | |
return startIndex.replace(regex, "") | |
.lowercase(); | |
} | |
private suspend fun getPlatformUUID(): String { | |
return expose(execSync("ioreg -rd1 -c IOPlatformExpertDevice")) | |
} | |
private suspend fun execSync(command: String): String = withContext(Dispatchers.IO) { | |
val proc = Runtime.getRuntime().exec(command) | |
val reader = BufferedReader(InputStreamReader(proc.inputStream)) | |
val string = buildString { | |
do { | |
val line = reader.readLine() | |
if(line!=null) { | |
appendLine(line) | |
} | |
} while (line != null) | |
} | |
proc.waitFor() | |
string | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment