Skip to content

Instantly share code, notes, and snippets.

@UdaraAlwis
Created March 5, 2019 10:41
Show Gist options
  • Save UdaraAlwis/06bc10542403ff17fdd2db9b44c9a8fc to your computer and use it in GitHub Desktop.
Save UdaraAlwis/06bc10542403ff17fdd2db9b44c9a8fc to your computer and use it in GitHub Desktop.
Generate Android App Hash Key in Xamarin using Package Name and Keystore Signature
// 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment