Muhammad Noamany
Muhammad Noamany

Reputation: 33

How to generate hash signature?

I am using twilio sdk to integrate sms verification with my android app, twilio asks me to register hash signature of my app and non of ways to generate hash signature worked, always says it is invalid hash signature this is the path that twilio wants the hash signature

diagram here

Upvotes: 3

Views: 13533

Answers (3)

dragon788
dragon788

Reputation: 3931

If you have opted your app into Google Play managed signing, then you need to use a different set of steps to get the correct signature hash.

How to configure Signature Hash with Google Play Signing · Issue #1550 · AzureAD/microsoft-authentication-library-for-android

Upvotes: 0

Zealous System
Zealous System

Reputation: 2324

User below command in terminal of android studio to generate release key hash

keytool -exportcert -alias [aliasname] -keystore [your app keystore path] | openssl sha1 -binary | openssl base64

And below code in your project to get development key hash

try {
    android.content.pm.PackageInfo info = getPackageManager().getPackageInfo(
            "com.apps.sonictonic",
            android.content.pm.PackageManager.GET_SIGNATURES);
    for (android.content.pm.Signature signature : info.signatures) {
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("SHA");
        md.update(signature.toByteArray());
        android.util.Log.d("KeyHash", "KeyHash:" + android.util.Base64.encodeToString(md.digest(),
                android.util.Base64.DEFAULT));

    }
} catch (android.content.pm.PackageManager.NameNotFoundException e) {

} catch (java.security.NoSuchAlgorithmException e) {

}

Upvotes: 7

Rohit Kumar
Rohit Kumar

Reputation: 21

Call this method in your Activity onCreate() and Search your key in logcat using key "HashKey"

public static void printHashKey(Context pContext) {
    try {
        PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            String hashKey = new String(Base64.encode(md.digest(), 0));
            Log.d("HashKey", "printHashKey() Hash Key: " + hashKey);
        }
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "printHashKey()", e);
    } catch (Exception e) {
        Log.e(TAG, "printHashKey()", e);
    }
}

Upvotes: 1

Related Questions