Skip to content

Instantly share code, notes, and snippets.

@alexstyl
Last active November 3, 2022 14:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexstyl/19e95745d5e37d94f32b576426a0d53c to your computer and use it in GitHub Desktop.
Save alexstyl/19e95745d5e37d94f32b576426a0d53c to your computer and use it in GitHub Desktop.
Get MacOS Machine ID
// 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