美文网首页
Android获取sha1

Android获取sha1

作者: ChaosHeart | 来源:发表于2023-10-09 15:49 被阅读0次

    代码获取sha1

    
        private fun getSHA1() {
            val packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES)
            val signatures: Array<Signature> = packageInfo.signatures
            val cert = signatures[0].toByteArray()
            val md = MessageDigest.getInstance("SHA1")
            val sha1 = md.digest(cert)
            val hexDigest: String = byte2HexFormatted(sha1)
            Log.d("SHA1:", hexDigest)
        }
    
    ///FF:48:35:A6:51:4D:74:EB:C7:39:21:C6:36:F7:76:C1:11:7B:A6:0E
        private fun byte2HexFormatted(arr: ByteArray): String {
            val str = StringBuilder(arr.size * 2)
            for (i in arr.indices) {
                var h = Integer.toHexString(arr[i].toInt())
                val l = h.length
                if (l == 1) {
                    h = "0$h"
                }
                if (l > 2) {
                    h = h.substring(l - 2, l)
                }
                str.append(h.toUpperCase())
                if (i < arr.size - 1) {
                    str.append(':')
                }
            }
            return str.toString()
        }
    
    
    ///ff4835a6514d74ebc73921c636f776c1117ba60e
    fun byte2hex(src: ByteArray): String {
        val stringBuilder = StringBuilder("")
        for (i in 0 until src.size) {
            val v = src[i].toInt() and 0xFF
            val hv = Integer.toHexString(v)
            if (hv.length < 2) {
                stringBuilder.append(0)
            }
            stringBuilder.append(hv)
        }
        return stringBuilder.toString()
    }
    

    命令获取sha1
    iMES_APP.jks 是 你的签名文件

    keytool -v -list -keystore iMES_APP.jks
    
    

    相关文章

      网友评论

          本文标题:Android获取sha1

          本文链接:https://www.haomeiwen.com/subject/vuvibdtx.html