/** * 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(); }