如何获取SHA1?

SHA1 是为您的应用安装包进行签名的证书的指纹信息,它是您使用的签名证书唯一性的表示形式,通过 SHA1 可以验证安装包是否被篡改。

更多详细信息请查看密钥、证书和密钥库

签名文件的 SHA1 的值可以通过下面提供的任何一种方式获取。

一、通过 keytool 工具获取

1、打开系统命令行或Android Studio的Terminal工具

2、输入命令 keytool -v -list -keystore keystore文件路径。(keytool 是由 JDK 提供的,具体路径为您本地 Java 安装路径的 bin 目录下。)

3、输入 Keystore 的密码。

如下图所示:

img

二、通过代码获取

具体代码

package com.example.myapplication;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.text.TextUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

public class SigningUtil {

    public static final String MD5 = "MD5";
    public final static String SHA1 = "SHA1";
    public final static String SHA256 = "SHA256";

      /**
       *  get sha1 by this method
       */ 
    public static String getSHA1(Context context) {
        ArrayList<String> signInfoList = getSignInfo(context, SHA1);
        if (signInfoList != null && !signInfoList.isEmpty()) {
            return signInfoList.get(0);
        }
        return "";
    }

    private static ArrayList<String> getSignInfo(Context context, String type) {
        if (context == null || type == null) {
            return null;
        }

        String packageName = context.getPackageName();
        if (TextUtils.isEmpty(packageName)) {
            return null;
        }

        ArrayList<String> result = new ArrayList<>();

        Signature[] signatures = getSignatures(context, packageName);
        if (signatures != null) {
            for (Signature signature : signatures) {
                String tmp = "error!";
                if (MD5.equals(type)) {
                    tmp = getSignatureByteString(signature, MD5);
                } else if (SHA1.equals(type)) {
                    tmp = getSignatureByteString(signature, SHA1);
                } else if (SHA256.equals(type)) {
                    tmp = getSignatureByteString(signature, SHA256);
                }
                result.add(tmp);
            }
        }
        return result;
    }

    private static Signature[] getSignatures(Context context, String packageName) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
            return packageInfo.signatures;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String getSignatureByteString(Signature signature, String type) {
        byte[] hexBytes = signature.toByteArray();
        String fingerprint = "error!";
        try {
            MessageDigest digest = MessageDigest.getInstance(type);
            byte[] digestBytes = digest.digest(hexBytes);
            StringBuilder stringBuilder = new StringBuilder();
            for (byte digestByte : digestBytes) {
                stringBuilder.append((Integer.toHexString((digestByte & 0xFF) | 0x100)).substring(1, 3).toUpperCase());
                stringBuilder.append(":");
            }
            fingerprint = stringBuilder.substring(0, stringBuilder.length() - 1);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return fingerprint;
    }
}

results matching ""

    No results matching ""