Search in sources :

Example 11 with LocalizableValidationException

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();
}
Also used : LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) CertificateEncodingException(java.security.cert.CertificateEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

LocalizableValidationException (com.vmware.xenon.common.LocalizableValidationException)11 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 IOException (java.io.IOException)3 File (java.io.File)2 BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)2 PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)2 Test (org.junit.Test)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 DescribeAvailabilityZonesRequest (com.amazonaws.services.ec2.model.DescribeAvailabilityZonesRequest)1 DescribeAvailabilityZonesResult (com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult)1 SubscriptionState (com.microsoft.azure.management.resources.SubscriptionState)1 SubscriptionClientImpl (com.microsoft.azure.management.resources.implementation.SubscriptionClientImpl)1 SubscriptionInner (com.microsoft.azure.management.resources.implementation.SubscriptionInner)1 EndpointConfigRequest (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest)1 PRIVATE_KEYID_KEY (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.PRIVATE_KEYID_KEY)1 PRIVATE_KEY_KEY (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.PRIVATE_KEY_KEY)1 REGION_KEY (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.REGION_KEY)1 RequestType (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.RequestType)1 SUPPORT_DATASTORES (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.SUPPORT_DATASTORES)1 SUPPORT_PUBLIC_IMAGES (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.SUPPORT_PUBLIC_IMAGES)1