Android examples for java.security:Sha
get Certificate SHA Fingerprint for Context
import java.io.ByteArrayInputStream; import java.io.InputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; public class Main { public static String getCertificateSHA1Fingerprint(Context context) { PackageManager pm = context.getPackageManager(); int flags = PackageManager.GET_SIGNATURES; StringBuffer hexString = new StringBuffer(); try {// w w w. j ava2 s . co m PackageInfo pi = pm.getPackageInfo(context.getPackageName(), flags); android.content.pm.Signature[] signatures = pi.signatures; byte[] cert = signatures[0].toByteArray(); InputStream is = new ByteArrayInputStream(cert); CertificateFactory cf = CertificateFactory.getInstance("X509"); X509Certificate c = (X509Certificate) cf.generateCertificate(is); MessageDigest md = MessageDigest.getInstance("SHA1"); byte[] publicKey = md.digest(c.getPublicKey().getEncoded()); for (int i = 0; i < publicKey.length; i++) { String appendString = Integer.toString(0xFF & publicKey[i]); if (appendString.length() == 1) { hexString.append("0"); } hexString.append(appendString); } } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } catch (CertificateException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return hexString.toString(); } }