use of com.vmware.xenon.common.LocalizableValidationException in project photon-model by vmware.
the class CertificateUtil method computeCertificateThumbprint.
/**
* Retrieve thumbprint of a X.509 certificate as specified in {@link ThumbprintAlgorithm}
* parameter.
* @param cert
* certificate
* @param thumbprintAlgorithm
* the type of {@link ThumbprintAlgorithm}
* @return the thumbprint corresponding to the certificate; {@code not-null} value
* @throws IllegalStateException
* if an error occur while getting the encoded form of the certificates
* @throws IllegalArgumentException
* if an error occur while getting the encoded form of the certificates
*/
public static String computeCertificateThumbprint(X509Certificate cert, ThumbprintAlgorithm thumbprintAlgorithm) {
if (cert == null) {
throw new LocalizableValidationException("certificate must not be null.", "security.certificate.null.error");
}
byte[] digest;
try {
MessageDigest md = MessageDigest.getInstance(thumbprintAlgorithm.getAlgorithmName());
digest = md.digest(cert.getEncoded());
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
} catch (CertificateEncodingException e) {
throw new IllegalArgumentException(e);
}
StringBuilder thumbprint = new StringBuilder();
for (int i = 0, len = digest.length; i < len; ++i) {
if (i > 0) {
thumbprint.append(':');
}
byte b = digest[i];
thumbprint.append(HEX.charAt((b & 0xF0) >> 4));
thumbprint.append(HEX.charAt(b & 0x0F));
}
return thumbprint.toString();
}
Aggregations