/** * Creates a String representation of the given object. * * @param certificate to print * @return the String representation */ private String toString(Object certificate) { final StringBuilder sb = new StringBuilder(); sb.append("<html><body>\n"); if (certificate instanceof X509Certificate) { renderX509(sb, (X509Certificate) certificate); } else { sb.append("<pre>\n"); sb.append(certificate.toString()); sb.append("</pre>\n"); } sb.append("</body></html>"); return sb.toString(); }
/** * Converts the byte array to hex string. * * @param raw the data. * @return the hex string. */ private String getHex(byte[] raw) { if (raw == null) return null; StringBuilder hex = new StringBuilder(2 * raw.length); Formatter f = new Formatter(hex); try { for (byte b : raw) f.format("%02x", b); } finally { f.close(); } return hex.toString(); }
/** * Calculates the hash of the certificate known as the "thumbprint" and returns it as a string * representation. * * @param cert The certificate to hash. * @param algorithm The hash algorithm to use. * @return The SHA-1 hash of the certificate. * @throws CertificateException */ private static String getThumbprint(X509Certificate cert, String algorithm) throws CertificateException { MessageDigest digest; try { digest = MessageDigest.getInstance(algorithm); } catch (NoSuchAlgorithmException e) { throw new CertificateException(e); } byte[] encodedCert = cert.getEncoded(); StringBuilder sb = new StringBuilder(encodedCert.length * 2); Formatter f = new Formatter(sb); try { for (byte b : digest.digest(encodedCert)) f.format("%02x", b); } finally { f.close(); } return sb.toString(); }