gpt4 book ai didi

android - 如何验证该应用程序是由我的证书签名的?

转载 作者:太空狗 更新时间:2023-10-29 16:20:02 25 4
gpt4 key购买 nike

如何检查我的应用程序的签名是否与我用来签署它的证书的签名相匹配?

这就是我应该如何获得证书指纹:

public String getCertificateFingerprint() throws NameNotFoundException, CertificateException, NoSuchAlgorithmException {
PackageManager pm = context.getPackageManager();
String packageName =context.getPackageName();

int flags = PackageManager.GET_SIGNATURES;

PackageInfo packageInfo = null;

packageInfo = pm.getPackageInfo(packageName, flags);
Signature[] signatures = packageInfo.signatures;

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

InputStream input = new ByteArrayInputStream(cert);

CertificateFactory cf = null;
cf = CertificateFactory.getInstance("X509");

X509Certificate c = null;
c = (X509Certificate) cf.generateCertificate(input);

MessageDigest md = MessageDigest.getInstance("MD5");
byte[] publicKey = md.digest(c.getPublicKey().getEncoded());

StringBuffer hexString = new StringBuffer();
for (int i = 0; i < publicKey.length; i++) {
String appendString = Integer.toHexString(0xFF & publicKey[i]);
if (appendString.length() == 1)
hexString.append("0");
hexString.append(appendString);
}

return hexString.toString();
}

这就是我应该如何获得我的证书的指纹:

keytool -v -list -keystore filenameandpath

我的问题是,这两个返回不同的结果。有人可以指出我搞砸了什么吗?

最佳答案

您正在计算错误数据的 MD5 哈希值。证书的指纹是原始证书的哈希值(MD5、SHA1、SHA256 等)。即,您应该计算这些字节的哈希值:

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

例如,以下计算 SHA1 指纹,如果您愿意,只需将 SHA1 更改为 MD5。

    public String computeFingerPrint(final byte[] certRaw) {

String strResult = "";

MessageDigest md;
try {
md = MessageDigest.getInstance("SHA1");
md.update(certRaw);
for (byte b : md.digest()) {
strAppend = Integer.toString(b & 0xff, 16);
if (strAppend.length() == 1)
strResult += "0";
strResult += strAppend;
}
strResult = strResult.toUpperCase(DATA_LOCALE);
}
catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}

return strResult;
}

关于android - 如何验证该应用程序是由我的证书签名的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17548756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com