赞
踩
开发APP 需要 使用uniapp离线打包 获取对应MD5: SHA-1 SHA-256:
代码如下(示例):
keytool -list -v -xxxxxxxxx.keystore
预计结果:
代码如下(示例):
signingConfigs {
config {
storeFile file("xxxxxxxxxxx.keystore")
storePassword '123' //秘钥
keyAlias 'com.xxxxxx'//别名
keyPassword '123' //秘钥
v1SigningEnabled true
v2SigningEnabled true
}
}
预计结果:
代码如下(示例):
import java.io.FileInputStream; import java.io.InputStream; import java.math.BigInteger; import java.security.KeyStore; import java.security.MessageDigest; import java.security.cert.Certificate; import java.util.Enumeration; public class MultiAliasKeystoreMD5 { public static void main(String[] args) { try { // 获取 keystore 文件中多个别名的 MD5、SHA-1 和 SHA-256 摘要 String keystorePath = "C:\\myProject\\xxxxxxxxx"; // 路径 String keystorePassword = "23xxxxx"; //密码 KeyStore keyStore = loadKeyStore(keystorePath, keystorePassword); if (keyStore != null) { listAliasesAndHashes(keyStore); } else { System.out.println("Failed to load keystore."); } System.out.println(); // 计算字符串的 MD5、SHA-1 和 SHA-256 摘要 String inputString = "your_input_string"; String md5String = formatMD5(calculateMD5(inputString.getBytes())); String sha1String = formatSHA1(calculateSHA1(inputString.getBytes())); String sha256String = formatSHA256(calculateSHA256(inputString.getBytes())); System.out.println("MD5 for input string: " + md5String); System.out.println("SHA-1 for input string: " + sha1String); System.out.println("SHA-256 for input string: " + sha256String); } catch (Exception e) { e.printStackTrace(); } } private static KeyStore loadKeyStore(String path, String password) { try (InputStream input = new FileInputStream(path)) { KeyStore keyStore = KeyStore.getInstance("JKS"); keyStore.load(input, password.toCharArray()); return keyStore; } catch (Exception e) { e.printStackTrace(); return null; } } private static void listAliasesAndHashes(KeyStore keyStore) { try { Enumeration<String> aliases = keyStore.aliases(); while (aliases.hasMoreElements()) { String alias = aliases.nextElement(); Certificate certificate = keyStore.getCertificate(alias); byte[] certData = certificate.getEncoded(); String md5 = formatMD5(calculateMD5(certData)); String sha1 = formatSHA1(calculateSHA1(certData)); String sha256 = formatSHA256(calculateSHA256(certData)); // 计算32位长度的十六进制数字的MD5 String md5_32 = calculateMD5(certData); System.out.println("Alias: " + alias); System.out.println("MD5: " + md5); System.out.println("MD5 (32): " + md5_32); // 输出32位长度的十六进制数字的MD5 System.out.println("SHA-1: " + sha1); System.out.println("SHA-256: " + sha256); System.out.println(); } } catch (Exception e) { e.printStackTrace(); } } private static String calculateMD5(byte[] data) { return calculateHash(data, "MD5"); } private static String calculateSHA1(byte[] data) { return calculateHash(data, "SHA-1"); } private static String calculateSHA256(byte[] data) { return calculateHash(data, "SHA-256"); } private static String calculateHash(byte[] data, String algorithm) { try { MessageDigest md = MessageDigest.getInstance(algorithm); byte[] digest = md.digest(data); BigInteger no = new BigInteger(1, digest); String hash = no.toString(16); while (hash.length() < 32) { hash = "0" + hash; } return hash; } catch (Exception e) { e.printStackTrace(); return null; } } private static String formatMD5(String md5) { return formatHash(md5); } private static String formatSHA1(String sha1) { return formatHash(sha1); } private static String formatSHA256(String sha256) { return formatHash(sha256); } private static String formatHash(String hash) { StringBuilder formattedHash = new StringBuilder(); for (int i = 0; i < hash.length(); i += 2) { formattedHash.append(hash.charAt(i)); formattedHash.append(hash.charAt(i + 1)); if (i + 2 < hash.length()) { formattedHash.append(":"); } } return formattedHash.toString().toUpperCase(); } }
执行顺序:
javac -encoding UTF-8 MultiAliasKeystoreMD5.java 生成文件
java MultiAliasKeystoreMD5 执行
预计结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。