获取SHA1发布码
2020-11-16 本文已影响0人
总是lin
一.通过Android studio 工具获取
1、打开Android Studio的Terminal工具。
2、输入命令:keytool -v -list -keystore keystore文件路径。
3、输入Keystore密码。
keytool -list -v -keystore ~/.android/debug.keystore -keypass android
二.动态代码获取SHA1
fun sHA1(context:Context):String? {
try {
val info = context.packageManager.getPackageInfo(
context.packageName, PackageManager.GET_SIGNATURES
)
val cert = info.signatures[0].toByteArray()
val md =MessageDigest.getInstance("SHA1")
val publicKey = md.digest(cert)
val hexString = StringBuffer()
for (iin publicKey.indices) {
val appendString =Integer.toHexString(0xFF and publicKey[i].toInt())
.toUpperCase(Locale.US)
if (appendString.length ==1) hexString.append("0")
hexString.append(appendString)
hexString.append(":")
}
var result:String = hexString.toString()
return result.substring(0, result.length -1)
}catch (e:PackageManager.NameNotFoundException) {
e.printStackTrace()
}catch (e:NoSuchAlgorithmException) {
e.printStackTrace()
}
return null
}