Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package global.raiser.plugins.utils;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.net.Uri;
import android.util.Base64;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class KeyHashGenerator {
public static String generateKeyHash(Context context) {
final String packageName = context.getPackageName();
try {
final PackageInfo info = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
for (final Signature signature : info.signatures) {
final MessageDigest messageDigest = MessageDigest.getInstance("SHA");
messageDigest.update(signature.toByteArray());
final String signatureHash = Base64.encodeToString(messageDigest.digest(), Base64.NO_WRAP);
final Uri.Builder builder = new Uri.Builder();
final Uri uri = builder.scheme("msauth")
.authority(packageName)
.appendPath(signatureHash)
.build();
return uri.toString();
}
} catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment