public static void main(String[] args) {
try {
// 读取证书文件
String str = "";
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
Base64.getDecoder().decode(str)
);
// 创建 CertificateFactory 对象
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
// 反序列化证书
X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(byteArrayInputStream);
// 创建 MessageDigest 对象,选择摘要算法,例如 SHA-256
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] documentHash = certificate.getTBSCertificate();
byte [] digests = digest.digest(documentHash);
BigInteger bigInteger = new BigInteger(convert(byteArrayToHexString(digests)), 16);
System.out.println(bigInteger);
byteArrayInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String convert(String r_hex) {
String pad_hex = "00" + "01";
int l = (4096 / 8 - 3 - ("3031300d060960864801650304020105000420" + r_hex).length() / 2);
for (int i = 0; i < l; i++) {
pad_hex = pad_hex + "FF";
}
pad_hex = pad_hex + "00" + "3031300d060960864801650304020105000420" + r_hex;
return pad_hex;
}
public static String byteArrayToHexString(byte[] byteArray) {
StringBuilder hexString = new StringBuilder();
for (byte b : byteArray) {
hexString.append(String.format("%02x", b));
}
return hexString.toString();
}
计算证书的签名
最新推荐文章于 2024-08-01 15:05:06 发布