Tag Archives: Windows

App Hash Key Helper extension for Xamarin Android apps!

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!


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! 😀 ❤

Generating App Hash key for SMS Retriever API for (Xamarin incl.)…

Trying to generate your app’s Hash Key for the implementation of new Android SMS Retriever API? Still couldn’t get it to work? Well the problem could be in the actual generation process of the has string!

And here are some ways you could actually make sure to generate the valid hash string to get the functionality working.

Specially if you’re developing on Windows, or better working on a Xamarin Android app? I’ve got some goodies to make it work as well! 😉

App Hash string for SMS Retriever API

Anyhow, to use the SMS Retriever API we need generate our App’s unique Hash string which is a combination of app’s package name and your app’s public key certificate. Once the SMS is received into the Inbox the SMS Retriever API looks for any matching hash key in the message corresponding to the app that requested to read the SMS, if it finds a perfect match it will kick in the execution of retrieving the SMS string, otherwise it will time out. So without the accurate app hash key the SMS Retriever API will not kick in at all. That is why it is very crucial we generate the correct app hash key for our app using the App package name and keystore hash.

Troublesome!

Although according to the official Google Documentation, it should be easy and straight forward, but it is not, specially if you’re using a Windows Dev Environment, which could be troublesome at times. To add some topping on to that, if you’re working on a Xamarin Android project in Windows, you’re in deep troublesome waters, since even Google Docs only provides samples for the Android Java implementation.

Then the sample command that Google Docs provide doesn’t really throw any error if anything goes wrong during, wrong parameter, or a wrong keyword, during hash string generation, it simply returns a false hash string, which makes things worse!

This is actually something I experienced while I was developing one of our Xamarin Android applications. Oh yes! that’s even more annoying since there’s no official documentation or Xamarin sample of the implementation code regarding that API. 😦

Hence Google’s doc command failed me I had to play around with the parameters we’re passing in to the command, re-arrange and modify to get it to work. But anyhow with a walk in the midst of the dark forest of troubleshooting, I figured out how to properly generate the App Hash key!

Get your pre-requisites right!

First of all prepare your keystore file path, keystore alias name and keystore password, and double check those values are being properly set. This is where most of them goes wrong. Then the obvious requirement, make sure you have Androd

Method 1: On MacOS Terminal

So this is the easiest and most straight forward way to generate the hash string, by simply executing the below command on a MacOS Terminal.

keytool -exportcert -alias <keystore alias name> -keystore <keystore file with extension> | xxd -p | tr -d "[:space:]" | echo -n <your app id> `cat` | sha256sum | tr -d "[:space:]-" | xxd -r -p | base64 | cut -c1-11
  • Replace the <keystore alias name> with your keystore alias name, ex: mykeystorename
  • Replace the <keystore file with extension> with the full path to your keystore file, ex: /Users/username/Desktop/mykeystorefile.keystore
  • Replace the <your app id> with your app id: com.mytest.app

If everything went well, next line Terminal will immediately prompt you to enter the keystore password, which will generate the accurate app hash key!

Yep simple as that! 😀

Now you might wonder how I shared the above screenshot so bravely without worrying about the security, oh well its just using the default Xamarin Android keystore generated by VS for Mac with its default settings and a fake app id! 😛

Troublesome? Get Utils installed!

Unless you got some missing utils in your mac developer machine, such as the following error: sha256sum: command not found, which indicates missing coreutils in your Mac. Therefore you need to fist install Homebrew if you don’t have already.

Install Homebrew: https://brew.sh/

Restart Terminal and run the below command!

brew install coreutils

Restart Terminal and re-run the has generation command! 🙂

Method 2: On Windows CMD Prompt

Now on Windows its going to be a little bit of more work specially most of the command parameters which are used in unix cannot be executed straight up in Windows Command Prompt.

So in hope of avoiding at least a little bit of pain I would recommend using the Visual Studio Command Prompt or the Android ADB Command Prompt in your Windows PC. Specially since the java keytool path should already be configured in the Path property there.

Step 1: Generate the hex string from keystore file

keytool -alias <keystore alias name> -exportcert -keystore "<keystore file with extension>" -storepass "<keystore password>" | xxd -p

Now the parameters that you need to replace are the same as we did on MacOS command, but as you can see keystore file path and password are provided within brackets or quotations. So make sure to add that detail.

If everything went well, it will return you the hex string from your keystore file!

Now that’s me trying out the default debug keystore file in Windows. If everything executes properly, it should return a super long hex string as shown above, actually even much longer than that lol, I just cropped it out 😛

Copy that hex string and keep it, we are gonna use it in the next step.

Step 2: Implement the Hash key generate code script

Next we’re going to port the Google Doc’s Android Java implementation of the Hash key generation execution to Xamarin Android C# code snippet, thanks to my few minutes of trail and error efforts 😛


// move to the class global level
using Android.Util;
using Java.Security;
using Java.Util;
using System.Text;
// move to the class global level
private static string HASH_TYPE = "SHA-256";
private static int NUM_HASHED_BYTES = 9;
private static int NUM_BASE64_CHAR = 11;
/// <summary>
/// Generate App hash key using package
/// name and keystore hex signature
/// </summary>
/// <param name="packageName"></param>
/// <param name="keystoreHexSignature"></param>
/// <returns></returns>
public static string GetAppHashKey
(String packageName, String keystoreHexSignature)
{
string appInfo = 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;
}
}

