Getting android keystore signature keeps changing after some days

55 views Asked by At

I've created a keystore for my app and has always been signing with it. In the app there's a verification to test if the key hash has changed. However today when I exported the apk with and installed it, the key hash has changed.

Since I've never done anything to the keystore, I suppose I shouldn't change. Below is the code snippet.

fun getSHA(context: Context): String {
        val info: PackageInfo
        var message = ""
        try {
            info = context.packageManager.getPackageInfo(context.packageName, PackageManager.GET_SIGNATURES)
            for (signature in info.signatures) {
                if(signature != null){
                    val algo = "SHA256"
                    val md: MessageDigest = MessageDigest.getInstance(algo)
                    md.update(signature.toByteArray())
                    val encode = Base64.encode(md.digest(), Base64.DEFAULT)
                    Log.d(TAG, signature.toCharsString())
                    return String(encode).also {
                        MyLog.d(TAG, "\n\n $it")
                    }
                }
            }
        } catch (e1: PackageManager.NameNotFoundException) {
            message = "$TAG Exception: " + e1.message
        } catch (e: NoSuchAlgorithmException) {
            message = "$TAG Exception: " + e.message
        } catch (e: Exception) {
            message = "$TAG Exception: " + e.message
        }
        return ""
    }
0

There are 0 answers