본문 바로가기
Android

[Android] AOS - SMS Retriever API 해쉬코드

by s_hoonee 2024. 6. 12.
반응형

굉장한 삽질 끝에 진정한 내 앱의 해쉬값을 찾을 수 있었다. 해당 코드를 사용하십시오..............  

import android.content.Context
import android.content.ContextWrapper
import android.content.pm.PackageManager
import android.content.pm.Signature
import android.util.Base64
import android.util.Log
import java.nio.charset.StandardCharsets
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import java.util.Arrays

class AppSignatureHelper(context: Context) : ContextWrapper(context) {
    val TAG = AppSignatureHelper::class.java.simpleName
    private val HASH_TYPE = "SHA-256"
    val NUM_HASHED_BYTES = 9
    val NUM_BASE64_CHAR = 11

    init {
        getAppSignatures()
    }

    fun getAppSignatures(): ArrayList<String>? {
        val appCodes = ArrayList<String>()
        try {
            val packageName = packageName
            val packageManager = packageManager
            val signatures: Array<Signature> = packageManager.getPackageInfo(
                packageName, PackageManager.GET_SIGNATURES
            ).signatures

            for (signature in signatures) {
                val hash = hash(packageName, signature.toCharsString())
                if (hash != null) {
                    appCodes.add(String.format("%s", hash))
                }
                Log.e(TAG, "Hash $hash")
            }
        } catch (e: PackageManager.NameNotFoundException) {
            Log.e(TAG, "Unable to find package to obtain hash.", e)
        }
        return appCodes
    }

    private fun hash(packageName: String, signature: String): String? {
        val appInfo = "$packageName $signature"
        try {
            val messageDigest = MessageDigest.getInstance(HASH_TYPE)
            messageDigest.update(appInfo.toByteArray(StandardCharsets.UTF_8))
            var hashSignature = messageDigest.digest()
            hashSignature = Arrays.copyOfRange(hashSignature, 0, NUM_HASHED_BYTES)
            var base64Hash = Base64.encodeToString(hashSignature, Base64.NO_PADDING or Base64.NO_WRAP)
            base64Hash = base64Hash.substring(0, NUM_BASE64_CHAR)
            Log.e(TAG, String.format("pkg: %s -- hash: %s", packageName, base64Hash))
            return base64Hash
        } catch (e: NoSuchAlgorithmException) {
            Log.e(TAG, "hash:NoSuchAlgorithm", e)
        }
        return null
    }
}

 

이렇게 가져오면 SMS Retriever API에 필요한 11자리가 잘 찍힌다!! 

      val helper = AppSignatureHelper(this)
      val hash = helper.getAppSignatures()?.get(0)

      Log.d("smsTest", "Hash: $hash")

 

'Android' 카테고리의 다른 글

[Android] 나의 첫 출시 어플 - "대타 출동"  (1) 2023.11.24