Simply copy that snippet into a Xamarin Android project with the specific imports mentioned at the top of the snippet. You could use this as a little helper extension method to generate the app hash key by passing in the app package name and keystore hash value that we generated in the step 1, as mentioned in the parameters.

That should work like a charm! 😀

Although keep in mind once you generate the app hash string, you should remove the keystore hex and package name hex from the code before you submit it to the app store. Google does not recommend those sensitive information inside the code as magic strings due to obvious security reasons.

Now the above code snippet can be used in either Visual Studio on Windows or Mac as long as you got the keystore hex string. 😉

TADAAA! 😀 That’s it!

You’re welcome!

Share the love! 😉 ❤

Having Windows Bootloader Issues ? Then here is best Solution ! ;)

Installed Windows 8 on your Windows 7 PC hoping to get it dual booted ? Unfortunately the Dual boot screen (boot loader screen) went missing ? :O
Or lets say you installed Windows 7 on your Windows 8 PC hoping for a dual boot ? 😛 Aaaand tadaa ! the boot screen is missing ? :O
Or even may be you are much like my self and tried to mess with the system’s boot loader settings and eventually screwed it up ? 😉

Very well then here is the solution. “EasyBCD” ! This is a small software tool which I used sometime back when I screwed up my boot loader settings. This small software was very useful and powerful I was able to recover my bootloader within minutes. And moreover its very easy to use as well. You don’t have to buy the whole software, you can register and try out the non-commercial version with almost all the features. So if you are facing any of those above catastrophes or any of the similar situations, then go ahead and try it out as it has helped me once. 😉

https://neosmart.net/EasyBCD/

https://neosmart.net/EasyBCD/

Cheers folks ! (^_^)

I Am Feeling………….. Your Feelings and Emotions towards your Success !

Know your Mind…

Human Feelings and Emotions are a most important factor of our Lives as Humans. We as Humans, posses different Feelings and Emotions at different Situations and Different Places. There is a pattern which these Feelings and Emotions occur according to the above factors, which is actually very different from each one of us as we all are Unique from one another. We all want to live healthy life, where as both Mental and Physical health matters…

Healthy Mind…

but in both of them, Mental health is the most important one, as our minds, thoughts and feelings drive everything we do. According to psychology if we could track and see the pattern of our own Feelings and Emotions then we could use it to alter it self for our own Success ! But how could we keep track of our own Feelings and Emotions according to these different situations and places.. Now that is why we came up with a Solution !

Eureka ! psst.. wait !

So We have been trying for a solution for this devastating situation, and we finally came up with a successful answer,
“I Am Feeling”
Know your own mind…
across it self… learn it..
succeed yourself..
Yes, this is kind of a radical thought, what if you could keep a track of all your daily feelings and emotions during those different situations you go through every single day ? Now in that way…

We Welcome You !

We could learn our own Minds, our emotions, feelings to those different situations and places and alter them in order to Succeed !
Ultimately we all want to lead a mentally healthy life filled with positive thoughts, which in case this solution lets you to recognize those moments and improve them towards your success. Keep track of your own Progression of Positive thoughts, and learn yourself to succeed…

Waiting is over….

And finally it is here…..

Welcome to the ‘I Am Feeling’ App !

Welcome to the revolutionary amazing Windows Phone App,
I Am Feeling – Track your own Feelings and Emotions to learn yourself and Succeed !  Try out today for #free on your Windows Phone !

#IAmFeeling #WindowsPhone #Nokia #Lumia #WP8 #WP81 #psychology#mind #health

http://www.windowsphone.com/en-us/store/app/i-am-feeling/95e6c318-0b81-4b09-bea9-787480803079?signin=true

This app would let you easily keep track of your own Feelings and Emotions at anytime, anywhere in your daily Life, then finally allow you to monitor them and evaluate by your self to see your own progress or even to recognize what is required to improve your mental health…

We have crafted this app with Simplicity, User Friendly Experience, and Intuitive Feedback for Yourself and moreover to give you a joyful experience right from the app.

So try out this revolutionary App today ! Learn Your Mind, Your Feelings and Emotions, Learn Yourself… Speak to Your own Self.. and Succeed !

Some amazing features we bring you –

  • Keep track of all your Feelings and Emotions at anytime, anywhere in your Daily Life…
  • Keep track of all the different Places you have experienced those Feelings and Emotions…
  • Monitor yourself and your mind with extensive feedback right from the app based on your own Feelings, Places and Situations…
  • Get to know how you could Change your lifestyle, thinking, and places you visit in order to succeed yourself easily for any kind of achievement…
  • See how your mind has been fluctuating through Positivity and Negativity throughout different times, places and situations…
  • See for yourself What kind of Feelings and Emotions you experience the most, and moreover based on the Places you were at those situations…
  • Monitor your own specialized Feelings and Places Map…
  • Learn your own Mind and Yourself, and identify how you could change the way you think, do, and places you visit, and how to adopt yourself to Succeed !

We would love to hear your feedback… We count on it.. 🙂

Comment below or drop us a mail ! 😉
xtremesourcecode@live.com