SMS Retriever API: Computing your app’s hash

You can find info on what the SMS Retriever API is here. Chances are that reaching this page means you already know that. For some reasons the hash generation process described in the docs did not work for me. Probably it’s a Mac only issue or the implementation of the sha256sum function I installed with coreutils.

What worked for me is getting the hash from within the App. A helper class for this is provided in Google Samples project.

A Kotlin snippet that does the same can be found bellow.

fun getAppSignature(context: Context) = context.packageManager.getPackageInfo(context.packageName, PackageManager.GET_SIGNATURES)
        .signatures.mapNotNull { hash(context.packageName, it.toCharsString()) }.firstOrNull()

private fun hash(packageName: String, signature: String) = try {
    val messageDigest = MessageDigest.getInstance("SHA-256")
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        messageDigest.update("$packageName $signature".toByteArray(StandardCharsets.UTF_8))
    }
    var hashSignature = messageDigest.digest()
    hashSignature = Arrays.copyOfRange(hashSignature, 0, 9)
    var base64Hash = Base64.encodeToString(hashSignature, Base64.NO_PADDING or Base64.NO_WRAP)
    base64Hash = base64Hash.substring(0, 11)
    base64Hash
} catch (e: NoSuchAlgorithmException) {
    null
}

Why Kotlin?

I few days ago I gave a talk at Voxxed Days Athens titled “Why Kotlin?

The first part is an introduction to the language (and might be boring for someone already into the language). In the second part I tried to provide some arguments on why Kotlin is here to stay.

My TOP-10 on Why Kotlin is the following:

  1. Makes writing code easier
  2. Has incremental learning curve
  3. Has nice features
  4. Follows modern programming language trends
  5. Can be easily mixed with Java
  6. Suitable for incremental adoption
  7. On Android there is no way back
  8. Is designed by Jetbrains that makes some of the best developer tools
  9. Is supported by major vendors (Google, Spring etc)
  10. Kotlin has a growing community

You can watch the full presentation above. The slides are also available at SpeakerDeck.
[Direct YouTube link]