fungetSelfVersionCode(context: Context): Long { val packageManager = context.packageManager val info = packageManager.getPackageInfo(context.packageName, 0) return PackageInfoCompat.getLongVersionCode(info) }
public String getSelfSignToSHA1(Context context) { try { PackageManagerpackageManager= context.getPackageManager(); List<Signature> signatureList = PackageInfoCompat.getSignatures(packageManager,context.getPackageName()); byte[] cert = signatureList.get(0).toByteArray(); MessageDigestmd= MessageDigest.getInstance("SHA1"); byte[] publicKey = md.digest(cert); StringBuilderhexString=newStringBuilder(); for (inti=0; i < publicKey.length; i++) { StringappendString= Integer.toHexString(0xFF & publicKey[i]).toUpperCase(Locale.US); if (appendString.length() == 1)hexString.append("0"); hexString.append(appendString); if (i != publicKey.length - 1) hexString.append(":"); } return hexString.toString(); } catch (Exception e) { e.printStackTrace(); returnnull; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
fungetSelfSign(context: Context): String { val packageManager = context.packageManager val signatures = PackageInfoCompat.getSignatures(packageManager, context.packageName) val bytes = signatures[0].toByteArray() val messageDigest = MessageDigest.getInstance("SHA1") val publicKey: ByteArray = messageDigest.digest(bytes) val hexString = StringBuilder() for (i in publicKey.indices) { val appendString = Integer.toHexString(0xFF and publicKey[i].toInt()) .toUpperCase(Locale.US) if (appendString.length == 1) hexString.append("0") hexString.append(appendString) if (i != publicKey.size - 1) hexString.append(":") } return hexString.toString() }