有些SDK提供方需要我们提供打包APK时使用的包名和签名文件(xxxxxx.jks这个文件)MD5值,然后SDK服务端应该会进行比对保证商户调用的合法性。不然光靠包名是可以伪造的,怎么获取签名文件的MD5值?
方法一:
原先通过命令keytool -list -v -keystore test.jks 可以查看md5值,但是发现现在查看不了,只有SHA1和SHA256的
方法二:改成从androidstudio查看
在app的build.gradle中配置你们签名文件,怎么生成签名文件?
build-->Generate signed bundle\APK-->next -->Create new
android{
signingConfigs { release { keyAlias 'test' storePassword '666666' keyPassword '666666' storeFile file('E:\\test.jks') } debug { keyAlias 'test' storePassword '666666' keyPassword '666666' storeFile file('E:\\test.jks') } }
}
右侧gradle Tasks里面双击signingReport就可以查看签名文件的MD5值了
方法三:从PackageInfo类中获取
- package com.chinapay.umsfacesdkdemo.utils;
-
- import android.content.Context;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.content.pm.Signature;
- import android.util.Log;
-
- import java.security.MessageDigest;
- import java.util.ArrayList;
- import java.util.HashMap;
-
-
- /**
- * 获取签名工具类
- */
- public class AppSigning {
- public final static String MD5 = "MD5";
- public final static String SHA1 = "SHA1";
- public final static String SHA256 = "SHA256";
- private static HashMap
> mSignMap = new HashMap<>(); -
- /**
- * 返回一个签名的对应类型的字符串
- *
- * @param context
- * @param type
- * @return 因为一个安装包可以被多个签名文件签名,所以返回一个签名信息的list
- */
- public static ArrayList
getSignInfo(Context context, String type) { - if (context == null || type == null) {
- return null;
- }
- String packageName = context.getPackageName();
- if (packageName == null) {
- return null;
- }
- if (mSignMap.get(type) != null) {
- return mSignMap.get(type);
- }
- ArrayList
mList = new ArrayList(); - try {
- Signature[] signs = getSignatures(context, packageName);
- for (Signature sig : signs) {
- String tmp = "error!";
- if (MD5.equals(type)) {
- tmp = getSignatureByteString(sig, MD5);
- } else if (SHA1.equals(type)) {
- tmp = getSignatureByteString(sig, SHA1);
- } else if (SHA256.equals(type)) {
- tmp = getSignatureByteString(sig, SHA256);
- }
- mList.add(tmp);
- }
- } catch (Exception e) {
- Log.e("e", e.getMessage());
- }
- mSignMap.put(type, mList);
- return mList;
- }
-
- /**
- * 获取签名sha1值
- *
- * @param context
- * @return
- */
- public static String getSha1(Context context) {
- String res = "";
- ArrayList
mlist = getSignInfo(context, SHA1); - if (mlist != null && mlist.size() != 0) {
- res = mlist.get(0);
- }
- return res;
- }
-
- /**
- * 获取签名MD5值
- *
- * @param context
- * @return
- */
- public static String getMD5(Context context) {
- String res = "";
- ArrayList
mlist = getSignInfo(context, MD5); - if (mlist != null && mlist.size() != 0) {
- res = mlist.get(0);
- }
- return res;
- }
-
- /**
- * 获取签名SHA256值
- *
- * @param context
- * @return
- */
- public static String getSHA256(Context context) {
- String res = "";
- ArrayList
mlist = getSignInfo(context, SHA256); - if (mlist != null && mlist.size() != 0) {
- res = mlist.get(0);
- }
- return res;
- }
-
- /**
- * 返回对应包的签名信息
- *
- * @param context
- * @param packageName
- * @return
- */
- private static Signature[] getSignatures(Context context, String packageName) {
- PackageInfo packageInfo = null;
- try {
- packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
- return packageInfo.signatures;
- } catch (Exception e) {
- Log.e("e", e.toString());
- }
- return null;
- }
-
- /**
- * 获取相应的类型的字符串(把签名的byte[]信息转换成16进制)
- *
- * @param sig
- * @param type
- * @return
- */
- private static String getSignatureString(Signature sig, String type) {
- byte[] hexBytes = sig.toByteArray();
- String fingerprint = "error!";
- try {
- MessageDigest digest = MessageDigest.getInstance(type);
- if (digest != null) {
- byte[] digestBytes = digest.digest(hexBytes);
- StringBuilder sb = new StringBuilder();
- for (byte digestByte : digestBytes) {
- sb.append((Integer.toHexString((digestByte & 0xFF) | 0x100)).substring(1, 3));
- }
- fingerprint = sb.toString();
- }
- } catch (Exception e) {
- Log.e("e", e.toString());
-
- }
-
- return fingerprint;
- }
-
- /**
- * 获取相应的类型的字符串(把签名的byte[]信息转换成 95:F4:D4:FG 这样的字符串形式)
- *
- * @param sig
- * @param type
- * @return
- */
- private static String getSignatureByteString(Signature sig, String type) {
- byte[] hexBytes = sig.toByteArray();
- String fingerprint = "error!";
- try {
- MessageDigest digest = MessageDigest.getInstance(type);
- if (digest != null) {
- byte[] digestBytes = digest.digest(hexBytes);
- StringBuilder sb = new StringBuilder();
- for (byte digestByte : digestBytes) {
- sb.append(((Integer.toHexString((digestByte & 0xFF) | 0x100)).substring(1, 3)).toUpperCase());
- sb.append(":");
- }
- fingerprint = sb.substring(0, sb.length() - 1).toString();
- }
- } catch (Exception e) {
- Log.e("e", e.toString());
- }
-
- return fingerprint;
- }
- }
-
-
-
-
调用上面的 String md5=AppSigning.getMD5(MainActivity.this)就可以了