最近APP开发要求进行备案,在APP备案时要求填写MD5、SHA1、模数等信息,由于之前维护的项目众多,为方便获取快速获取,可以通过自定义Task实现。方法如下:
/**
* 获取签名信息*/
task appSigningReport() {
group "android"
doLast {
def signingConfig = android.signingConfigs.release
def applicationId = android.defaultConfig.applicationId
def storeFile = signingConfig.storeFile
def storePassword = signingConfig.storePassword
def keyPassword = signingConfig.keyPassword
def keyAlias = signingConfig.keyAlias
FileInputStream fis = new FileInputStream(storeFile);
KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(fis, storePassword.toCharArray());
Certificate cert = keystore.getCertificate(keyAlias);
PublicKey publicKey = cert.getPublicKey();
def format = publicKey.getFormat()
System.out.println("类型:" + format)
String publicKeyType = publicKey.getAlgorithm();
System.out.println("公钥类型: " + publicKeyType);
if ("RSA" == publicKeyType) {
def exponent = ((RSAPublicKey) publicKey).getPublicExponent()
System.out.println("指数: " + exponent);
String modulus = ((RSAPublicKey) publicKey).getModulus();
System.out.println("模数: " + modulus);
getDigestPair(cert, "MD5")
getDigestPair(cert, "SHA1")
getDigestPair(cert, "SHA-256")
System.out.println("keystorePath:$storeFile")
System.out.println("keystorePSW:$storePassword")
System.out.println("aliasPSW:$keyPassword")
System.out.println("alias:$keyAlias")
System.out.println("packageName:$applicationId")
}
fis.close();
}
}
private void getDigestPair(Certificate cert, String algorithm) {
MessageDigest digest = MessageDigest.getInstance(algorithm)
byte[] bytes = digest.digest(cert.getEncoded())
StringBuilder builder = new StringBuilder()
for (byte b : bytes) {
String hex = Integer.toHexString(0xFF & b)
if (hex.length() == 1) {
builder.append('0')
}
builder.append(hex)
builder.append(':')
}
String str = builder.toString().toUpperCase()
println("${algorithm}: " + str.substring(0, str.length() - 1))
BigInteger bigInteger = new BigInteger(1, bytes);
System.out.println("${algorithm.toLowerCase()}: " + bigInteger.toString(16));
}
运行结果如下:
![](https://img.haomeiwen.com/i1425503/c2ee9936657e699e.png)
网友评论