Kotlin Study

Android HashKey 구하기 (feat.Kotlin)

85chong 2021. 3. 19. 10:48
728x90
반응형
SMALL

 

 

    fun getHashKey(context: Context) = try {
        val signature = when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.P -> {
                context.packageManager.getPackageInfo(context.packageName, PackageManager.GET_SIGNING_CERTIFICATES).signingInfo.apkContentsSigners[0]
            }
            else -> {
                context.packageManager.getPackageInfo(context.packageName, PackageManager.GET_SIGNATURES).signatures[0]
            }
        }
        MessageDigest.getInstance("SHA").let { messageDigest ->
            messageDigest.update(signature.toByteArray())
            String(Base64.encode(messageDigest.digest(), Base64.DEFAULT)).trim()
        }
    } catch (e:Exception) {
        ""
    }

import 할때 Base64 여러개 나와서 헷갈릴 수 있음

 

* import java.security.MessageDigest

* import android.util.Base64

 

 

- 끝 -