Last active
July 21, 2020 10:14
-
-
Save UdaraAlwis/c83ebd573d262552b5bd0836058be58b to your computer and use it in GitHub Desktop.
Generate Android App Hash Key in Xamarin
This file contains hidden or 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; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 36 is obsolete:
var signatures = packageManager.GetPackageInfo(context.PackageName, PackageInfoFlags.Signatures).Signatures;
Are you able to correct it?