How to read SHA and MD5 fingerprint programmatically in Android

N Sharma picture N Sharma · Jan 31, 2015 · Viewed 8.8k times · Source

Hello I want to read SHA and MD5 fingerprint value of keystore programmatically of my app from which it was signed.

I'll take either SHA or MD5 value as key for security. This key I will use in the code to encrypt something and decrypt same at server end.

Is there any way to find this or is there any way to do same using different good approach. This should be in such a way nobody other can find this key.

Thanks in advance.

Answer

nbaroz picture nbaroz · Feb 9, 2015
PackageInfo info;
try {

    info = getPackageManager().getPackageInfo(
        "com.your.package.name", PackageManager.GET_SIGNATURES);

    for (Signature signature : info.signatures) {
        MessageDigest md;
        md = MessageDigest.getInstance("SHA");
        md.update(signature.toByteArray());
        String hash_key = new String(Base64.encode(md.digest(), 0));
    }

} catch (NameNotFoundException e1) {
} catch (NoSuchAlgorithmException e) {
} catch (Exception e) {
}