Remember my last post “Generating App Hash key for SMS Retriever API for (Xamarin incl.)…” where I shared with you guys how to successfully generating the App hash key in both MacOs and Windows environments without any hassle?
Now this is the improved alternative of it! making the whole process much easier specially for Xamarin Android development!
Previously…
So earlier we looked at how we could generate the App Hash key straight away from MacOS Terminal or on Windows with the keystore hex generation followed by the Xamarin Android helper method execution to retrieve the app hash key!
That specific helper method I had extracted partially from the Google’s official documentation sample regarding the SMS Retriever API, which I couldn’t completely port to Xamarin Android at that time while I was working on my private project.
But later I took a proper look at it and figured out how to completely port it to Xamarin Android, with a few minutes of back and forth between the Java code and C# Xamarin namespaces! 😀
Easier and Faster method!
Now with the fully ported code, as an extension helper class, we could easily generate the Hash key for our Xamarin Android app either on Windows or Mac using Visual Studio, without having to use any external Terminal or Command prompt.
but you need to follow some pre-requisites to get it to properly work..
Let’s get started!
Get your pre-requisites right!
You need to make sure you’re using the correct Keystore file to sign your APK during build, therefore navigate to your Xamarin Android project properties and set the correct Keystore file path and configuration.
In Visual Studio on Windows:
In Visual Studio on Mac:
That’s done!
Finally make sure you have given the proper app package name or app id to your app in the same project properties. ex: com.testapp.xyz
Why you ask? Since we’re directly generating the app hash code from the project itself, we need to make sure during the compilation the app, the final APK is signed using the keystore we are hoping to use in production. So we can retrieved the signed hash hex from the app Context at run time and the app id to generate the app hash code. 🙂
Then let’s begin the AppHashKeyHelper implementation… 😉
Let the implementation begin…
Let the implementation begin! lol 😀 So here is the complete ported helper class for generating the app hash key for your Xamarin Android app!
Simply copy the whole code to anywhere in your Xamarin Android project, and you could straight away call it from anywhere in the app to generate the app hash key!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Text; | |
using Android.Content; | |
using Android.Content.PM; | |
using Android.Util; | |
using Java.Security; | |
using Java.Util; | |
namespace WhateverNameSpace.Droid.Util | |
{ | |
/// <summary> | |
/// This is a helper class to generate your message hash to be included in your SMS message. | |
/// | |
/// Without the correct hash, your app won't recieve the message callback. This only needs to be | |
/// generated once per app and stored.Then you can remove this helper class from your code. | |
/// | |
/// Ported to Xamarin C# from the AppSignatureHelper.java offcial Android sample | |
/// – Udara Alwis | |
/// </summary> | |
public class AppHashKeyHelper | |
{ | |
private static string HASH_TYPE = "SHA-256"; | |
private static int NUM_HASHED_BYTES = 9; | |
private static int NUM_BASE64_CHAR = 11; | |
/// <summary> | |
/// Retrieve the app signed package signature | |
/// known as signed keystore file hex string | |
/// </summary> | |
/// <param name="context"></param> | |
/// <returns></returns> | |
private static string GetPackageSignature(Context context) | |
{ | |
PackageManager packageManager = context.PackageManager; | |
var signatures = packageManager.GetPackageInfo(context.PackageName, PackageInfoFlags.Signatures).Signatures; | |
return signatures.First().ToCharsString(); | |
} | |
/// <summary> | |
/// Gets the app hash key. | |
/// </summary> | |
/// <returns>The app hash key.</returns> | |
/// <param name="context">Android app Context.</param> | |
public static string GetAppHashKey(Context context) | |
{ | |
string keystoreHexSignature = GetPackageSignature(context); | |
String appInfo = context.PackageName + " " + keystoreHexSignature; | |
try | |
{ | |
MessageDigest messageDigest = MessageDigest.GetInstance(HASH_TYPE); | |
messageDigest.Update(Encoding.UTF8.GetBytes(appInfo)); | |
byte[] hashSignature = messageDigest.Digest(); | |
hashSignature = Arrays.CopyOfRange(hashSignature, 0, NUM_HASHED_BYTES); | |
String base64Hash = Android.Util.Base64.EncodeToString(hashSignature, Base64Flags.NoPadding | Base64Flags.NoWrap); | |
base64Hash = base64Hash.Substring(0, NUM_BASE64_CHAR); | |
return base64Hash; | |
} | |
catch (NoSuchAlgorithmException e) | |
{ | |
return null; | |
} | |
} | |
} | |
} |
We’re simply passing in the Android App Context, which will then first retrieve the signed kestore hash string from the Package Manager.
Then we retrieve the app package name from the Context itself, and move ahead with the usual encrypted key generation.
I would recommend calling this from your MainActivity itself, since this is just a one time execution in your local debug environment.
There you go! Simple as that! 😀
Now once again, keeping in mind to make sure to remove the above helper class call from your project code, before you submit your final APK to the Play Store, since google does not recommend generating the App’s hash key during user run time. Just simply retrive your hash key and remove the code, and then set it up in your SMS API gateway execution.
Thats it! 😀
Cheers!
Share the love! 😀 ❤