android studio 查看sha1(Mac windows下的问题都可以通过此解决)

直接上代码,打个log就可以查看了,记住,最后打出来的log就是你久sha,最后一个冒号去掉

原创--http://blog.csdn.net/mr_oorange/article/details/49361001

public static String sHA1(Context context) {
    try {
        PackageInfo info = context.getPackageManager().getPackageInfo(
                context.getPackageName(), PackageManager.GET_SIGNATURES);

        byte[] cert = info.signatures[0].toByteArray();

        MessageDigest md = MessageDigest.getInstance("SHA1");
        byte[] publicKey = md.digest(cert);
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < publicKey.length; i++) {
            String appendString = Integer.toHexString(0xFF & publicKey[i])
                    .toUpperCase(Locale.US);
            if (appendString.length() == 1)
                hexString.append("0");
            hexString.append(appendString);
            hexString.append(":");
        }
        return hexString.toString();
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}
实测有效
 
  

你可能感兴趣的:(Android运用开发)