姬長信(Redy)

用Java生成X509Certificate的主题哈希


我目前正在尝试使用Java Security API和BouncyCastle生成主题哈希.

当我使用Openssl库时,我就是这样做的:

openssl x509 -in  /Users/Sn0wfreezeDev/Downloads/Test.pem -hash

这会生成一个短的8位数哈希值1817886a

这是我的Java代码

X509Certificate cert = CertManager.getCertificate(number, c);  
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
System.out.println("  Subject " + cert.getSubjectDN());
System.out.println("   Issuer  " + cert.getIssuerDN());
sha1.update(cert.getSubjectDN().getName().getBytes());
String hexString =  bytesToHex(sha1.digest());
System.out.println("   sha1    " + hexString);
System.out.println();