Created
October 10, 2024 10:05
-
-
Save hoangchungk53qx1/db5c30d4294b14daa72c953d8c5a8752 to your computer and use it in GitHub Desktop.
DeviceID
This file contains hidden or 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
import android.media.MediaDrm | |
import android.media.UnsupportedSchemeException | |
import android.os.Build | |
import java.security.MessageDigest | |
import java.security.NoSuchAlgorithmException | |
import java.util.UUID | |
object DeviceUtils { | |
fun getUniqueDeviceId(): String? { | |
val uuid = UUID(-0x121074568629b532L, -0x5c37d8232ae2de13L) | |
var mediaDrm: MediaDrm? = null | |
try { | |
mediaDrm = MediaDrm(uuid) | |
val widevineId: ByteArray = | |
mediaDrm.getPropertyByteArray(MediaDrm.PROPERTY_DEVICE_UNIQUE_ID) | |
val md: MessageDigest = MessageDigest.getInstance("SHA-256") | |
md.update(widevineId) | |
return bytesToHex(md.digest()) | |
} catch (e: UnsupportedSchemeException) { | |
e.printStackTrace() | |
} catch (e: NoSuchAlgorithmException) { | |
e.printStackTrace() | |
} finally { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | |
mediaDrm?.close() | |
} else { | |
mediaDrm?.release() | |
} | |
} | |
return null | |
} | |
private const val digits = "0123456789ABCDEF" | |
private fun bytesToHex(byteArray: ByteArray): String { | |
val hexChars = CharArray(byteArray.size * 2) | |
for (i in byteArray.indices) { | |
val v = byteArray[i].toInt() and 0xff | |
hexChars[i * 2] = digits[v shr 4] | |
hexChars[i * 2 + 1] = digits[v and 0xf] | |
} | |
return String(hexChars) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment